PACKAGE · VALIDATION

superstruct

A simple and composable way to validate data in JavaScript (and TypeScript).

WEEKLY DOWNLOADS 2.6M
STARS 7.1K
FORKS 221
OPEN ISSUES 100
GZIP SIZE 3.5 kB
UNPACKED SIZE 182.3 kB
DEPENDENCIES 1
LAST UPDATED 1y ago
DOWNLOAD TRENDS

superstruct downloads — last 12 months

Download trends for superstruct1 download series from Jun 2025 to May 2026. Use left and right arrow keys to inspect monthly values.05.3M10.7M16.0M21.3MJun 2025SepDecMarMay 2026
superstruct
ABOUT SUPERSTRUCT

Superstruct is a library designed for defining and validating data structures in JavaScript and TypeScript. It addresses the common challenge of ensuring data integrity and consistency, especially in applications where data originates from external sources like APIs, user inputs, or configuration files. By providing a declarative way to describe expected data shapes, Superstruct allows developers to catch errors early in the development cycle and at runtime, preventing unexpected behavior and improving application robustness.

The core philosophy of Superstruct centers around composability and expressiveness. It allows developers to build complex data types by combining simpler primitives, offering a powerful yet intuitive API. This approach makes it suitable for a wide range of projects, from small utility scripts to large-scale applications requiring rigorous data validation. The primary audience includes developers working with dynamic data sources who need a reliable method for type checking and data sanitization.

Key to Superstruct's API is its schema definition language. Developers define structures using functions like `Struct.string()`, `Struct.number()`, `Struct.boolean()`, and `Struct.object()`, which can be nested and combined. For example, `Struct.object({ name: Struct.string(), age: Struct.number() })` defines a user object. It supports advanced features like unions, intersections, optional fields, and custom validation logic through the `refine` method, enabling highly specific data constraints.

Superstruct integrates seamlessly into various development workflows. Its TypeScript support is first-class, allowing for static type inference from defined structures. This makes it an excellent companion for projects using TypeScript, enhancing developer experience and compile-time safety. It can be used in backend frameworks for API input validation or in frontend applications to validate data before state updates or rendering.

With a reported bundle size of only 3.5 kB (gzipped), Superstruct offers significant validation capabilities with minimal impact on application performance and load times. This small footprint makes it an attractive option even for performance-sensitive applications or those with strict bundle size budgets. Its maturity is indicated by its widespread adoption, with 3.7 million weekly downloads, and consistent updates, as evidenced by its last update on 2024-10-01.

While Superstruct is highly capable, developers should be aware that its extensive feature set might introduce a learning curve for newcomers. For extremely simple validation needs, such as validating a single primitive value without complex nesting, a more minimal, built-in JavaScript approach might be sufficient and lighter. Additionally, the abstraction layer, while powerful, means developers are working with the Superstruct API rather than direct JavaScript types in some validation scenarios.

WHEN TO USE
  • When defining complex data shapes for API responses or request bodies that require strict validation.
  • When leveraging TypeScript for static type inference directly from your data validation schemas.
  • When building forms where user input needs to be validated against specific types and constraints before submission.
  • When you need to create reusable validation logic by composing smaller struct definitions.
  • When ensuring data consistency across different parts of an application, especially with data fetched from external services.
  • When defining runtime type checks for data manipulation in JavaScript environments without static typing.
WHEN NOT TO USE
  • If your validation needs are limited to simple primitive type checks (e.g., ensuring a value is a string or number) – native JavaScript checks or a lighter validation library may suffice.
  • If you are working in a highly performance-critical environment where even a 3.5 kB bundle size is a concern and custom, unopinionated validation logic is preferred.
  • When you primarily rely on static typing for all data integrity and do not require runtime validation beyond what TypeScript provides at compile time.
  • If your data structures are extremely simple and do not benefit from the composability and features offered by defining explicit schemas.
  • When integrating with systems that enforce their own schema validation and do not require an additional layer of validation on your end.

CORRECTIONS

Spot wrong data here?

A short note helps us fix it.

Anonymous · No account · No email back

COMPARISONS 9
superstruct vs fast-xml-parser ★ 3.1K · 40.1M/wk superstruct vs zod ★ 42.9K · 94.7M/wk superstruct vs valibot ★ 8.7K · 5.8M/wk superstruct vs @sinclair/typebox ★ 8 · 47.8M/wk superstruct vs yup ★ 23.7K · 5.7M/wk superstruct vs ow ★ 3.9K · 1.2M/wk superstruct vs joi ★ 21.2K · 10.1M/wk superstruct vs ajv ★ 14.7K · 153.1M/wk superstruct vs class-validator ★ 11.8K · 4.5M/wk