superstruct
Superstruct is a library for validating data in JavaScript and TypeScript using composable, predictable, and familiar APIs. Superstruct takes a functional appro…
Installation
npm install superstruct
yarn add superstruct
pnpm add superstruct
Import
import { object, string, number, optional } from 'superstruct';Quick Example
import { object, string, number, optional, assert } from 'superstruct';
const User = object({
name: string(),
email: string(),
age: optional(number()),
});
assert({ name: 'Alice', email: '[email protected]' }, User);
// Throws if invalid, passes silently if validAbout superstruct
Superstruct is a library for validating data in JavaScript and TypeScript using composable, predictable, and familiar APIs. Superstruct takes a functional approach where schemas are built by composing small, reusable struct functions. The API provides primitive types (string(), number(), boolean()), complex types (object(), array(), tuple(), record()), utility types (optional(), nullable(), defaulted()), and combinators (union(), intersection(), type()) for building sophisticated validation schemas. Each validation returns a detailed failure result with the path to the invalid data, the expected type, and the received value. Superstruct schemas can coerce data during validation (trimming strings, parsing dates, converting numbers), making them suitable for normalizing input data. The library's define() function allows creating custom types with arbitrary validation logic. TypeScript users get automatic type inference from schema definitions through Infer<typeof schema>. Superstruct is designed to be smaller and more composable than Joi while providing better TypeScript support than Yup. The library is used by Slate.js (the rich text editor) for validating editor state. Superstruct is a solid choice for projects that value composability and small bundle size in their validation library.
Quick Facts
| Package | superstruct |
| Category | Validation |
| Weekly Downloads | 3M+ |
| License | MIT |
| Install | npm install superstruct |
Related Packages
Zod is a TypeScript-first schema validation library that provides a concise, chainable API for defin…
Yup is a schema validation library for JavaScript that provides a declarative, chainable API for def…
Ajv (Another JSON Schema Validator) is the fastest JSON Schema validator for JavaScript and TypeScri…
Valibot is a modular schema validation library for TypeScript that achieves exceptionally small bund…
TypeBox is a JSON Schema type builder that creates in-memory JSON Schema objects that can be statica…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.