Skip to main content

nullable

function

Combinator that makes any SerializationSchema nullable. The returned schema yields `null` when the value is `null` or `undefined` (so `null` is its recoverable default) and otherwise delegates to `inner`. This guarantees a `T | null` result for an untrusted value, unlike `value || null`, which can pass a non-`T` (or falsy) value straight through with the wrong type. Pass `{defaultAsNull: true}` when an in-band value equal to `inner`'s default also means "no value" — the historical `serializedNode.rel || null` idiom, where an empty string is not a real `rel`. Equality is `inner`'s own (see SerializationSchema.isEqual), so a reference-typed default is compared by content: `nullable(arrayValue(...), {defaultAsNull: true})` reads an explicitly empty array as `null`.

function nullable<T, In = T>(inner: SerializationSchema<T, never, In>, options?: { readonly defaultAsNull?: boolean; }): SerializationSchema<T | null, never, In | null | undefined>

Import

import { nullable } from '@modular/sdk/editor';

Notes

example
```ts const parseRel = nullable(stringValue(), {defaultAsNull: true}); // ^? SerializationSchema<string | null> parseRel('noopener'); // 'noopener' parseRel(''); // null ('' is stringValue's default) parseRel(null); // null parseRel(undefined); // null (the recoverable default) ```
__NO_SIDE_EFFECTS__