Type boolean is not supported
Closed this issue · 11 comments
How to validate boolean type?
{
type: 'boolean',
required: true,
message: 'Some message',
match: '^(true|false)$'
}
Using this gives an error message saying validation failed
even if I remove the match it still gives me the error. But if I change the type to string or number and pass the appropriate values It works fine.
Don't know why it is failing for boolean. Is there something I am doing wrong?
What version are you using? Can you post an example? I can't deduce from this what's going on.
I am using 3.0.1
Here's an example:
import Schema from 'validate'
const user = new Schema({
username: {
type: 'boolean',
required: true,
message: 'some message'
},
})
var obj = {
username: true
}
user.validate(obj)
Your example works just fine
https://runkit.com/eivindfjeldstad/5ae30f569347790011ef78a9
Are you passing in a request body? In that case you're probably passing in a string, not a boolean, which would explain why the validation fails. You can enable typecasting by doing something like:
const user = new Schema({
username: {
type: 'boolean',
required: true,
message: 'some message'
}
}, { typecast: true })
Also, version 3.0.1 is quite old. I recommend updating to the latest version.
Yup now the same thing is working perfectly fine..
Hi, found the issue.. For true it is working as intended, for false it is failing
https://runkit.com/embed/2ytdqg6p8imj
const Schema = require('validate@3.0.1')
const user = new Schema({
username: {
type: 'boolean',
required: true,
message: 'some message'
}
})
var obj = {
username: false
}
const errors = user.validate(obj)
console.log(errors)
Ah, yes, that is an old bug. You should update to the latest version. It was fixed a while back.
Hi when I updated to the latest version, I am getting this error:
[TypeError: Cannot call a class as a function]
That has nothing to do with this lib, though. You have probably forgotten a new
somewhere. The stack trace should tell you where
Yeah it is working fine now. I had one missing new which I guess is required with the latest version.
And had to add polyfill for Array.includes and Object.entries