Validation process bug
If a field is not required then the other chained validation functions should ONLY come into play IFF a value is present in that field and
For example I have this field that I wish to be NOT required but have a length of 3 - 128 if a value is present. check(value).len(3, 128).is(/^([-a-z0-9 ])+$/i); will always be required since the length has to be more that three always. This field is not required but the validator forces it to be.
I usually just wrap it in a conditional:
if ( req.body.foo ) {
check(foo).len(3,128)
}
My set up is a bit different. I have a bruteforce validation function, but in any case a validator should not be performing TWO functions, I believe. Makes it easier to use it into many more different ways.
I will have to make it work, but something to consider for future updates
If you're talking about request parameter validation then this is an express-validator issue. node-validator is responsible for validating the strings, it should not be aware of where the strings are coming from.
Also, what's wrong with this form?
if (param_exists) {
//validate param
}
Not refering to request param.
var check = require('validator').check; var _ = require('underscore');
exports.validateObjects = function (obj) { var errors = {};
_.each(obj, function(value, key) {
try
{
switch(key)
{
case 'status_id':
check(value).isNumeric();
break;
case 'type_id':
check(value).isNumeric();
break;
case 'fax':
check(value).len(9, 16).is(/^[01]?[- .]?\(?[2-9]\d{2}\)?[- .]?\d{3}[- .]?\d{4}$/);
break;
default:
throw 'This is an unrecognised input field.';
}
}
catch(e)
{
errors[key] = e;
}
});
return errors;
}
This is how i do my validation. I do not wish to have my validation tied to a form. i would like to validate data as i wish by passing a object (key value pair). and it allows me a DRY approach.
You shouldn't have to use that should be built in
if (param_exists) { //validate param }
there is a function that handles that function. What happens is that functions like len(9, 16) also has is saying the field is required.
i.e. If i was validating a zip code with only len(5,9) validator it will always show an error although the zip is NOT required because the zip must be at least 5.
So... the if statement you provided should be in the validators and not have to be written explicitly.
I disagree. The library handles string validation only; incorporating control flow is completely outside the scope of the library.
Yes , it does handle string validation only! I wouldn't call that control flow. The len and other functions have included a required aspect. It assumes the form field I am validating is required. each time i use len on a form element doesn't mean the field is required.
doing the below for each param, or input, or field is inefficient.
if (param_exists) { //validate param }
I will however change it to meet my requirements.
You don't need to do it per field though. A quick refactor removes two dependencies and eliminates duplicate code
var validators = {
status_id: /^[0-9]+$/
, type_id: /^[0-9]+$/
, fax: /^[01]?[- .]?\(?[2-9]\d{2}\)?[- .]?\d{3}[- .]?\d{4}$/
};
var optional = {
fax: true
};
function validate(object) {
for (var key in validator) {
if (!(key in object)) {
if (optional[key]) {
continue;
}
//handle missing value
} else if (!validators[key].test(object[key])) {
//handle invalid value
}
}
}
The if statement will suffice for my situation