Getting Started
Install @zap-studio/validation and validate your first payload in a few lines.
This page walks you through installing @zap-studio/validation and validating your first payload. For deeper details, check the other pages in this section.
Install
npm install @zap-studio/validationyarn add @zap-studio/validationpnpm add @zap-studio/validationbun add @zap-studio/validationdeno add jsr:@zap-studio/validationDefine a Schema
The helpers accept any Standard Schema-compatible schema. This example uses Zod, but Valibot, ArkType, or any other compatible library works the same way.
import { z } from "zod";
const userSchema = z.object({
name: z.string(),
age: z.number(),
});
Validate Data
Use standardValidate to validate a value against the schema. By default it does not throw — it returns the raw Standard Schema result, which has either a value or an issues property.
import { standardValidate } from "@zap-studio/validation";
const result = await standardValidate(input, userSchema);
if (result.issues) {
console.error("Validation failed", result.issues);
} else {
console.log("Validation passed", result.value);
}
Throw on Failure
Pass throwOnError: true to get the parsed value directly. On failure, the helper throws a ValidationError instead of returning issues.
import { standardValidate, ValidationError } from "@zap-studio/validation";
try {
const user = await standardValidate(input, userSchema, {
throwOnError: true,
});
console.log(user.name);
} catch (error) {
if (error instanceof ValidationError) {
console.error("Validation failed", error.issues);
}
}