Skip to content

Commit

Permalink
Merge pull request balderdashy#1419 from balderdashy/streamline-valid…
Browse files Browse the repository at this point in the history
…ations

[patch] Streamline validations
  • Loading branch information
particlebanana authored Dec 21, 2016
2 parents fe7f635 + 8df57e8 commit f2eedb0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 41 deletions.
1 change: 1 addition & 0 deletions accessible/allowed-validations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('anchor/accessible/rules');
46 changes: 6 additions & 40 deletions lib/waterline/utils/system/validation-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,22 @@

var _ = require('@sailshq/lodash');
var anchor = require('anchor');
var RESERVED_PROPERTY_NAMES = require('./reserved-property-names');
var RESERVED_VALIDATION_NAMES = require('./reserved-validation-names');
var RESERVED_VALIDATION_NAMES = require('../../../../accessible/allowed-validations');

module.exports = function ValidationBuilder(attributes) {
// Hold the validations used for each attribute
var validations = {};


// ╔╦╗╔═╗╔═╗ ┌─┐┬ ┬┌┬┐ ┬ ┬┌─┐┬ ┬┌┬┐┌─┐┌┬┐┬┌─┐┌┐┌┌─┐
// ║║║╠═╣╠═╝ │ ││ │ │ └┐┌┘├─┤│ │ ││├─┤ │ ││ ││││└─┐
// ╩ ╩╩ ╩╩ └─┘└─┘ ┴ └┘ ┴ ┴┴─┘┴─┴┘┴ ┴ ┴ ┴└─┘┘└┘└─┘
_.each(attributes, function(attribute, attributeName) {
// Build a validation list for the attribute
validations[attributeName] = {};
var validations = _.reduce(attributes, function(memo, attribute, attributeName) {

// Process each property in the attribute and look for any validation
// properties.
_.each(attribute, function(property, propertyName) {
// Ignore NULL values
if (_.isNull(property)) {
return;
}

// If the property is reserved, don't do anything with it
if (_.indexOf(RESERVED_PROPERTY_NAMES, propertyName) > -1) {
return;
}

// If the property is an `enum` alias it to the anchor IN validation
if (propertyName.toLowerCase() === 'enum') {
validations[attributeName].in = property;
return;
}
// Build a validation list for the attribute
memo[attributeName] = attribute.validations || {};

// Otherwise validate that the property name is a valid anchor validation.
if (_.indexOf(RESERVED_VALIDATION_NAMES, propertyName) < 0) {
return;
}
return memo;

// Set the validation
validations[attributeName][propertyName] = property;
});
});
}, {});


// ╔╗ ╦ ╦╦╦ ╔╦╗ ┬ ┬┌─┐┬ ┬┌┬┐┌─┐┌┬┐┬┌─┐┌┐┌ ┌─┐┌┐┌
Expand Down Expand Up @@ -121,13 +94,6 @@ module.exports = function ValidationBuilder(attributes) {
}
}

// If Boolean and required manually check
if (curValidation.required && curValidation.type === 'boolean' && (!_.isUndefined(value) && !_.isNull(value))) {
if (value.toString() === 'true' || value.toString() === 'false') {
return;
}
}

// Run the Anchor validations
var validationError = anchor(value).to(requirements.data, values);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
],
"dependencies": {
"@sailshq/lodash": "^3.10.2",
"anchor": "~0.11.2",
"anchor": "^1.0.0",
"async": "2.0.1",
"bluebird": "3.2.1",
"flaverr": "^1.0.0",
Expand Down

0 comments on commit f2eedb0

Please sign in to comment.