Giter Site home page Giter Site logo

isBoolean? about validator.js HOT 10 CLOSED

validatorjs avatar validatorjs commented on May 7, 2024
isBoolean?

from validator.js.

Comments (10)

chriso avatar chriso commented on May 7, 2024

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.

awvalenti avatar awvalenti commented on May 7, 2024

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

from validator.js.

chriso avatar chriso commented on May 7, 2024

It mimics the way JavaScript coerces strings to a boolean

from validator.js.

awvalenti avatar awvalenti commented on May 7, 2024

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.

chriso avatar chriso commented on May 7, 2024

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.

awvalenti avatar awvalenti commented on May 7, 2024

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.

chriso avatar chriso commented on May 7, 2024

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.

sebpiq avatar sebpiq commented on May 7, 2024

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.

chriso avatar chriso commented on May 7, 2024

@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.

sebpiq avatar sebpiq commented on May 7, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.