import e from "validator";
await e.boolean(
{} // Optionally pass options
)
.validate(true) // returns true
// Alternatives
e.true();
e.false();
interface IBooleanValidatorOptions extends TBaseValidatorOptions {
/** Pass custom messages for the errors */
messages?: Partial<
Record<"typeError" | "notTrue" | "notFalse", TErrorMessage>
>;
/** Validate expected value to be true or false */
expected?: boolean;
}
Case 1 (Using validator options)
// Cast string to boolean
await e.boolean({ cast: true }).validate("1") // returns true
// Alternatively you can do this (Using a utility validator)
await e.cast(e.boolean()).validate("0") // returns false
await e.cast(e.boolean()).validate(1) // returns true
await e.cast(e.boolean()).validate("true") // returns true
Case 2 (Alternative methods)
// Validate True
await e.true({
messages: {
notTrue: "Value is not true!"
}
})
.validate(false) // throws ValidationException
// Validate False
await e.false({
messages: {
notFalse: "Value is not false!"
}
})
.validate(true) // throws ValidationException