Information of how to use zod-extras
package.
The zod framework supports preprocessors which are functions that can modify input data before it's send to validation.
A common usecase for preprocessors is type coercion - this package includes a few helpers for that, you can find documentation for those here.
Another common usecase is turning a string like "Alice,Bob,Charlie"
into ['Alice', 'Bob', 'Charlie']
, this can be done using the stringSplitFactory
function which returns a function that can be used as a preprocessor.
Arguments
Name | Description | Type | Default |
---|---|---|---|
delimiter | The pattern used as delimiter | string | , |
trim | If true values will be trimmed after splitting |
boolean | false |
import { z } from 'zod'
import stringSplitFactory from 'src/stringSplitFactory'
const preprocessor = stringSplitFactory({ delimiter: '|' })
const schema = z.preprocess(preprocessor, z.array(z.string()))
const result = schema.parse('Alice|Bob|Charlie')
expect(result).toEqual(['Alice', 'Bob', 'Charlie'])
The numberSplitFactory
works similar to stringSplitFactory
except it will also cast values to numbers after splitting.
Arguments
Name | Description | Type | Default |
---|---|---|---|
delimiter | The pattern used as delimiter | string | , |
import { z } from 'zod'
import numberSplitFactory from 'src/numberSplitFactory'
const preprocessor = numberSplitFactory({ delimiter: '|' })
const schema = z.preprocess(preprocessor, z.array(z.number()))
const result = schema.parse('1|2|3')
expect(result).toEqual([1, 2, 3])