Comments (10)
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'
from validator.js.
A question: why does toBoolean() accept stuff other than 'true' and 'false' and why so does toBooleanStrict()?
from validator.js.
It mimics the way JavaScript coerces strings to a boolean
from validator.js.
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.
from validator.js.
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.
from validator.js.
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
from validator.js.
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.
from validator.js.
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 ?
from validator.js.
@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'
from validator.js.
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.
from validator.js.
Related Issues (20)
- What is the exact ISO 8601 format for isDate() ? HOT 4
- Can't be used within remix routes HOT 7
- isLocale doesn't work anymore (returns true on wrong locals) HOT 4
- Add support uuid v7 for `isUUID` HOT 3
- isURL() returns false for long urls HOT 6
- The isDate function returns false for some timezones (e.g. North American time zones) HOT 2
- fix: Date is not validated as expected HOT 3
- Email validator accepts special character "ΒΈ" which should not be valid HOT 7
- Enhance assertion check for function "equals" HOT 2
- readme.md toBoolean function documentation typo HOT 4
- No Active Maintainers? New Maintainers Needed? How to Proceed? HOT 10
- `isISO8601` doesn't support duration HOT 3
- isEmail accepts &@<domain> HOT 3
- Inconsistent isSlug results with "." in the inputs HOT 2
- "Cannot find module" error for lib or es/lib modules HOT 1
- isDate returns false for all strings HOT 8
- extended ascii passed isEmail validation HOT 1
- isEmail does not reject emails when blacklisted_chars includes quote
- get list of supported countries for passports HOT 1
- Invalid URL get validated HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from validator.js.