@sekhmet/schema
@sekhmet/schema validates and transforms values. A Schema<I, O, E> parses an input I, under a panic runtime from @sekhmet/panic, into a Vow of an Outcome<O, E>: the output, an expected error, or Panicked when your own code in the schema threw. The input, the output and the error are all typed, a failure is a value rather than a throw, and a synchronous parse settles synchronously. The built-in schemas fail with SchemaIssues that say why a value was rejected, and composites such as object report every failure with its path. Every schema also describes itself as data, for tools like form generators, and bridges to Standard Schema in both directions.
Install
The package is published on JSR.
deno add jsr:@sekhmet/schemaFor Node.js, Bun and other npm-compatible runtimes, install through JSR's npm compatibility layer:
npx jsr add @sekhmet/schema # npm
pnpm dlx jsr add @sekhmet/schema # pnpm
yarn dlx jsr add @sekhmet/schema # yarn
bunx jsr add @sekhmet/schema # bunEntrypoints
@sekhmet/schema re-exports every main entrypoint. Each feature also has a mini entrypoint of tree-shakable functions, with a subpath per module (e.g. @sekhmet/schema/core/mini/issue).
| Feature | Main (method chaining) | Mini (functions) | Holds |
|---|---|---|---|
| core | @sekhmet/schema/core | @sekhmet/schema/core/mini | Schema, parsing, transforms, SchemaIssue, Issues, nodes, annotations, is |
| primitives | @sekhmet/schema/primitives | @sekhmet/schema/primitives/mini | string, number, boolean, stringbool, bigint, date, literal… |
| composites | @sekhmet/schema/composites | @sekhmet/schema/composites/mini | object, array, tuple, record, union, discriminatedUnion, lazy… |
| algebra | @sekhmet/schema/algebra | @sekhmet/schema/algebra/mini | or, brand, readonly, and the shape reshapers pick, omit, partial, required |
| standard | @sekhmet/schema/standard | @sekhmet/schema/standard/mini | toStandard and fromStandard |
Example
import { assertEquals } from "@std/assert";
import { definePanicRuntime } from "@sekhmet/panic";
import { object } from "@sekhmet/schema/composites";
import { entries, formatIssue } from "@sekhmet/schema/core";
import { number, string } from "@sekhmet/schema/primitives";
const panics = definePanicRuntime();
const member = object({
shape: {
name: string({ trim: true, minLength: 1 }),
age: number({ int: true, min: 0 }),
},
});
const ada = member.parse({ input: { name: " Ada ", age: 36 }, panics });
assertEquals((await ada.wait()).unwrap(), { name: "Ada", age: 36 });
const bad = member.parse({ input: { name: "", age: 1.5 }, panics });
const issues = (await bad.wait()).unwrapErr();
assertEquals(
entries(issues).map(({ path, error }) => [path, formatIssue(error)]),
[
[["name"], "Too small: expected string to have >= 1 characters"],
[["age"], "Expected integer, received number"],
],
);Learn more
- Tutorial: Getting started builds a schema step by step, from one string to a checked, transformed object.
- How-to guides: Recipes for validating JSON bodies, reading query strings and forms, parsing API responses, branding IDs, reshaping objects, recursive data, type guards, form labels and Standard Schema.
- Explanation: Concepts covers why a schema parses to an
Outcome, issues as data, strict primitives, checks as options, main and mini, sync and async, and schemas that describe themselves. - Reference: the API reference on JSR documents every schema, option and issue.
The guides are also on the docs site.
Design record
The decision record keeps the history of the package's design decisions and the alternatives they replaced.
License
MIT. See LICENSE.md.