API Reference

Superstruct

A lightweight, composable data validation library that infers TypeScript types from runtime schemas.

Official Documentation ↗
CompatibilityThis documentation covers version v2.0.2. Check the official docs for the latest API surface.

Installation

npmnpm install superstruct

Methods

Superstruct builds validators from small composable pieces and reports the exact failing path.

Key Features

  • Composable structs with type inference
  • assert and is for different failure modes
  • Friendly error paths and messages

Usage Notes

Pin a version and check the official docs for the API surface you depend on before upgrading.

Errors

The following points are quoted verbatim from the official documentation (https://docs.superstructjs.org/). Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

  • But Superstruct is designed for validating data at runtime, so it throws (or returns) detailed runtime errors for you or your end users. (https://docs.superstructjs.org/)

FAQ

How do I coerce values?
Wrap a struct in coerce(Struct, (value) => newValue) to transform or default values when they do not match.
What is the error format?
Failures expose a failures() function returning { key, value, type } entries, making it easy to map errors to form fields.
Does it validate at compile time?
No. Superstruct is a runtime validator; the TypeScript type is inferred from the struct, but invalid data is only rejected at runtime.