Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimum/maximum rule doesn't work when number is zero #179

Closed
indrimuska opened this issue Dec 10, 2015 · 9 comments
Closed

Minimum/maximum rule doesn't work when number is zero #179

indrimuska opened this issue Dec 10, 2015 · 9 comments

Comments

@indrimuska
Copy link
Collaborator

Plunker example:
http://plnkr.co/edit/NfhwLeXPzmo9SANJuOrQ?p=preview

Looking for a fix.

EDIT: possible workaround is using a function rule as below:

myField: {
  required: true,
  type: 'number',
  minimum: function () { return 0; }
}
@indrimuska
Copy link
Collaborator Author

Ok, got it, the problem is here:
https://github.com/bvaughn/angular-form-for/blob/master/source/services/model-validator.ts#L421

@bvaughn is there any reason to check if minimum is set/not-null/not-zero?
Could we simply check if it is just a valid number !isNaN(validationRules.minimum) ?

@bvaughn
Copy link
Owner

bvaughn commented Dec 11, 2015

Ah. The general problem is that we shouldn't show a min/max validation error if the user didn't supply a value. That's more in the realm of required validation. (A field may have min/max constraints while at the same time being optional.) The problem is that 0 is a falsy value in JavaScript so we need to be careful about deciding when the user has supplied a value. That's why I'm checking to make sure there's a non-empty string (eg. '123') and that it's a legit number (eg '123' vs 'abc') and only then validating.

@indrimuska
Copy link
Collaborator Author

Yes, sure, I know a field could be optional and, at the same time, have a minimum constraint.
Isn't it enough to check if validationRules.minimum || validationRules.minimum === 0?

@bvaughn
Copy link
Owner

bvaughn commented Dec 11, 2015

Oh! Haha! I was completely misunderstanding your question. I thought you were saying that it didn't work when the user supplied value was 0. You're saying it doesn't work when the minimum value is 0!

Looks like we maybe need to do a similar, more robust form of checking for the min/max validation rules as we're doing for their values. (Not just rely on falsy checks, but more strictly check for undefined, null, or ''.)

Or yeah, I guess we could also just explicitly check for 0 like you suggest :)

@indrimuska
Copy link
Collaborator Author

Aha, don't worry. Well, we must remember that minimum rule could be a number, an object or a function.

So, don't re-invent the wheel, let's use angular utilities:

function checkValue(value) {
   if (angular.isObject(value) || angular.isFunction(value) || angular.isNumber(value))
      console.log('valid');
   else
      console.log('invalid');
}

I made some tests with this fn, and these are the results:

Input Output
0 "valid"
1 "valid"
function () { } "valid"
{ rule: 0 } "valid"
null "invalid"
undefined "invalid"
true "invalid"
false "invalid"
" " "invalid"

What are your thought?

@bvaughn
Copy link
Owner

bvaughn commented Dec 12, 2015

Just checking if it's not falsy or 0 (a number) would be sufficient though
because functions and objects are both truthy things in JavaScript :)

On Saturday, December 12, 2015, indrimuska notifications@github.com wrote:

Aha, don't worry. Well, we must remember that minimum rule could be a
number, an object or a function.

So, don't re-invent the wheel, let's use angular utilities:

function checkValue(value) {
if (angular.isObject(value) || angular.isFunction(value) || angular.isNumber(value))
console.log('valid');
else
console.log('invalid');
}

I made some tests with this fn, and these are the results:
Input Output 0 "valid" 1 "valid" function () { } "valid" { rule: 0 }
"valid" null "invalid" undefined "invalid" true "invalid" false "invalid" "
" "invalid"

What are your thought?


Reply to this email directly or view it on GitHub
#179 (comment)
.

@indrimuska
Copy link
Collaborator Author

Ok, but, as you said, this will accept inputs like " " or true and convert them to integer 1. This means that if you set minimum rule to true and your field is equal to 0 it will cause a validation error.
Anyway, I know that setting minimum rule to true is not a correct behavior, so if you like we can just use the same simplest rule I already described before.

@bvaughn
Copy link
Owner

bvaughn commented Dec 12, 2015

Hmm. I'm all for robust checking of user input but it seems unlikely to
me that someone would configure their validation rules that way. I don't
feel strongly about this though- either approach would be fine,

On Saturday, December 12, 2015, indrimuska notifications@github.com wrote:

Ok, but, as you said, this will accept inputs like " " or true and
convert them to integer 1. This means that if you set minimum validation
to true and your field is equal to 0 it will cause a validation error.
Anyway, I know that setting minimum rule to true is not a correct
behavior, so if you like we can just use the same simplest rule I already
described before
#179 (comment)
.


Reply to this email directly or view it on GitHub
#179 (comment)
.

@indrimuska
Copy link
Collaborator Author

Ok, I will create a PR with the simplest one. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants