Validation3M+/wkMIT

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
npm install superstruct
yarn
yarn add superstruct
pnpm
pnpm add superstruct

Import

ESM
import { object, string, number, optional } from 'superstruct';

Quick Example

usage
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 valid

About 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

Packagesuperstruct
CategoryValidation
Weekly Downloads3M+
LicenseMIT
Installnpm install superstruct

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.