-
Notifications
You must be signed in to change notification settings - Fork 337
Validators should have the option to skip all other validators on failure. #101
Comments
Since validators are not ordered this isn't really possible. |
@ansman: is there a reason the validators aren't ordered? Thoughts on adding the additional error condition to the length validator and removing the log line? |
They are not ordered because they are keys in an object which are not ordered. |
I created a wrapper for the Example Usage var data = {
email: "example@github.com"
};
var constraints = [
{
email: {
presence: true
}
},
{
email: {
checkDatabaseValidator: true
}
}
];
validate.isValid(data, constraints, function (values) {
// do things with validated data
}, function (errors) {
// invalid
});
// or promise support
validate.isValid(data, constraints)
.then(function (values) {
// do things with validated data
}, function (errors) {
// invalid
}); In the above example, if data's Wrapper validate.isValid = function (data, constraints, successCallback, failCallback) {
return new Promise(function (resolve, reject) {
constraints = Array.isArray(constraints) ? constraints : [constraints];
var combinedValues = {};
var iterate = function (constraints) {
var constraint = constraints.shift();
if (!constraint) {
resolve(combinedValues);
// Support for callbacks
if (successCallback) {
successCallback(combinedValues);
}
return;
}
this.validate.async(data, constraint)
.then(
// Success, validate next group/set
function (values) {
// Merge values to return
for (var key in values) {
combinedValues[key] = values[key];
}
iterate(constraints);
},
// Fail, stop further validation
function (errors) {
reject(errors);
// Support for callbacks
if (failCallback) {
failCallback(errors);
}
}
);
}.bind(this);
iterate(constraints);
}.bind(this));
}; |
Interesting idea, feel free to open a pull request |
Certain validators, such as length, rely on an object being of a certain type or having a certain value. An easy way to ensure this is by having a type validator come first (as in #80). However, even when doing so, the length validator will still be called on an empty object after the type validator fails and creates a console log that the length attribute is non numeric.
Example:
This will log
ERROR: '[validate.js] Attribute id has a non numeric value for length'
to the console in addition to returning an error for being the wrong type and an error for being the incorrect length. The preferred behavior would be to ONLY receive the error for being the wrong type.Ideally, each validator could have an optional attribute called
exit
that defaults tofalse
, but when set totrue
, will skip all future validators when it errors.Example:
Maybe this should go in a separate issue - but it seems like https://github.com/ansman/validate.js/blob/master/validate.js#L781 could be removed and replaced with a different error message. Length currently has the
wrongLength
,tooShort
andtooLong
messages. Another one,nonNumericLength
, could be added to account for this scenario.The text was updated successfully, but these errors were encountered: