OptionalSchemaclass
new OptionalSchema<T>(options: OptionalSchemaOptions<T>)
| Param | Type | |
|---|---|---|
options | OptionalSchemaOptions<T> | Allowed options for OptionalSchema. required |
.value | undefined | Default value is always undefined (the default is only used when the input is undefined, so otherwise undefined could never be returned). readonly |
| Return | |
|---|---|
OptionalSchema<T> | Schema that wraps a source schema and additionally allows undefined. |
Schema that wraps a source schema and additionally allows undefined.
undefinedinput validates toundefinedinstead of being passed to the source schema.- When used with
validateData()this means the prop can be silently skipped. - Any other value is delegated to the source schema for validation.
Wraps another schema to allow undefined as a valid value. Any value other than undefined is delegated to the wrapped schema. It is mainly used for partial data props.
OPTIONAL(schema) is the sugar factory that builds an OptionalSchema around an existing schema.
Usage
import { OPTIONAL, NUMBER } from "shelving/schema";
const OPTIONAL_NUMBER = OPTIONAL(NUMBER);
OPTIONAL_NUMBER.validate(undefined); // undefined
OPTIONAL_NUMBER.validate(42); // 42Examples
const schema = new OptionalSchema({ source: STRING });
schema.validate(undefined); // Returns undefined
schema.validate("abc"); // Returns "abc"