Creates a schema that validates the value is exactly null.
null
By default the schema is required — only null is accepted. Call .optional() to also allow undefined.
.optional()
undefined
import { nul } from '@cleverbrush/schema';nul().validate(null); // { valid: true, object: null }nul().validate(undefined); // { valid: false }nul().validate(0); // { valid: false } Copy
import { nul } from '@cleverbrush/schema';nul().validate(null); // { valid: true, object: null }nul().validate(undefined); // { valid: false }nul().validate(0); // { valid: false }
nul().optional().validate(null); // { valid: true, object: null }nul().optional().validate(undefined); // { valid: true, object: undefined }nul().optional().validate(false); // { valid: false } Copy
nul().optional().validate(null); // { valid: true, object: null }nul().optional().validate(undefined); // { valid: true, object: undefined }nul().optional().validate(false); // { valid: false }
// Nullable field in an object schemaimport { object, string, nul, union, InferType } from '@cleverbrush/schema';const Schema = object({ name: string(), deleted: union(nul()).or(string()), // null | string});type T = InferType<typeof Schema>;// { name: string; deleted: null | string } Copy
// Nullable field in an object schemaimport { object, string, nul, union, InferType } from '@cleverbrush/schema';const Schema = object({ name: string(), deleted: union(nul()).or(string()), // null | string});type T = InferType<typeof Schema>;// { name: string; deleted: null | string }
Creates a schema that validates the value is exactly
null.By default the schema is required — only
nullis accepted. Call.optional()to also allowundefined.