rawTransform

Creates a raw transformation action.

const Action = v.rawTransform<TInput, TOutput>(action);

Generics

  • TInput extends any
  • TOutput extends any

Parameters

Explanation

With rawTransform you can freely transform and validate the input with a custom action and add issues if necessary.

Returns

Examples

The following examples show how rawTransform can be used.

Calculate game result

Schema that calculates the total score of a game based on the scores and a multiplier.

This rawTransform validation action adds an issue for points that exceed a certain maximum and forwards it via path to the appropriate nested score.

const GameResultSchema = v.pipe(
  v.object({
    scores: v.array(v.pipe(v.number(), v.integer())),
    multiplier: v.number(),
  }),
  v.rawTransform(({ dataset, addIssue, NEVER }) => {
    // Create total variable
    let total = 0;

    // Iterate over scores and check points
    for (let index = 0; index < dataset.value.scores.length; index++) {
      // Calculate points by multiplying score with multiplier
      const score = dataset.value.scores[index];
      const points = score * dataset.value.multiplier;

      // Add issue if points exceed maximum of 1,000 points
      if (points > 1_000) {
        addIssue({
          message:
            'The score exceeds the maximum allowed value of 1,000 points.',
          path: [
            {
              type: 'object',
              origin: 'value',
              input: dataset.value,
              key: 'scores',
              value: dataset.value.scores,
            },
            {
              type: 'array',
              origin: 'value',
              input: dataset.value.scores,
              key: index,
              value: score,
            },
          ],
        });

        // Abort transformation
        return NEVER;
      }

      // Add points to total
      total += points;
    }

    // Add calculated total to dataset
    return { ...dataset.value, total };
  })
);

The following APIs can be combined with rawTransform.

Schemas

Methods

Contributors

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

  • 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 Thanaen
  • GitHub profile picture of KATT
  • GitHub profile picture of osdiab
  • GitHub profile picture of ruiaraujo012
  • GitHub profile picture of hyunbinseo
  • GitHub profile picture of F0rce
  • GitHub profile picture of caegdeveloper
  • GitHub profile picture of seahindeniz