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

Numeric validation has incorrect error messages #72

Closed fairbairn closed 8 years ago

fairbairn commented 8 years ago

Similar to another numeric issue, but this is different... (using version 2.0.2)

var Validator = require('validatorjs');

var v = new Validator({ amount: '123000.45' }, { amount: 'required|numeric|min:4|max:9' });
if (v.fails())
{
    console.log('error', v.errors); 

    // error { errors: { amount: [ 'The amount must be less than 9 characters.' ] } }

}

I can't tell when min, max is checking length or the actual numeric value.

In our cases, we're using web forms so it's always a string... we think it's trying to check value which is fine, we can set it like this instead...

var Validator = require('validatorjs');

var v = new Validator({ amount: '123000.45' }, { amount: 'required|numeric|min:1000|max:99999' });
if (v.fails())
{
    console.log('error', v.errors);

    // error { errors: { amount: [ 'The amount must be less than 99999 characters.' ] } }
}

but it's default error message makes no sense because it still thinks it's failing on "characters" instead of a value out of range.

I think the BUG is that when the type is numeric and min and max are value ranges, the error message is incorrect.

garygreen commented 8 years ago

I don't have time to look into this too much but I suspect it's something to do with this line it should check on the rule if it's numeric based, not simply against the typeof.

iamdtang commented 8 years ago

@fairbairn Can you cast it to a number before passing it to Validator?

fairbairn commented 8 years ago

It's not convenient simply because we're embedded in redux-forms and all of our inputs are form based, so they will always be strings. We can if we have to, but it's not a one-liner in our case.

iamdtang commented 8 years ago

@garygreen @fairbairn Do either of you know how Laravel handles this? If input looks like a number, does it treat it as a number? If not, I'll have to spin up a Laravel project and see how they handle it.

garygreen commented 8 years ago

@skaterdav85 like I said in previous comment, it just needs to check if the value is tested against a "numeric-like" rule, and if so use the numeric template when generating the error message. Laravel does something similar. At the moment it does a simple typeof -- it also needs to make use of the internal function _hasNumericRule, which it doesn't at the moment, hence the incorrect error message.

garygreen commented 8 years ago

I'll fix it quickly now.

garygreen commented 8 years ago

Fixed in 5d6752b6def9d1b17d9b07b59e20c7b6e0f50391 @skaterdav85 would you mind releasing a new version?

iamdtang commented 8 years ago

@fairbairn @garygreen released in 2.0.3!

fairbairn commented 8 years ago

Fantastic, thanks guys! Works like a champ.