mikeerickson / validatorjs

A data validation library in JavaScript for the browser and Node.js, inspired by Laravel's Validator.
https://www.npmjs.com/package/validatorjs
MIT License
1.77k stars 280 forks source link

Empty Array #215

Open alexanderankin opened 7 years ago

alexanderankin commented 7 years ago

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 commented 7 years ago

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
bfday commented 6 years ago

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"