npm install @weipertda/sigiljsor:
bun add @weipertda/sigiljsA sigil is an executable contract object. Define structure once, then use it to turn unknown runtime data into trusted data at a boundary.
import { oneOf, optional, sigil } from '@weipertda/sigiljs';
const User = sigil.exact({
id: String,
email: String,
role: oneOf('admin', 'user'),
age: optional(Number),
});
// Validate unknown data
const result = User.safeParse(unknownInput);
if (result.success) {
console.log(result.data.role); // 'admin' | 'user' — verified
} else {
console.error(result.error.message);
console.error(result.error.path); // ['role'] — path to the failing field
}Use check() when you only need true or false:
User.check(data); // booleanUse parse() when you want to throw on invalid data:
try {
const trusted = User.parse(unknownInput);
} catch (error) {
console.log(error.message); // Expected property "role" to be "admin" | "user", got superuser
console.log(error.path); // ['role']
}Project the contract into JSON Schema, TypeScript, or OpenAPI:
User.toJSONSchema();
User.toTypeScript('User');
User.toOpenAPI();Generate a valid sample fixture:
const sample = User.mock({ seed: 1 });
// { id: 'string', email: 'string', role: 'admin' }
// The fixture is always valid
User.parse(sample); // no throwValidate a JSON file from the terminal:
sigil check contracts/user.sigil data/user.jsonFrom this repository before installing:
bun run src/playground.js check examples/workflows/cli-check-api-response/contract.sigil examples/workflows/cli-check-api-response/valid.jsonNext: Sigils · Public API · Stability Map