Esta página se muestra en inglés. Una traducción al español está en curso.
ScriptsMay 15, 2026·3 min de lectura

Superstruct — Composable Validation for TypeScript and JavaScript

Lightweight library for validating data against structs with coercion, defaults, and detailed error paths.

Listo para agents

Este activo puede ser leído e instalado directamente por agents

TokRepo expone un comando CLI universal, contrato de instalación, metadata JSON, plan según adaptador y contenido raw para que los agents evalúen compatibilidad, riesgo y próximos pasos.

Native · 98/100Política: permitir
Superficie agent
Cualquier agent MCP/CLI
Tipo
Skill
Instalación
Single
Confianza
Confianza: Established
Entrada
Superstruct Overview
Comando CLI universal
npx tokrepo install dd4760a9-4ff5-11f1-9bc6-00163e2b0d79

Introduction

Superstruct is a validation library that defines data structures using composable struct definitions. Unlike schema-heavy validators, Superstruct treats validation as composition of simple building blocks. It produces clear error messages with exact paths to invalid fields and supports coercion for transforming data during validation.

What Superstruct Does

  • Validates JavaScript values against declarative struct definitions
  • Infers TypeScript types from struct schemas automatically
  • Coerces data by applying transforms like defaults, trimming, and type casting
  • Reports detailed errors with the exact path to each invalid field
  • Composes complex schemas from simple primitives using union, intersection, and refinement

Architecture Overview

Superstruct defines a struct as a function that takes unknown input and returns either the validated value or a detailed error. Structs compose through combinators like object(), array(), union(), and intersection(). The coercion system applies transforms in a pre-validation pass, and refinements add custom constraints on top of base types. All structs expose an Infer utility type for extracting static TypeScript types.

Self-Hosting & Configuration

  • Install via npm with zero dependencies
  • Define structs inline and export them for reuse across modules
  • Works with any JavaScript runtime and TypeScript version 4.0 or later
  • No build plugins or code generation required
  • Ships both ESM and CommonJS module formats

Key Features

  • Composable struct primitives that combine like building blocks
  • Automatic TypeScript type inference from struct definitions
  • Coercion layer for defaults, trimming, and data transformation
  • Detailed error messages with field paths for debugging
  • Lightweight with zero dependencies

Comparison with Similar Tools

  • Zod — richer API with method chaining; larger community and plugin ecosystem
  • ArkType — string-based TypeScript syntax; faster editor inference for complex types
  • Valibot — modular tree-shakeable design; similar composable approach
  • io-ts — fp-ts integration with codec-based encode/decode; more FP-oriented

FAQ

Q: When should I use Superstruct over Zod? A: Superstruct is a good fit when you prefer a minimal composable API and want built-in coercion. Zod is better when you need a larger ecosystem of plugins and integrations.

Q: Does Superstruct support async validation? A: Superstruct is synchronous by design. For async validation, run async checks separately after struct validation passes.

Q: Can I use Superstruct for API request validation? A: Yes. Use assert() or create() in your request handlers to validate and optionally coerce incoming payloads.

Q: How does coercion work? A: Define a coercion with coerce(struct, transform). During create(), the transform runs before validation, allowing defaults, trimming, or type conversion.

Sources

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados