Spec-Zone.ru › Valibot

rawTransformAsync

Creates a raw transformation action.

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

Generics

  • TInput extends any
  • TOutput extends any

Parameters

  • action (context: Context<TInput>) => MaybePromise<TOutput>

Explanation

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

Returns

  • Action RawTransformActionAsync<TInput, TOutput>

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 };
    }
  )
);

Related

The following APIs can be combined with rawTransformAsync.

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

Utils

isOfKind, isOfType

Async

arrayAsync, customAsync, exactOptionalAsync, forwardAsync, intersectAsync, lazyAsync, looseObjectAsync, looseTupleAsync, mapAsync, nonNullableAsync, nonNullishAsync, nonOptionalAsync, nullableAsync, nullishAsync, objectAsync, objectWithRestAsync, optionalAsync, pipeAsync, recordAsync, setAsync, strictObjectAsync, strictTupleAsync, tupleAsync, tupleWithRestAsync, undefinedableAsync, unionAsync, variantAsync

© Fabian Hiller
Licensed under the MIT License.
https://valibot.dev/api/rawTransformAsync/

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API