Skip to content

validateWithStandardSchema

function validateWithStandardSchema<T>(schema, value): T;

Defined in: tanstack/core/standard-schema.ts:58

Run a Standard Schema synchronously, returning its output or throwing. Async validators are unsupported (matches nuqs parseAsJson).

Failed validation throws an Error whose message joins the issue messages with "; "; an async schema throws a TypeError. parseAsJson and parseAsStandardSchema use it and turn the throw into null. Handy in custom parsers (wrap it in try/catch and return null).

Type Parameter
T
Parameter Type
schema StandardSchemaV1<unknown, T>
value unknown

T

import { validateWithStandardSchema } from "@xtandard/lib/tanstack";
import * as v from "valibot";
const Email = v.pipe(v.string(), v.email("Invalid email"));
validateWithStandardSchema(Email, "[email protected]"); // → "[email protected]"
validateWithStandardSchema(Email, "nope"); // throws Error: Invalid email