Minimum/maximum rule doesn't work when number is zero
Closed this issue · 9 comments
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; }
}
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)
?
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.
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
?
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 :)
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?
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)
.
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.
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)
.
Ok, I will create a PR with the simplest one. :)