UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

23 lines (22 loc) 920 B
import { ValueFeedback } from "../feedback/Feedback.js"; import { isData } from "../util/data.js"; import { validateData } from "../util/validate.js"; import { OPTIONAL } from "./OptionalSchema.js"; import { Schema } from "./Schema.js"; /** Validate a data object. */ export class DataSchema extends Schema { props; constructor({ props, title = "Data", value = {}, ...options }) { super({ title, value, ...options }); this.props = props; } validate(unsafeValue = this.value) { if (!isData(unsafeValue)) throw new ValueFeedback("Must be object", unsafeValue); return validateData(unsafeValue, this.props); } } /** Valid data object with specifed properties. */ export const DATA = (props) => new DataSchema({ props }); /** Valid data object with specifed properties, or `null` */ export const OPTIONAL_DATA = (props) => OPTIONAL(new DataSchema({ props }));