Back to articles
TypeBox Has a Free API — JSON Schema Validation at TypeScript Speed

TypeBox Has a Free API — JSON Schema Validation at TypeScript Speed

via Dev.to JavaScriptAlex Spinov

TypeBox creates JSON Schema types that are also TypeScript types. One definition, two systems — compile-time safety and runtime validation from the same code. What is TypeBox? TypeBox generates standard JSON Schema objects that double as TypeScript types. It works with any JSON Schema validator (Ajv, etc.) while giving you full TypeScript inference. Quick Start npm install @sinclair/typebox import { Type , Static } from ' @sinclair/typebox ' ; import { Value } from ' @sinclair/typebox/value ' ; const User = Type . Object ({ name : Type . String (), email : Type . String ({ format : ' email ' }), age : Type . Number ({ minimum : 0 }), }); type User = Static < typeof User > ; // { name: string; email: string; age: number } // Validate const isValid = Value . Check ( User , { name : ' Alice ' , email : ' a@b.com ' , age : 30 }); // true // Get errors const errors = [... Value . Errors ( User , { name : 123 })]; Common Types // Primitives Type . String (); Type . Number (); Type . Boolean

Continue reading on Dev.to JavaScript

Opens in a new tab

Read Full Article
2 views

Related Articles