Spec-Zone.ru › Valibot

stringifyJson

Creates a JSON stringify transformation action.

const Action = v.stringifyJson<TInput, TConfig, TMessage>(config, message);

Generics

  • TInput extends any
  • TConfig extends StringifyJsonConfig | undefined
  • TMessage extends ErrorMessage<StringifyJsonIssue<TInput>> | undefined

Parameters

  • config TConfig
  • message TMessage

Explanation

With stringifyJson you can stringify a JSON object. If the input is unable to be stringified, you can use message to customize the error message.

Returns

  • Action StringifyJsonAction<TInput, TConfig, TMessage>

Examples

The following examples show how stringifyJson can be used.

Stringify JSON

Stringify a JSON object.

const StringifiedObjectSchema = v.pipe(
  v.object({ key: v.string() }),
  v.stringifyJson()
);

Stringify JSON with replacer

Stringify a JSON object with a replacer function.

const StringifiedObjectSchema = v.pipe(
  v.object({ key: v.string() }),
  v.stringifyJson({
    replacer: (key, value) =>
      typeof value === 'string' ? value.toUpperCase() : value,
  })
);

Related

The following APIs can be combined with stringifyJson.

Schemas

any, array, boolean, custom, date, enum, exactOptional, instance, intersect, lazy, literal, looseObject, looseTuple, nonNullable, nonNullish, nonOptional, null, nullable, nullish, number, object, objectWithRest, picklist, record, strictObject, strictTuple, string, tuple, tupleWithRest, union, unknown, variant

Methods

pipe

Utils

isOfKind, isOfType

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

Spec-Zone.ru

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