Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added number coercion for validatesLength qualifier arguments. #35

Merged
merged 1 commit into from

2 participants

@lgomezma

Adds number coercion to the validatesLength min, max and exact length qualifiers so it will accept for example "42" instead of 42, as discussed in issue #34

@mde mde merged commit 70b2052 into from
@mde
Owner

I'll probably do a bit of cleanup on this, but it's a big improvement already, thanks!

@lgomezma

Thanks to you for merging :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 17, 2013
  1. @lgomezma
This page is out of date. Refresh to see the latest.
Showing with 15 additions and 0 deletions.
  1. +15 −0 lib/validators.js
View
15 lib/validators.js
@@ -80,6 +80,11 @@ var validators = {
if (!val) {
return rule.message || 'Field "' + name + '" is required.';
}
+ if(Math.round(qual)==qual){
+ qual = Math.round(qual);
+ }else if(typeof qual !== 'undefined' && typeof qual !== 'object'){
+ throw new Error('qualifier at validatesLength needs to be a number or an object');
+ }
if (typeof qual == 'number') {
if (val.length != qual) {
//return rule.message || 'Field "' + name + '" must be ' + qual +
@@ -89,6 +94,16 @@ var validators = {
}
}
else {
+ if(Math.round(qual.min)==qual.min){
+ qual.min = Math.round(qual.min);
+ }else if(typeof qual.min !== 'undefined'){
+ throw new Error('min property of qualifier at validatesLength needs to be a number');
+ }
+ if(Math.round(qual.max)==qual.max){
+ qual.max = Math.round(qual.max);
+ }else if(typeof qual.max !== 'undefined'){
+ throw new Error('max property of qualifier at validatesLength needs to be a number');
+ }
if (typeof qual.min == 'number' && val.length < qual.min) {
//return rule.message || 'Field "' + name + '" must be at least ' +
// qual.min + ' characters long.';
Something went wrong with that request. Please try again.