transform
Creates a custom transformation action.
const Action = v.transform<TInput, TOutput>(action);
Generics
TInputextends anyTOutputextends any
Parameters
action(input: TInput) => TOutput
Explanation
transform can be used to freely transform the input. The action parameter is a function that takes the input and returns the transformed output.
Returns
ActionTransformAction<TInput, TOutput>
Examples
The following examples show how transform can be used.
Transform to length
Schema that transforms a string to its length.
const StringLengthSchema = v.pipe(
v.string(),
v.transform((input) => input.length)
);
Add object entry
Schema that transforms an object to add an entry.
const UserSchema = v.pipe(
v.object({ name: v.string(), age: v.number() }),
v.transform((input) => ({
...input,
created: new Date().toISOString(),
}))
);
Related
The following APIs can be combined with transform.
Schemas
any, array, bigint, blob, boolean, custom, date, enum, exactOptional, file, function, instance, intersect, lazy, literal, looseObject, looseTuple, map, nan, never, nonNullable, nonNullish, nonOptional, null, nullable, nullish, number, object, objectWithRest, optional, picklist, promise, record, set, strictObject, strictTuple, string, symbol, tuple, tupleWithRest, undefined, undefinedable, union, unknown, variant, void
Methods
Utils
© Fabian Hiller
Licensed under the MIT License.
https://valibot.dev/api/transform/