Schema

Overview

npm i @webroute/schema

The JS/TS ecosystem has a wide range of schema and validation libraries. Each library has strengths and weaknesses, and caters to different use cases and preferences.

Instead of providing yet another schema library, @webroute/schema enables supporting existing libraries by unifying tooling and empowering downstream tools to be schema-agnostic.

Parse and Infer

import { Infer, parse } from "@webroute/schema"
 
type Schema = Infer<typeof AnySchema>;
 
const parsed = parse(AnySchema, value):

Convert Between Schemas

@webroute/schema provides flexible tools for converting popular schema libraries to and from each other, and even into JSON schema.

import { createParser, createFormatter } from "@webroute/schema";
import { ZodParser } from "@webroute/schema/zod";
import { JoiFormatter, JoiJsonSchemaFormatter } from "@webroute/schema/joi";
 
const parser = createParser(ZodParser());
const formatter = createFormatter(JoiFormatter());
const joiToJsonSchema = JoiJsonSchemaFormatter();
 
// Convert from zod schema to Joi schema
const def = parser.parse(MyZodSchema);
const MyJoiSchema = parser.parse(def);
// Convert this to JSON Schema
const jsonSchema = joiToJsonSchema(MyJoiSchema);

On this page