shelving
Version:
Toolkit for using data in JavaScript.
19 lines (18 loc) • 730 B
JavaScript
import { ThroughSchema } from "./ThroughSchema.js";
/**
* Validate a property in an optional way, i.e. it can be the value, or `undefined`
* - If the prop is `undefined`, then `undefined` is returned.
* - When used with `validateData()` this means the prop can be silently skipped.
*/
export class OptionalSchema extends ThroughSchema {
constructor(options) {
super({ ...options, value: undefined });
}
validate(unsafeValue) {
if (unsafeValue === undefined)
return undefined;
return super.validate(unsafeValue);
}
}
/** Make a property of a set of data optional, i.e. it can be the value or `undefined` */
export const OPTIONAL = (source) => new OptionalSchema({ source });