-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
isBoolean? #52
Comments
True, but you can assign a custom value to checkbox for when it's checked. I've always provided the following filters: toBoolean() //True unless str = '0', 'false', or str.length == 0
toBooleanStrict() //False unless str = '1' or 'true' |
A question: why does toBoolean() accept stuff other than 'true' and 'false' and why so does toBooleanStrict()? |
It mimics the way JavaScript coerces strings to a boolean |
You said JavaScript coerces strings to boolean. By which means? I checked the construct Boolean(myString) and if (myString) , but unfortunately they evaluate the string 'false' to true... Really odd! The second case is understandable, but the first is definetely not. |
The console.log("123" ? true : false); //true
//or just
console.log(!!"123"); //true I designed |
I'm still unable to see the mimics there. If I got it right: JavaScript: toBoolean(): toBooleanStrict(): |
Correct. "1" or "true" is considered true, "0" or "false" is considered false, and then your choice of |
What about if I'm doing server-side validation (I mean that the data hasn't been submitted through a form, therefore no checkbox), and I actually require the value to be a boolean ? |
@sebpiq it's a string validation/sanitization library. Checking for a boolean primitive is simple however, e.g. to check for |
Oh right ... sorry, I guess I was mixed up by the |
Would isBoolean() be a good validator?
Some fields only accepts true or false (checkboxes).
The text was updated successfully, but these errors were encountered: