Fixed nasty bug where mixins were bashing the schemas passed to them
This commit is contained in:
@@ -17,7 +17,7 @@ import updateSchemaMixin from '/imports/api/mixins/updateSchemaMixin.js';
|
||||
|
||||
let Features = new Mongo.Collection('features');
|
||||
|
||||
let FeatureSchema = schema({
|
||||
let FeatureSchema = new SimpleSchema({
|
||||
name: {
|
||||
type: String,
|
||||
optional: true,
|
||||
@@ -38,18 +38,21 @@ let FeatureSchema = schema({
|
||||
|
||||
FeatureSchema.extend(ColorSchema);
|
||||
|
||||
Features.attachSchema(FeatureSchema);
|
||||
Features.attachSchema(PropertySchema);
|
||||
Features.attachSchema(ChildSchema);
|
||||
Features.attachSchema(
|
||||
schema(FeatureSchema)
|
||||
.extend(PropertySchema)
|
||||
.extend(ChildSchema)
|
||||
);
|
||||
|
||||
|
||||
const insertFeature = new ValidatedMethod({
|
||||
name: 'Features.methods.insert',
|
||||
mixins: [
|
||||
creaturePermissionMixin,
|
||||
setDocAncestryMixin,
|
||||
ensureAncestryContainsCharIdMixin,
|
||||
setDocToLastMixin,
|
||||
simpleSchemaMixin,
|
||||
simpleSchemaMixin,
|
||||
ensureAncestryContainsCharIdMixin,
|
||||
setDocAncestryMixin,
|
||||
creaturePermissionMixin,
|
||||
],
|
||||
collection: Features,
|
||||
permission: 'edit',
|
||||
|
||||
@@ -1,22 +1,12 @@
|
||||
import SimpleSchema from 'simpl-schema';
|
||||
|
||||
const getColorSchema = function({optional = true} = {}){
|
||||
let schema = {
|
||||
const ColorSchema = new SimpleSchema({
|
||||
color: {
|
||||
type: String,
|
||||
// match hex colors of the form #A23 or #A23f56
|
||||
regEx: /^#([a-f0-9]{3}){1,2}\b$/i,
|
||||
};
|
||||
if (optional) {
|
||||
schema.optional = true;
|
||||
} else {
|
||||
schema.defaultValue = "#9E9E9E";
|
||||
}
|
||||
return schema
|
||||
};
|
||||
|
||||
const ColorSchema = new SimpleSchema({
|
||||
color: getColorSchema(),
|
||||
optional: true,
|
||||
},
|
||||
});
|
||||
|
||||
export default ColorSchema;
|
||||
export { getColorSchema };
|
||||
|
||||
@@ -8,12 +8,12 @@ export function setDocToLastMixin(methodOptions){
|
||||
if (methodOptions.validate){
|
||||
throw new Meteor.Error(`setDocToLastMixin should come before simpleSchemaMixin`);
|
||||
}
|
||||
methodOptions.schema.extend({
|
||||
methodOptions.schema = new SimpleSchema({
|
||||
charId: {
|
||||
type: String,
|
||||
regEx: SimpleSchema.RegEx.Id,
|
||||
},
|
||||
});
|
||||
}).extend(methodOptions.schema);
|
||||
let collection = methodOptions.collection;
|
||||
if (!collection){
|
||||
throw new Meteor.Error("`collection` required in method options for setDocToLastMixin");
|
||||
|
||||
@@ -81,7 +81,7 @@ export function getAncestry({id, collection}){
|
||||
};
|
||||
|
||||
// Ancestors is [...parent's ancestors, parent ref]
|
||||
let ancestors = parentDoc.ancestors;
|
||||
let ancestors = parentDoc.ancestors || [];
|
||||
ancestors.push(parent);
|
||||
|
||||
return {parent, ancestors};
|
||||
@@ -90,9 +90,10 @@ export function getAncestry({id, collection}){
|
||||
export function setDocAncestryMixin(methodOptions){
|
||||
// Extend the method's schema to require the needed properties
|
||||
// This mixin should come before simpleschema mixin
|
||||
methodOptions.schema.extend({
|
||||
methodOptions.schema = new SimpleSchema({
|
||||
parent: {
|
||||
type: Object,
|
||||
optional: true,
|
||||
},
|
||||
'parent.id': {
|
||||
type: String,
|
||||
@@ -101,10 +102,14 @@ export function setDocAncestryMixin(methodOptions){
|
||||
'parent.collection': {
|
||||
type: String,
|
||||
},
|
||||
});
|
||||
}).extend(methodOptions.schema);
|
||||
// Change the doc's ancestry before running
|
||||
let runFunc = methodOptions.run;
|
||||
methodOptions.run = function(doc, ...rest){
|
||||
// If the doc's parent doesn't exist, set it to the character
|
||||
if (!doc.parent && doc.charId) {
|
||||
doc.parent = {id: doc.charId, collection: 'creatures'};
|
||||
}
|
||||
let {parent, ancestors} = getAncestry(doc.parent);
|
||||
doc.parent = parent;
|
||||
doc.ancestors = ancestors;
|
||||
@@ -137,12 +142,12 @@ function ensureAncestryContainsId(ancestors, id){
|
||||
export function ensureAncestryContainsCharIdMixin(methodOptions){
|
||||
// Extend the method's schema to require the needed properties
|
||||
// This mixin should come before simpleSchemaMixin
|
||||
methodOptions.schema.extend({
|
||||
methodOptions.schema = new SimpleSchema({
|
||||
charId: {
|
||||
type: String,
|
||||
regEx: SimpleSchema.RegEx.Id,
|
||||
},
|
||||
});
|
||||
}).extend(methodOptions.schema);
|
||||
let runFunc = methodOptions.run;
|
||||
methodOptions.run = function({charId, ancestors}){
|
||||
ensureAncestryContainsId(ancestors, charId);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import SimpleSchema from 'simpl-schema';
|
||||
|
||||
function getDefaultSchema(){
|
||||
return new SimpleSchema({}, {
|
||||
export default function schema(options){
|
||||
return new SimpleSchema(options, {
|
||||
clean: {
|
||||
filter: true,
|
||||
autoConvert: true,
|
||||
@@ -11,8 +11,4 @@ function getDefaultSchema(){
|
||||
removeNullsFromArrays: true,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export default function schema(options){
|
||||
return getDefaultSchema().extend(options);
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user