explaingit

ianstormtaylor/superstruct

7,146TypeScriptAudience · developerComplexity · 2/5Setup · easy

TLDR

A TypeScript and JavaScript library for validating that data from APIs or user input matches the shape you expect, returning detailed, field-specific errors and supporting coercion and TypeScript type narrowing.

Mindmap

mindmap
  root((repo))
    What it does
      Runtime data validation
      Detailed error reporting
      Coercion and defaults
    Tech stack
      TypeScript
      JavaScript
    Use cases
      API response validation
      Form input checking
      Type narrowing
    Audience
      Web developers
      Full-stack engineers
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Validate API response data before using it in your app so unexpected shapes are caught immediately.

USE CASE 2

Apply default values to incoming data while validating it in a single step using coercion.

USE CASE 3

Define all your data shape definitions in one place and reuse them across a TypeScript codebase.

USE CASE 4

Validate user form input and return field-specific error messages to display in the UI.

Tech stack

TypeScriptJavaScript

Getting it running

Difficulty · easy Time to first run · 5min

In plain English

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.

Copy-paste prompts

Prompt 1
How do I use Superstruct to validate a JSON API response in TypeScript and automatically infer the type of the validated data?
Prompt 2
Show me how to define a Superstruct schema with optional fields and default values using coercion so missing fields are filled in automatically.
Prompt 3
How do I write a custom Superstruct validator for an email address format that I can reuse across my app?
Prompt 4
How can I catch a Superstruct validation error and extract which specific field failed to show the user a helpful message?
Prompt 5
What is the difference between assert(), validate(), and is() in Superstruct, and when should I use each?
Open on GitHub → Explain another repo

← ianstormtaylor on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.