shelving
Version:
Toolkit for using data in JavaScript.
20 lines (19 loc) • 1.12 kB
TypeScript
import type { Schema } from "./Schema.js";
import { ThroughSchema, type ThroughSchemaOptions } from "./ThroughSchema.js";
export interface OptionalSchemaOptions<T> extends ThroughSchemaOptions<T | undefined> {
/** Default value for an `OptionalSchema` can always `undefined` */
readonly value?: undefined;
}
/**
* 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 declare class OptionalSchema<T> extends ThroughSchema<T | undefined> {
/** Default value for an `OptionalSchema` is always `undefined` (default value is only used when a value is `undefined`, so otherwise `undefined` could never be returned as a value). */
readonly value: undefined;
constructor(options: OptionalSchemaOptions<T>);
validate(unsafeValue: unknown): T | undefined;
}
/** Make a property of a set of data optional, i.e. it can be the value or `undefined` */
export declare const OPTIONAL: <T>(source: Schema<T>) => OptionalSchema<T>;