UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

14 lines (13 loc) 586 B
import { ValueFeedback } from "../feedback/Feedback.js"; import { ThroughSchema } from "./ThroughSchema.js"; /** Validate a value of a specifed type, but throw `Feedback` if the validated value is falsy. */ export class RequiredSchema extends ThroughSchema { validate(unsafeValue) { const safeValue = super.validate(unsafeValue); if (!safeValue) throw new ValueFeedback("Required", safeValue); return safeValue; } } /** Create a new required schema from a source schema. */ export const REQUIRED = (source) => new RequiredSchema({ source });