Utilities
Utility methods are methods designed to functionally manipulate a schema definition.
AST examples are provided, where applicable, for the sake of completeness.
nullable#
nullable(isNullable: boolean = true)
Indicates whether null is a valid value for this schema.
- ast
- functional
Note: nullable() === nullable(true)
typeError#
typeError(string | ({value,label}) => string)
Sets the error message for the typeError. value and label are available as interpolation parameters.
def#
def(value | () => value)
Sets the default value of a schema when the provided value is undefined.
- ast
- functional
label#
label(string | () => string)
Sets the label on a schema - used for interpolation in error messages.
- ast
- functional
meta#
Sets the meta property on a schema
- ast
- functional
tests#
tests(...ValidatorDefinition[])
Adds tests to a schema
- ast
- functional
test#
(name: string,TestFn: (value,{schema,options}) => boolean | ValidationError | Promise<boolean | ValidationError>)
Functional way to create a validator
transforms#
transforms(...TransformFn[])
Adds transforms to a schema
- ast
- functional
conditions#
conditions(...Condition[])
Adds conditions to a schema
- ast
- functional
when/condition#
when is an alias of the condition
- ast
- functional
ref#
Create a reference to another field or context
- ast
- functional
The following two utility method are used to manipulate a schema definition
without#
without(property: string, schema: Schema, ...refs: any[]): Schema
Removes a property from a schema definition. If the property is an array it removes any reference given by ...refs from the array.
of#
of(schema: Schema)
Defines the inner schema of an array schema.
- ast
- functional
shape#
shape({[key: string]: Schema})
Defines the inner schema of an object schema.
- ast
- functional
withoutAny#
withoutAny(property: string, schema: Schema): Schema
Removes all transforms/conditions/tests from a schema
warnings#
warnings(shouldWarn: boolean = false)
There are certain warnings that occur in @zuze/schema that you may want to suppress in a production environment. Warnings are enabled by default and can be turned off by calling warnings(false).