validatorjs icon indicating copy to clipboard operation
validatorjs copied to clipboard

Empty Array

Open alexanderankin opened this issue 7 years ago • 2 comments

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] } } }
*/

alexanderankin avatar Oct 08 '17 02:10 alexanderankin

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

alexanderankin avatar Oct 08 '17 02:10 alexanderankin

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"

bfday avatar Feb 16 '18 14:02 bfday