Spec-Zone.ru › Valibot

parseJson

Creates a JSON parse transformation action.

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

Generics

  • TInput extends string
  • TConfig extends ParseJsonConfig | undefined
  • TMessage extends ErrorMessage<ParseJsonIssue<TInput>> | undefined

Parameters

  • config TConfig
  • message TMessage

Explanation

With parseJson you can parse a JSON string. If the input is not valid JSON, you can use message to customize the error message.

Returns

  • Action ParseJsonAction<TInput, TConfig, TMessage>

Examples

The following examples show how parseJson can be used.

Parse and validate JSON

Parse a JSON string and validate the result.

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

Parse JSON with reviver

Parse a JSON string with a reviver function.

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

Related

The following APIs can be combined with parseJson.

Schemas

any, custom, string

Methods

pipe

Utils

isOfKind, isOfType

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

Spec-Zone.ru

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