validatorjs
validatorjs copied to clipboard
Empty Array
There is no way to write a test which accepts only Arrays including empty array. also your unit tests for custom tests arent reflective of this case:
var Validator = require('validatorjs');
var data = { baseTracks: [] };
var rules = {
baseTracks: 'required|array',
};
var validation = new Validator(data, rules);
console.log(validation.passes()); // false
console.log(validation.fails()); // true
console.log(validation.errors.all()); // { baseTracks: [ 'The baseTracks field is required.' ] }
console.log(validation.getRule('this should throw right?'));
/**
{ name: 'this should throw right?',
fn: undefined,
passes: null,
_customMessage: undefined,
async: false,
validator:
Validator {
input: { baseTracks: [] },
messages:
Messages {
lang: 'en',
messages: [Object],
customMessages: {},
attributeNames: {},
attributeFormatter: [Function: formatter] },
errors: Errors { errors: [Object] },
errorCount: 2,
hasAsync: false,
rules: { baseTracks: [Object] } } }
*/
also came across this:
var Validator = require('validatorjs');
var data = { baseTracks: '' };
var rules = {
baseTracks: 'array',
};
var validation = new Validator(data, rules);
console.log(validation.passes()); // true
console.log(validation.fails()); // false
the problem is in _isValidatable method. it uses rule "required" and interprets results in strange manner. null and undefined is not "isValidatable" but, also empty string too. so u can place any (which not in "implicit" array) rule in "rules" and get "true"