Utilities
Utility methods are methods designed to functionally manipulate a schema definition.
AST examples are provided, where applicable, for the sake of completeness.
#
nullablenullable(isNullable: boolean = true)
Indicates whether null
is a valid value for this schema.
- ast
- functional
Note: nullable() === nullable(true)
#
typeErrortypeError(string | ({value,label}) => string)
Sets the error message for the typeError. value
and label
are available as interpolation parameters.
#
defdef(value | () => value)
Sets the default value of a schema when the provided value is undefined
.
- ast
- functional
#
labellabel(string | () => string)
Sets the label on a schema - used for interpolation in error messages.
- ast
- functional
#
metaSets the meta property on a schema
- ast
- functional
#
teststests(...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
#
transformstransforms(...TransformFn[])
Adds transforms to a schema
- ast
- functional
#
conditionsconditions(...Condition[])
Adds conditions to a schema
- ast
- functional
#
when/conditionwhen
is an alias of the condition
- ast
- functional
#
refCreate a reference to another field or context
- ast
- functional
The following two utility method are used to manipulate a schema definition
#
withoutwithout(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.
#
ofof(schema: Schema)
Defines the inner schema of an array schema.
- ast
- functional
#
shapeshape({[key: string]: Schema})
Defines the inner schema of an object schema.
- ast
- functional
#
withoutAnywithoutAny(property: string, schema: Schema): Schema
Removes all transforms/conditions/tests from a schema
#
warningswarnings(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)
.