shelving
Version:
Toolkit for using data in JavaScript.
22 lines (21 loc) • 899 B
JavaScript
import { DataSchema } from "./DataSchema.js";
import { KEY } from "./KeySchema.js";
import { OPTIONAL } from "./OptionalSchema.js";
/** Validate an item object. */
export class ItemSchema extends DataSchema {
constructor({ id = KEY, props, ...options }) {
super({ props: { id, ...props }, ...options });
}
}
/**
* Valid item object with specifed properties.
* - An `Item` is a `Data` object with a string `.id` prop.
* - Optional validator for `.id` must be a `KeySchema` and defaults to `KEY`.
*/
export const ITEM = (props, id) => props instanceof DataSchema ? new ItemSchema({ id, ...props }) : new ItemSchema({ props, id });
/**
* Valid item object or `null`.
* - An `Item` is a `Data` object with a string `.id` prop.
* - Optional validator for `.id` must be a `KeySchema` and defaults to `KEY`.
*/
export const OPTIONAL_ITEM = (props, id) => OPTIONAL(ITEM(props, id));