Schema

Library Support

@webroute/schema currently supports the following libraries.

InferParseParserFormatterDiscriminatorJSON Schema
Zod
Yup
Joi
Valibot
TypeBox
Superstruct
Runtypes
Installation

Bear in mind the actual validation library must be installed to work properly.

For example: @webroute/schema/zod -> npm i zod

Usage

Under the @webroute/schema/<library> packages, you can find helpers for the given validator library.

For example, we can import the zod helpers via.

import {
  ZodParser,
  ZodFormatter,
  ZodDiscriminator,
  ZodJsonSchemaFormatter,
} from "@webroute/schema/zod";

Parsing

const parser = createParser(ZodParser());
 
// Parse zod to it's underlying schema definition
const def = parser.parse(SomeZodSchema);

Formatting

const formatter = createFormatter(ZodFormatter());
 
// Format it back into zod, or another format
const BackToZodSchema = formatter.format(def);

JSON Schema

const toJsonSchema = ZodJsonSchemaFormatter();
 
// Convert into JSON schema
const jsonSchema = toJsonSchema(SomeZodSchema);

On this page