Superstruct is a robust and composable library for validating data in JavaScript and TypeScript, designed to provide a simple yet powerful way to define data schemas and enforce their integrity. Currently stable at version 2.0.2, the library maintains an active release cadence, frequently publishing patches to address type resolution, minor bugs, and improve compatibility, as seen with recent 2.0.x releases. Its core differentiator lies in its idiomatic JavaScript API, making it easy to define complex data structures using plain objects and functions, rather than relying on class-based or decorator-heavy approaches. Superstruct focuses on clear error reporting and type inference, integrating seamlessly into TypeScript projects to provide compile-time type safety alongside runtime validation. It's particularly useful in API development, configuration parsing, and any scenario requiring reliable data ingress.
npm install superstructVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates defining a data schema for a `User` using `object`, `string`, `number`, and `array` structs, then uses `assert` to validate both valid and invalid data against the schema, catching and logging `StructError` for failures.
Review any schemas using `object()`, `type()`, or `record()` that validate properties expecting arrays. Ensure your input data strictly adheres to the defined array types, as previously invalid arrays will now correctly cause validation failures.
For Node.js projects, ensure your `tsconfig.json` correctly configures `moduleResolution` (e.g., `bundler` or `node16`) and `module` (e.g., `esnext`, `nodenext`). If using CommonJS, be mindful of `require()` calls and potential interop issues, favoring ESM imports where possible.
If experiencing type resolution issues in a CommonJS project with NodeNext, ensure you are on Superstruct `v2.0.3` or newer. You may also need to adjust your `tsconfig.json` to explicitly handle `d.cts` files or ensure your build setup correctly processes CJS type definitions.
Migrate your import statements to ES Modules syntax: `import { object } from 'superstruct'`. If you are in a Node.js CJS module, you may need to ensure your `package.json` specifies `"type": "commonjs"` and potentially configure your build system for interop.Verify `superstruct` is installed. Check your `tsconfig.json` for `moduleResolution` (e.g., `"node16"` or `"bundler"`) and `module` (e.g., `"esnext"` or `"nodenext"`). Ensure your TypeScript version is up-to-date (>=4.7 for NodeNext features).
Inspect the `error.path` and `error.value` properties of the `StructError` to identify the problematic data point. Adjust the input data to conform to the schema's type expectations, or modify the schema if the data structure has genuinely changed.
No dependency data recorded yet.