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)
- \" bypasses email validation HOT 2
- Simple Install for Contributors HOT 1
- isCurrency doesn't work correctly with toLocaleString HOT 2
- We should make major releases more often HOT 2
- [Feature Request] Implementation of the isCpf Function in the Validator.js Library HOT 3
- [Feature Request] Implementation of the isPis Function in the Validator.js Library
- Add new functionality for RG format validation regex
- regex for country code PT HOT 2
- isMobilePhone with locale es-CU matches numbers of any length > 8 when the number starts with 5
- [Feature Request] Add method to check if a text belongs to a given locale
- How do I make emails with `.@` be invalid HOT 3
- Ports starting with multiple zeros pass the verification HOT 5
- Валидатор HOT 1
- File of different languages HOT 1
- isURL returns false when testing urls with localhost domain HOT 3
- Add ISO-15924 validation HOT 2
- isJWT function returning true for invalid JWT tokens with 2 parts HOT 1
- Non breaking space bypass email validation HOT 1
- some ISO are not exist in typescript but exist in javascript HOT 2
- Add isColor to validate CSS compatible color formats HOT 1
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.