BooleanSchema.validate()method
Validate an unknown value and coerce it to a boolean.
validate(unsafeValue: unknown = this.value): boolean
new BooleanSchema({ value = false, required = false, ...options }: BooleanSchemaOptions)| Param | Type | |
|---|---|---|
options | BooleanSchemaOptions | Allowed options for BooleanSchema. required |
.value | boolean | Default boolean value used when the input is undefined. Defaults to false readonly |
.required | boolean | When true, a falsy result is rejected as invalid. Defaults to false readonly |
| Return | |
|---|---|
BooleanSchema | Schema that defines a valid boolean. |
Schema that defines a valid boolean.
"", "false", "0", "no", "n", "off") become false, everything else becomes true.Validates a value into a boolean. Following the robustness principle, the strings "no" and "false" coerce to false, and any other value is tested for truthiness. A missing value falls back to the schema's value default (false).
BOOLEAN is the ready-made sugar instance for a boolean field.
To save creating a new instance of BooleanSchema for trivial uses, you can use the BOOLEAN sugar instance instead.
import { BOOLEAN } from "shelving/schema";
BOOLEAN.validate(true); // true
BOOLEAN.validate("false"); // false (coerced)
BOOLEAN.validate("no"); // false (coerced)
BOOLEAN.validate(1); // true (truthiness)
BOOLEAN.validate(undefined); // false (default)const schema = new BooleanSchema({ required: true });
schema.validate("yes"); // Returns true
schema.validate(""); // Throws "Required"Validate an unknown value and coerce it to a boolean.
validate(unsafeValue: unknown = this.value): boolean