transform

Adds a transformation step to a schema, which is executed at the end of parsing and can change the output type.

// Transform with an optional pipe
const Schema = transform<TSchema, TOutput>(schema, action, pipe);

// Transform with an optional validation
const Schema = transform<TSchema, TOutput>(schema, action, validation);

Generics

Parameters

Explanation

transform can be used to transform data after validation. Compared to the transformations you can perform inside pipelines, there are no rules here. So the input type can be completely different from the output type.

To transform the input at the very beginning of the process, before any validation is performed, use the coerce method.

The pipe and validation arguments can optionally be used to validate the output of the transformation.

Returns

Examples

The following examples show how transform can be used.

Transform to length

Schema that transforms a string to its length.

const StringLengthSchema = transform(string(), (input) => input.length);

Add object entry

Schema that transforms an object to add an entry.

const UserSchema = transform(
  object({ name: string(), age: number() }),
  (input) => ({
    ...input,
    created: new Date().toISOString(),
  })
);

The following APIs can be combined with transform.

Schemas

Methods