Valibot

repository·main·Indexed 11 days ago

https://github.com/fabian-hiller/valibot

A modular and type-safe schema library for validating structural data. Valibot features a functional API design that enables high tree-shaking efficiency to reduce bundle sizes. It provides utilities for runtime type checking via v.parse and v.safeParse, and includes a dedicated package, @valibot/to-json-schema, to convert schemas into JSON Schema Draft-07, Draft-2020-12, and OpenAPI 3.0 formats.

Tokens
249.8K
Snippets
954
Records
1.6K
Agent score
94%

What's inside Valibot

  1. Explore the Valibot Ecosystem

    main

    Valibot is supported by a wide range of third-party frameworks, libraries, and utilities. This ecosystem includes tools for web frameworks, API development, AI integration, form management, and schema transformations.

    Key categories include:

    • Frameworks: Support for Elysia, NestJS, Qwik, and more.
    • API Libraries: Integration with ORMs like Drizzle, web frameworks like Hono, and RPC libraries like tRPC and oRPC.
    • AI Libraries: Tools like LangChain, Mastra, and Flue that use Valibot for structured output or tool inputs.
    • Form Libraries: Specialized resolvers and managers for React Hook Form, TanStack Form, Superforms, and Vue-based solutions.
    • Schema Converters: Tools to convert Valibot schemas to JSON Schema, OpenAPI, or other formats, and vice versa (e.g., from Prisma or GraphQL).
  2. Handle Error Details and Async Validation

    main

    Error Details

    Superstruct's failures method is replaced in Valibot by accessing issues directly on the safeParse result or via the issues property of a ValiError. Use flatten, summarize, or getDotPath to process them.

    Async Validation

    Unlike Superstruct, Valibot supports asynchronous validation natively. Use v.pipeAsync() and v.checkAsync() to perform async logic (e.g., database checks) within your schema.

  3. Compare union and variant schemas

    main

    When choosing between union and variant, consider the structure of your data:

    • Use union for general logical OR relationships between any types (e.g., string | number).
    • Use variant for discriminated unions, where objects in the union share a common key (the discriminator) used to identify which schema to validate against. variant is preferred for performance and better error messages in object-based unions.
  4. Understand the Standard Schema types interface

    main

    Valibot implements the Standard Schema types interface, which defines how input and output types are handled within a schema. This interface uses two primary generics to distinguish between the raw data being validated and the transformed data produced after validation.

    Generics

    • TInput: Represents the type of the data before validation/transformation.
    • TOutput: Represents the type of the data after validation/transformation.

    StandardTypes Definition

    The StandardTypes interface consists of:

    • input: The type representing the input schema requirements.
    • output: The type representing the successful validation result.
  5. How Valibot's API design benefits coding agents

    main

    Valibot's architecture is designed to be highly predictable for both humans and AI agents. The library is composed of schemas, actions, and methods, all of which are implemented as plain objects.

    Because there is no complex class hierarchy or inherited methods to discover, once an agent understands how a single schema and action interact, it can apply that same pattern across the entire API. This reduces the amount of context required to write correct Valibot code.

  6. Use Methods to parse or modify schemas

    main

    Methods are functions that operate on schemas. The most common pattern is passing a schema as the first argument to a method to perform an operation, such as v.parse(schema, data).

    Note that while most methods are schema-centric, some exceptions like forward and flatten are used with actions or issues instead.

    import * as v from 'valibot';
    
    const BookSchema = v.object({ /* ... */ });
    
    function createBook(data: unknown) { 
      // v.parse is a method that uses the schema to validate data
      return v.parse(BookSchema, data);
    }
  7. Use pipelines for validation and transformation

    main

    The v.pipe() method allows you to chain multiple validation or transformation actions onto a schema.

    Rules for pipelines:

    • A pipeline must always start with a base schema.
    • It can be followed by up to 19 validation or transformation actions.
    • Actions are executed in sequence, passing the result of one action to the next.
    import * as v from 'valibot';
    
    const EmailSchema = v.pipe(v.string(), v.email(), v.endsWith('@example.com'));