From 6d68796a112532c096328ca343cc7c925b4026b4 Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Mon, 1 Apr 2019 16:57:29 +0200 Subject: [PATCH] Fixed nasty bug where mixins were bashing the schemas passed to them --- .../api/creature/properties/Features.js | 19 +++++++++++-------- .../api/creature/subSchemas/ColorSchema.js | 18 ++++-------------- app/imports/api/mixins/setDocToLastMixin.js | 4 ++-- app/imports/api/parenting/parenting.js | 15 ++++++++++----- app/imports/api/schema.js | 10 +++------- 5 files changed, 30 insertions(+), 36 deletions(-) diff --git a/app/imports/api/creature/properties/Features.js b/app/imports/api/creature/properties/Features.js index eba5d322..dce84077 100644 --- a/app/imports/api/creature/properties/Features.js +++ b/app/imports/api/creature/properties/Features.js @@ -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', diff --git a/app/imports/api/creature/subSchemas/ColorSchema.js b/app/imports/api/creature/subSchemas/ColorSchema.js index e0b74c61..f258c269 100644 --- a/app/imports/api/creature/subSchemas/ColorSchema.js +++ b/app/imports/api/creature/subSchemas/ColorSchema.js @@ -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 }; diff --git a/app/imports/api/mixins/setDocToLastMixin.js b/app/imports/api/mixins/setDocToLastMixin.js index 1b31b90d..db056d9b 100644 --- a/app/imports/api/mixins/setDocToLastMixin.js +++ b/app/imports/api/mixins/setDocToLastMixin.js @@ -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"); diff --git a/app/imports/api/parenting/parenting.js b/app/imports/api/parenting/parenting.js index 88bba7f0..04fd6a4e 100644 --- a/app/imports/api/parenting/parenting.js +++ b/app/imports/api/parenting/parenting.js @@ -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); diff --git a/app/imports/api/schema.js b/app/imports/api/schema.js index 4ec13855..de1f8fe9 100644 --- a/app/imports/api/schema.js +++ b/app/imports/api/schema.js @@ -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); -}; +}