Validate API response data before using it in your app so unexpected shapes are caught immediately.
Apply default values to incoming data while validating it in a single step using coercion.
Define all your data shape definitions in one place and reuse them across a TypeScript codebase.
Validate user form input and return field-specific error messages to display in the UI.
Superstruct is a JavaScript and TypeScript library for validating data at runtime. The idea is that you define a schema describing what shape your data should have, then pass actual data through it to confirm it matches. If it does not, you get a detailed error explaining exactly what was wrong and where. The library is aimed at situations where you receive data from outside your control, such as a REST API or user input, and need to confirm it is structured correctly before using it. You define your expected shapes using composable building blocks: an object with specific fields, an array of a particular type, a string, a number, nested objects, and so on. You can also define custom validators for things like email addresses or UUIDs that are specific to your application. One feature the README highlights is coercion: you can have Superstruct apply default values and other transformations to incoming data as part of the validation step, so the resulting object is both validated and ready to use. In TypeScript, the library also provides type narrowing, meaning TypeScript will understand the shape of the data in the block where validation passed, without needing separate type declarations. The README explains the design choices by comparing Superstruct to other popular validation libraries. The author argues that many of them return unclear errors, make custom types difficult to add, spread type definitions across the codebase, or are tied to a specific framework like Express. Superstruct aims to be framework-agnostic, to return errors with enough detail to surface to end users, and to encourage defining all your custom types in one place. It works in any JavaScript environment, both in browsers and on the server. A live interactive demo is available, and the repository includes example files for common patterns like optional values, composed structs, and returning errors instead of throwing them.
← ianstormtaylor on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.