parseJson
Creates a JSON parse transformation action.
const Action = v.parseJson<TInput, TConfig, TMessage>(config, message);
Generics
TInputextends stringTConfigextends ParseJsonConfig | undefinedTMessageextends ErrorMessage<ParseJsonIssue<TInput>> | undefined
Parameters
configTConfigmessageTMessage
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
ActionParseJsonAction<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
Methods
Utils
© Fabian Hiller
Licensed under the MIT License.
https://valibot.dev/api/parseJson/