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

Contributors

Thanks to all the contributors who helped make this page better!

  • GitHub profile picture of HJGreen
  • GitHub profile picture of fabian-hiller

Partners

Thanks to our partners who support the project ideally and financially.

Sponsors

Thanks to our GitHub sponsors who support the project financially.

  • GitHub profile picture of dailydotdev
  • GitHub profile picture of ivan-mihalic
  • GitHub profile picture of KATT
  • GitHub profile picture of osdiab
  • GitHub profile picture of Thanaen
  • GitHub profile picture of ruiaraujo012
  • GitHub profile picture of hyunbinseo
  • GitHub profile picture of caegdeveloper