From 91fb1d74ff7cd5d64534b8dbf46e42882a920d01 Mon Sep 17 00:00:00 2001 From: Andrew Greenstreet Date: Wed, 23 May 2018 17:33:13 -0700 Subject: [PATCH 1/2] Add attributeMode strict or flexible to schema.js Allow the user to define whatever properties they would like on an attribute, while still maintaining standard attribute validation. --- lib/waterline-schema/schema.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/waterline-schema/schema.js b/lib/waterline-schema/schema.js index 9f100a4..18cf38e 100644 --- a/lib/waterline-schema/schema.js +++ b/lib/waterline-schema/schema.js @@ -132,6 +132,9 @@ module.exports = function schemaBuilder(collections) { tableName: collection.tableName, datastore: collection.datastore || collection.connection, attributes: _.merge({}, collection.attributes), + //Allow the attribute properties to be flexibly defined, + //e.g. phone: {type:"string", isMobilePhone:true, isXYZProperty:false} + attributeMode: collection.attributeMode && collection.attributeMode === "flexible" || "strict", // The schema piece will be transformed along the way to reflect the // underlying datastructure. i.e. expanding out associations into foreign // keys, etc. @@ -234,11 +237,13 @@ module.exports = function schemaBuilder(collections) { // ╚╝ ╩ ╩╩═╝╩═╩╝╩ ╩ ╩ ╚═╝ ┴ ┴└─└─┘┴ └─┘┴└─ ┴ ┴└─┘└─┘ // If the attribute contains a property that isn't whitelisted, then return // an error. - _.each(attribute, function parseProperties(propertyValue, propertyName) { - if (_.indexOf(validProperties, propertyName) < 0) { - throw flaverr({ name: 'userError' }, Error('The attribute `' + attributeName + '` on the `' + collection.identity + '` model contains invalid properties. The property `' + propertyName + '` isn\'t a recognized property.')); - } - }); + if (schemaCollection.attributeMode === "strict") { + _.each(attribute, function parseProperties(propertyValue, propertyName) { + if (_.indexOf(validProperties, propertyName) < 0) { + throw flaverr({ name: 'userError' }, Error('The attribute `' + attributeName + '` on the `' + collection.identity + '` model contains invalid properties. The property `' + propertyName + '` isn\'t a recognized property.')); + } + }); + } // ╦ ╦╔═╗╦ ╦╔╦╗╔═╗╔╦╗╔═╗ ┌─┐┬ ┬ ┌─┐┬ ┬╔╗╔┬ ┬┬ ┬ From 2404c4d89e1a91ca20622105bb49f6d548131556 Mon Sep 17 00:00:00 2001 From: Andrew Greenstreet Date: Wed, 23 May 2018 17:44:18 -0700 Subject: [PATCH 2/2] Allow non-standard attribute properties Adds a flag: attributeWhitelist to model to bypass attribute property validation. While this is fine, it doesn't allow for custom properties to be added to attributes. This might be helpful to some users, but others might want to make distinct model schemas the contain isMobilePhone, or JSON Schema style snytax for an enum, etc. --- lib/waterline-schema/schema.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/waterline-schema/schema.js b/lib/waterline-schema/schema.js index 18cf38e..9bdae36 100644 --- a/lib/waterline-schema/schema.js +++ b/lib/waterline-schema/schema.js @@ -134,7 +134,7 @@ module.exports = function schemaBuilder(collections) { attributes: _.merge({}, collection.attributes), //Allow the attribute properties to be flexibly defined, //e.g. phone: {type:"string", isMobilePhone:true, isXYZProperty:false} - attributeMode: collection.attributeMode && collection.attributeMode === "flexible" || "strict", + attributeWhitelist: collection.attributeWhitelist && collection.attributeWhitelist === "flexible" || "strict", // The schema piece will be transformed along the way to reflect the // underlying datastructure. i.e. expanding out associations into foreign // keys, etc. @@ -237,7 +237,7 @@ module.exports = function schemaBuilder(collections) { // ╚╝ ╩ ╩╩═╝╩═╩╝╩ ╩ ╩ ╚═╝ ┴ ┴└─└─┘┴ └─┘┴└─ ┴ ┴└─┘└─┘ // If the attribute contains a property that isn't whitelisted, then return // an error. - if (schemaCollection.attributeMode === "strict") { + if (schemaCollection.attributeWhitelist === "strict") { _.each(attribute, function parseProperties(propertyValue, propertyName) { if (_.indexOf(validProperties, propertyName) < 0) { throw flaverr({ name: 'userError' }, Error('The attribute `' + attributeName + '` on the `' + collection.identity + '` model contains invalid properties. The property `' + propertyName + '` isn\'t a recognized property.'));