shelving
Version:
Toolkit for using data in JavaScript.
18 lines (17 loc) • 645 B
JavaScript
import { Schema } from "./Schema.js";
const NEGATIVE = ["", "false", "0", "no", "n", "off"];
/** Define a valid boolean. */
export class BooleanSchema extends Schema {
constructor({ value = false, required = false, ...options }) {
super({ value, ...options });
this.required = required;
}
validate(unsafeValue = this.value) {
const value = typeof unsafeValue === "string" ? !NEGATIVE.includes(unsafeValue.toLowerCase().trim()) : !!unsafeValue;
if (this.required && !value)
throw "Required";
return value;
}
}
/** Valid boolean. */
export const BOOLEAN = new BooleanSchema({});