Skip to content

Repository files navigation

SafeShape

Runtime contracts for TypeScript: define one schema, validate unknown input at runtime, infer static types, and generate tooling artifacts from the same source.

npm packageNode.jsTypeScriptESMrelease gate

SafeShape terminal demo showing install, schema definition, validation, diagnostics, and CLI export

Why SafeShape

TypeScript types disappear at runtime. SafeShape keeps the runtime boundary explicit: no hidden coercion, immutable schemas, stable diagnostics, and strong type inference from the contract you actually execute.

Use SafeShape when data crosses a trust boundary:

  • API requests and responses.
  • JSON files and config.
  • CLI input and generated artifacts.
  • Webhook payloads and integration events.
  • Any unknown value that must become typed data.

How It Differs From Zod

Zod is the broader validation ecosystem today. SafeShape is intentionally narrower: it treats runtime schemas as public contracts that must stay explicit, documented, toolable, and release-tested.

DecisionZodSafeShape
Primary goalTypeScript-first schema validationRuntime contract platform
API shapeBroad convenience surfaceConservative stable surface
CoercionRich convenience APIs, including coercion-oriented workflowsNo hidden coercion; transforms are explicit
ToolingLarge ecosystem and built-in conversion featuresFirst-party CLI, JSON Schema export, TypeScript generation, validation reports
HTTP boundariesUsually handled through adapters or app codeFirst-party framework-neutral HTTP helpers
Release postureMature general-purpose libraryContract-first release gate with tests, examples, benchmarks, consumer install, audit, and pack dry-run

Choose Zod when you need the largest ecosystem and the widest validation feature set. Choose SafeShape when you want a smaller contract layer with explicit runtime behavior, stable diagnostics, first-party tooling, and package boundaries that are designed for API stability.

Quick Start

Install the full runtime and tooling surface:

npm install safe-shape

Define a schema and validate unknown input:

import{number,object,string,typeInfer}from"safe-shape";constUser=object({id: string(),age: number().optional(),});typeUser=Infer<typeofUser>;constresult=User.safeParse({id: "user_1",age: 42});if(!result.success){console.error(result.error.issues);}else{constuser: User=result.data;console.log(user.id);}

SafeShape validates without coercion. { age: "42" } is invalid until you add an explicit transform.

CLI Example

SafeShape also ships a CLI. Use it to turn runtime contracts into generated artifacts:

safe-shape --json schema export \
--module ./dist/contracts/user.js \
--export User \
--schema https://json-schema.org/draft/2020-12/schema \
--out ./dist/contracts/user.schema.json
safe-shape --json schema types \
--module ./dist/contracts/user.js \
--export User \
--name User \
--out ./dist/contracts/user.d.ts

The CLI is machine-readable under --json, treats validation failures as command results, and does not require authentication.

Release Metrics

Current stable release gate:

SignalStatus
Packages7 publishable packages
Unit tests69 passing tests
Consumer installTarball install smoke check passes
ExamplesRunnable examples pass
Security audit0 known vulnerabilities
Benchmarks5 runtime parse scenarios
Package dry runnpm pack --workspaces --dry-run passes

Sample local benchmark run on Node.js v20.10.0 / macOS arm64:

ScenarioThroughput
Primitive string safeParse valid9,107,731 ops/sec
Object user safeParse valid269,899 ops/sec
Union event safeParse valid38,447 ops/sec
Array users safeParse valid11,389 ops/sec
Object user safeParse invalid78,017 ops/sec

Benchmark results are execution evidence, not fixed release thresholds. Re-run them locally with:

npm run build
npm run benchmarks:check

Packages

Install safe-shape when you want the complete public surface from one package. Import only what each module needs:

import{object,string,validateSchema}from"safe-shape";

Use narrower packages when you want strict dependency boundaries:

PackagePurpose
safe-shapeUmbrella package that re-exports runtime and tooling APIs
@safe-shape/coreRuntime schemas, parsing, diagnostics, and type inference
@safe-shape/httpFramework-neutral HTTP boundary helpers
@safe-shape/json-schemaJSON Schema export
@safe-shape/typescriptTypeScript declaration generation
@safe-shape/validationJSON-friendly validation reports
@safe-shape/cliCommand-line tooling

Design Principles

  • Runtime first.
  • API stability over feature count.
  • Immutable schemas and parse results.
  • Rich diagnostics with stable issue paths.
  • Correctness before performance.
  • Performance before convenience.
  • No magic and no hidden coercion.

Documentation

Local Development

npm install
npm run build
npm run test
npm run release:check

Use the built CLI without a global install:

npm run cli:doctor

Runnable examples live in examples:

npm run examples:check

Project Status

SafeShape is on the 1.0.x stable API release line. The release gate covers metadata checks, build, typecheck, tests, examples, benchmarks, consumer tarball installation, npm audit, and package dry-run.

About

Runtime contracts for TypeScript: validation, diagnostics, JSON Schema, TypeScript generation, and CLI tooling.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages