Transforms
Transform Functions#
Transform functions are run during casting/validation in the order they exist in a schema definitions transform array.
It is important to note that transforms are never run when the initial value is undefined or null.
- ast
- functional
If the option strict:true is passed to cast/validation then no transforms will be run.
- ast
- functional
Some schemas include default transforms. The default transforms behave like any other transforms with the exception that they are always applied first - this means that if strict:true is passed as an option, the default transforms won't be run either.
string#
The default transform for a string schema is simply toString()
- ast
- functional
Some other useful transforms are available in @zuze/schema for string schemas:
uppercase#
Converts a string to uppercase
- ast
- functional
lowercase#
Converts a string to lowercase
- ast
- functional
trim#
trim({start = true,end = true})
Removes whitespace from beginning and end of a string. Can pass an object as an argument that refines this behavior:
- ast
- functional
strip#
Removes all whitespace from a string
- ast
- functional
number#
The default transform for a number schema attempts to coerce the value to a numeric type.
- ast
- functional
boolean#
- ast
- functional
date#
The default transform for date is to use parseISO to convert the value being cast/validated to a Date object.
This can be changed by providing a different parser as the first argument to a date schema or by providing the dateParser option to createSchema(s)/matches when creating schemas via the AST api. SugarDate allows you to do some pretty cool things
- ast
- functional
array#
The default transform for an array is to use JSON.parse, if applicable.
unique#
unique(by?: ((a:any,b:any) => boolean) | string)
Unique checks by equality, but it also accepts a comparator function OR a string (interpreted as a path used by property-expr)
- ast
- functional
compact#
compact(rej?: value => boolean)
Compact removes all false-y values from an array. Accepts an optional function parameter to choose whether to reject a value
- ast
- functional
object#
Like array, the default transform for an object schema is to use JSON.parse, if applicable.
- ast
- functional
object schemas also support some further useful transforms you may use:
entries#
entries((key:string,value:any) => Object | undefined)
entries can be used to transform entries of an object, it accepts a function that gets called with a key and value and returns an object or undefined.
stripWhere#
stripWhere((key: string, value: any) => boolean)
stripWhere is very closely related to entries except it's callback function returns a boolean.
- ast
- functional
allowWhere#
allowWhere((key: string, value: any) => boolean)
Inverse of stripWhere
- ast
- functional
stripKeys#
stripKeys(...string[])
Same as stripWhere except the arguments are the keys to blacklist.
allowKeys#
allowKeys(...string[])
Same as allowWhere except the arguments are the keys to blacklist.
stripUnknown#
stripUnknown()
Removes all keys not part of the inner schema (shape):
- ast
- functional
from#
from(frm: string, to: string, alias = false)
Converts a key to another key. If alias is true, then the original key will be retained.
- ast
- functional