Standard Schema
How valrs implements the Standard Schema specification
Standard Schema Compliance
valrs implements the Standard Schema specification v1, enabling seamless interoperability with any tool or library that supports the standard.
What is Standard Schema?
Standard Schema is an open specification that defines a common interface for schema validation libraries. It solves a fundamental problem in the JavaScript ecosystem: validation libraries have historically been incompatible with each other, forcing framework authors to write adapters for each library they want to support.
With Standard Schema, any compliant library can work with any compliant tool. This means:
- Form libraries can validate using valrs, Zod, Valibot, or any other compliant library
- API frameworks can accept schemas from any compliant source
- Documentation tools can generate specs from any compliant schema
- You can switch validation libraries without rewriting your integration code
How valrs Implements Standard Schema
Every valrs schema exposes the Standard Schema interface through the ~standard property. This is an internal implementation detail that enables interoperability while keeping the public API clean.
The ~standard Interface
The ~standard property is a namespaced object (using the ~ prefix to avoid conflicts) that contains everything a Standard Schema consumer needs:
| Property | Type | Description |
|---|---|---|
version | 1 | The Standard Schema specification version |
vendor | string | The library identifier ('valrs') |
validate | function | Validates unknown input, returning sync or async result |
Validation Results
The validate function returns a discriminated union that cleanly separates success from failure:
When validation succeeds, the result contains the typed value. When it fails, the result contains an array of issues describing what went wrong:
Type Guards
valrs exports type guards for working with validation results:
Interoperability with Other Libraries
The power of Standard Schema is that valrs works seamlessly alongside other compliant libraries like Zod and Valibot.
Writing Library-Agnostic Code
Any function that accepts StandardSchemaV1 will work with valrs:
Mixed Schema Collections
You can mix schemas from different libraries in the same application:
JSON Schema Extension
valrs also implements StandardJSONSchemaV1, which extends the base interface with JSON Schema generation:
This enables tools to generate JSON Schema from your valrs schemas:
Type Inference
valrs exports utility types for extracting input and output types from any Standard Schema:
Checking Schema Compliance
You can check if an unknown object implements Standard Schema:
Why the ~standard Property?
The ~ prefix is intentional. It:
- Avoids conflicts with user-defined properties
- Signals internal use - consumers should use the public API when available
- Enables interoperability without polluting the schema object's namespace
You typically do not need to access ~standard directly when using valrs. The public API (parse, safeParse, etc.) is more ergonomic. The ~standard interface exists for framework and library authors who need to work with schemas generically.
Next Steps
- API Reference - Complete API documentation
- Getting Started - Quick setup guide