rawTransformAsync

Creates a raw transformation action.

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

Generics

  • TInput extends any
  • TOutput extends any

Parameters

Explanation

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

Returns

Examples

The following examples show how rawTransformAsync can be used.

Order schema

Schema that rejects an order that does not meet a requirement when free delivery is expected.

import { getTotalAmount } from '~/api';
import { FREE_DELIVERY_MIN_AMOUNT } from '~/constants';

const OrderSchema = v.pipeAsync(
  v.object({
    cart: v.array(
      v.object({
        itemId: v.pipe(v.string(), v.uuid()),
        quantity: v.pipe(v.number(), v.integer(), v.minValue(1)),
      })
    ),
    expectsFreeDelivery: v.optional(v.boolean(), false),
  }),
  v.rawTransformAsync(
    async ({ dataset: { value: input }, addIssue, NEVER }) => {
      const total = await getTotalAmount(input.cart);
      if (input.expectsFreeDelivery && total < FREE_DELIVERY_MIN_AMOUNT) {
        addIssue({
          label: 'order',
          expected: `>=${FREE_DELIVERY_MIN_AMOUNT}`,
          received: `${total}`,
          message: `The total amount must be at least $${FREE_DELIVERY_MIN_AMOUNT} for free delivery.`,
          path: [
            {
              type: 'object',
              origin: 'value',
              input,
              key: 'cart',
              value: input.cart,
            },
          ],
        });
        return NEVER;
      }
      return { ...input, total };
    }
  )
);

The following APIs can be combined with rawTransformAsync.

Schemas

Utils

Async

Contributors

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

  • GitHub profile picture of @fabian-hiller
  • GitHub profile picture of @santoshyadavdev
  • GitHub profile picture of @EltonLobo07

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 @antfu
  • GitHub profile picture of @Thanaen
  • GitHub profile picture of @osdiab
  • GitHub profile picture of @ruiaraujo012
  • GitHub profile picture of @hyunbinseo
  • GitHub profile picture of @F0rce
  • GitHub profile picture of @UniquePixels
  • GitHub profile picture of @jdgamble555
  • GitHub profile picture of @nickytonline
  • GitHub profile picture of @KubaJastrz
  • GitHub profile picture of @andrewmd5
  • GitHub profile picture of @caegdeveloper
  • GitHub profile picture of @dslatkin
  • GitHub profile picture of @BrianCurliss