shelving
Version:
Toolkit for using data in JavaScript.
13 lines (12 loc) • 501 B
JavaScript
import { ThroughSchema } from "./ThroughSchema.js";
/** Validate a value of a specifed type, but throw `"Required"` if the validated value is falsy. */
export class RequiredSchema extends ThroughSchema {
validate(unsafeValue) {
const safeValue = super.validate(unsafeValue);
if (!safeValue)
throw "Required";
return safeValue;
}
}
/** Create a new required schema from a source schema. */
export const REQUIRED = (source) => new RequiredSchema({ source });