Schemas
@zuze/schema makes the following schemas available for you to use:
Unlike yup, joi, and ajv, @zuze/schema was designed to be functional and that means that it doesn't use methods. All validators can be added to all schemas regardless of if they make sense (but we'll warn you if you're doing something silly) via the tests
function.
Because of this, our documentation of each schema focuses primarily on the default transforms that come with each schema because, again, schemas don't have methods.
#
SchemasAll schema constructor functions (except lazy) have the same signature:
object, array and date may also accept different parameters as their first parameter.
#
mixedA mixed schema is an abstract schema, it's use is generally not encouraged except maybe for the base of a conditional schema.
Partial definitions can be passed to it as arguments just like any other:
NOTE: mixed
schemas DO NOT perform any default transform on its value at validation/cast time.
- ast
- functional
#
stringAccepts values that are strings and attempts to cast the subject value to a string at validation/cast time.
- ast
- functional
#
numberA number schema attempts to coerce the subject value to a numeric value, if it isn't one.
- ast
- functional
#
booleanA boolean schema attempts to coerce the subject value to a boolean (starting to notice a pattern?)
- ast
- functional
#
dateA date schema is the first deviation from the regular pattern. Instead of only accepting partial schema definitions, it can accept a function (only as it's first parameter). If a function is given, that function will be used as a mechanism to parse the subject value to convert it to a Date object. If no function is given it will use date-fns parseISO (optional dependency, don't forget to install it!).
Providing your own parser allows you to tree-shake date-fns if you don't need it, or it also allows you to do some really cool relative date parsing using other libraries.
- ast
- functional
#
lazyA lazy schema has no AST-equivalent because it doesn't need one. It's for developers who like to write functional code.
It accepts only a single parameter - a function that is called with the subject value that must return a schema.
#
objectobject
and array
are special schemas in that they have inner's. They can have their own transforms/validations and they can also transform/validate their subschemas. In the case of an object this is done via it's shape
- ast
- functional
#
arrayAn array schema's subschema is given to it via of
Let's validate the following structure
- ast
- functional
#
tupleA tuple
is an fixed length array of values. Instead of having a tuple
schema, we just use array
but instead of passing of
a single schema, we pass it an array of schemas:
Let's look at a similar structure to above:
- ast
- functional
#
FunctionsAll schema operating functions accept the following arguments:
- schema: SchemaDefinition
- value: any;
- options?: ValidationOptions
#
castcast(schema: SchemaDefinition, value: any, options?: ValidationOptions): any
Casts a value using a schema's transforms. Transforms are not run when the ValidationOption strict
is true
.
#
validatevalidate(schema: SchemaDefinition, value: any, options: ValidationOptions): any
When sync is false, returns a Promise that resolves to the passed in value or rejects with a ValidationError.
#
validateSyncAlias for validate with { sync: true }
option. Returns the passed in value or throws a ValidationError
#
validateAtvalidateAt(path: string, schema: SchemaDefinition, value: object | any[], options: ValidationOptions): any
Note: The value
passed in must be the value described by the entire schema definition, not the value at path
.
#
validateAtSyncAlias for validateAt with { sync: true }
option.
#
isValidisValid(schema: SchemaDefinition, value: any, options: ValidationOptions): Promise<boolean>
isValid
and isValidSync
do not throw errors. These methods always return booleans or Promises that resolve to a boolean.
#
isValidSyncAlias for isValid with { sync: true }
option.
#
isValidAtisValid(path: string, schema: SchemaDefinition, value: any, options: ValidationOptions): boolean | Promise<boolean>
Same as validateAt except this method always returns a boolean or a Promise that resolves to a boolean.
#
isValidSyncAtAlias for isValidAt with { sync: true }
option.
#
getErrorsgetErrors(schema: SchemaDefinition, value: any, options?: ValidationOptions)
getErrors
is similar to validate
except that it returns (instead of throws) error messages.
If accepts an option of multi: true
to return error messages as an array if abortEarly
is false.
In the case of on object schema it maps errors to an object where the paths are the keys:
#
getErrorsSyncAlias for getErrors with { sync: true }
option.