-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
Incorrect validation of numbers on vulcan.validation.numerics #2
Comments
Hi @gtklocker. Actually I was mistaken. This is correct. Arrays are relational maps like in PHP in JavaScript. In your test case the zero index is implied and its value is 42. Therefore the test case is not wrong. It is actually correct! |
Many thanks for your reply. If so, shouldn't the following two cases also be fixed accordingly?
|
Hi Kostis, definitely not!
The JSon objects and BSONs are not considered arrays in any way. They are
generalized abstract objects. Please read ECMA for more details about the
previous array "strange" definition and interpretation.
Thanks a lot for the comments again!
…On Dec 4, 2017 21:02, "Kostis Karantias" ***@***.***> wrote:
Many thanks for your reply. If so, shouldn't the following two cases also
be fixed accordingly?
var my_vulcan = new vulcan;
var obj = {0: 42};
console.log(my_vulcan.validation.numerics.is_number(obj));
// false
var map = new Map;
// Map(0) {}
map.set(0, 42);
// Map(1) {0 => 42}
console.log(my_vulcan.validation.numerics.is_number(map));
// false
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#2 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAaX6PH6KsAyqpZfIwJ8r21GqNd1x5wOks5s9EG-gaJpZM4Qz4HR>
.
|
@g0d makes sense. There is a tiny detail that the zero index is implied regardless of length. As per the ECMA specification even if you have the array What follows is that |
I agree but my problem and everyone's problem in this case, I guess, is that the interpreter is too weak due to those poorly defined specs. Since there are so many variables that are not "well defined" (with the mathematical point of view) then I may fail covering those edge cases. To be more precise, I am a bit reluctant to go forward with this because I am afraid to over do it. You know, if I over use code to do a simple validation then I am actually becoming a weak and slow tool/lib as well. I don't know if you can see my point here. I think it's more wise to leave that out until the most recent ECMA specification gets updated and finds its way into the core of the new JS parsers! |
I understand your concern about performance but I think you can keep vulcan strong and fast using the following approach: if (self.validation.misc.is_array(val))
return !isNaN(val[0]); |
@petrosagg @g0d Do you believe the zero index should be implied on Here follows a standalone example which accomplishes this and doesn't sacrifice correctness or speed: const is_number = x => typeof(x) === 'number' && !isNaN(x); |
@gtklocker your function is written in versions of ecmascript not supported by older browsers. But besides that, it doesn't work for Number instances, eg it's much better to use var is_number = function(x) {
return (typeof(x) === "number" || x instanceof Number) && !isNaN(x);
} |
Thank you all gents,
I truly appreciate your involvement in this. I am not sure yet what is the best course of action. I will have to run several tests and then decide which works best under all exotic scenarios 😁
If you have any ideas I am more than happy to read about them. I will reopen the issue and let's see what we can achieve.
Talk later....
…On Dec 7, 2017 18:07, "Aleksis Brezas" ***@***.***> wrote:
@gtklocker <https://github.com/gtklocker> your function is written in
versions of ecmascript not supported by older browsers.
But besides that, it doesn't work for Number instances, eg new Number(2)
it's much better to use
var is_number = function(x) {
return (typeof(x) === "number" || x instanceof Number) && !isNaN(x)
}
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#2 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAaX6A9eI1HSrElxR58USPqDVyWdx0KTks5s-A1cgaJpZM4Qz4HR>
.
|
Btw, regarding this:
Why do you think "instanceof" is needed in the first place? Isn't "typeof" really enough to make sure a number is an actual... well, number :) |
Well, interestingly the type of |
Hi @abresas, I know that. I meant why do you need the 'x instanceof Number' comparison to evaluate the expression. Isn't typeof(x) === "number" and !isNaN(x) enough for that? |
Fixed. Thanks. |
The library incorrectly thinks that a single-element array is a number
The text was updated successfully, but these errors were encountered: