English | Русский
This guide takes you from installation to a validated value and a reviewable contract snapshot.
- Node.js
>=20.10 - An ESM-compatible TypeScript project
Install the umbrella package for the complete runtime and tooling surface:
npm install safe-shapeimport { integer, object, string, type Infer } from "safe-shape";
const User = object({
id: string({ minLength: 1 }),
age: integer({ minimum: 0 }).optional(),
});
type User = Infer<typeof User>;
const result = User.safeParse({ id: "user_1", age: 42 });
if (!result.success) {
console.error(result.error.issues);
} else {
const user: User = result.data;
console.log(user.id);
}safeParse() returns a discriminated result. Use parse() instead when an
invalid value should throw. SafeShape does not coerce { age: "42" }; input
changes require an explicit transform.
Every failed result contains stable, structured issues:
const result = User.safeParse({ id: "", age: -1 });
if (!result.success) {
for (const issue of result.error.issues) {
console.error(issue.code, issue.path, issue.message);
}
}Issue paths are arrays and remain machine-readable through validation reports, HTTP helpers, Standard Schema, and the CLI.
Compile the schema module to ESM, then point the installed CLI at the JavaScript file:
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.tsCreate a reviewed v2 baseline:
safe-shape contract snapshot \
--module ./dist/contracts/user.js \
--export User \
--id user \
--format v2 \
--out ./.safe-shape/user.contract.jsonCheck the input contract in CI:
safe-shape --json contract check \
--module ./dist/contracts/user.js \
--export User \
--against ./.safe-shape/user.contract.json \
--side input \
--compatibility backwardCommit reviewed baselines. Do not regenerate them inside the CI check job.