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

isBoolean? #52

Closed
ghost opened this issue Dec 12, 2011 · 10 comments
Closed

isBoolean? #52

ghost opened this issue Dec 12, 2011 · 10 comments

Comments

@ghost
Copy link

ghost commented Dec 12, 2011

Would isBoolean() be a good validator?

Some fields only accepts true or false (checkboxes).

@chriso
Copy link
Collaborator

chriso commented Dec 13, 2011

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'

@awvalenti
Copy link

A question: why does toBoolean() accept stuff other than 'true' and 'false' and why so does toBooleanStrict()?

@chriso
Copy link
Collaborator

chriso commented Jan 5, 2012

It mimics the way JavaScript coerces strings to a boolean

@awvalenti
Copy link

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.

@chriso
Copy link
Collaborator

chriso commented Jan 21, 2012

The Boolean constructor obviously has different semantics. I mean the way non-empty strings are considered truth-y:

console.log("123" ? true : false); //true
//or just
console.log(!!"123"); //true

I designed toBoolean() to do the same except in the case of "0" or "false" which will often be passed as request parameters.

@awvalenti
Copy link

I'm still unable to see the mimics there. If I got it right:

JavaScript:
str.length == 0 ? false : true

toBoolean():
str.length == 0 || str == 'false' || str == '0' ? false : true

toBooleanStrict():
str == '1' || str == 'true' ? true : false

@chriso
Copy link
Collaborator

chriso commented Jan 22, 2012

Correct. "1" or "true" is considered true, "0" or "false" is considered false, and then your choice of toBoolean() or toBooleanStrict() controls how other strings are coerced.

@chriso chriso closed this as completed Jan 22, 2012
@sebpiq
Copy link

sebpiq commented Jan 22, 2013

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 ?

@chriso
Copy link
Collaborator

chriso commented Jan 23, 2013

@sebpiq it's a string validation/sanitization library. Checking for a boolean primitive is simple however, e.g. to check for true only => value && typeof(value) === 'boolean'

@sebpiq
Copy link

sebpiq commented Jan 23, 2013

Oh right ... sorry, I guess I was mixed up by the isArray, isNumber and so on ... I was actually looking for an all-purpose validation library, but I guess I've found what I needed now.

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

3 participants