superstruct

v2.0.2 MIT

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

Weekly Downloads
3.2M
Stars
7.1K
Forks
220
Open Issues
98
Gzip Size
3.5 kB
Unpacked Size
182.3 kB
Dependencies
1
Last Updated
1y ago

superstruct Download Trends

Download trends for superstruct04.5M9.1M13.6M18.1MFeb 2025MayAugNovFebApr 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.

superstruct Alternatives

superstruct Categories