Transforms
#
Transform FunctionsTransform 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.
#
stringThe default transform for a string
schema is simply toString()
- ast
- functional
Some other useful transforms are available in @zuze/schema for string
schemas:
#
uppercaseConverts a string to uppercase
- ast
- functional
#
lowercaseConverts a string to lowercase
- ast
- functional
#
trimtrim({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
#
stripRemoves all whitespace from a string
- ast
- functional
#
numberThe default transform for a number schema attempts to coerce the value to a numeric type.
- ast
- functional
#
boolean- ast
- functional
#
dateThe 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
#
arrayThe default transform for an array
is to use JSON.parse
, if applicable.
#
uniqueunique(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
#
compactcompact(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
#
objectLike 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:
#
entriesentries((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.
#
stripWherestripWhere((key: string, value: any) => boolean)
stripWhere
is very closely related to entries except it's callback function returns a boolean.
- ast
- functional
#
allowWhereallowWhere((key: string, value: any) => boolean)
Inverse of stripWhere
- ast
- functional
#
stripKeysstripKeys(...string[])
Same as stripWhere except the arguments are the keys to blacklist.
#
allowKeysallowKeys(...string[])
Same as allowWhere except the arguments are the keys to blacklist.
#
stripUnknownstripUnknown()
Removes all keys not part of the inner schema (shape
):
- ast
- functional
#
fromfrom(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