Sapphire Documentation
Schema once, types and adapters everywhere. These docs walk you from a first install through every concept and adapter Sapphire ships with.
Getting Started
- Getting Started β install, your first schema, parse/safeParse, plugging in an adapter.
Concepts
- Overview β what Sapphire is, what it isnβt, and the mental model.
- Fields and modifiers β full vocabulary of primitives, composites, and modifiers.
- Inferring types β
Infer<>vsInferInput<>and the brand-type model. - Composition β
pick,omit,partial,required,extend,merge. - Unions, literals, and enums β the
a.type()namespace. - Tuples vs arrays β fixed-position vs homogeneous collections.
- Refs and relations β named schemas and
a.ref(). - Nullable vs optional β the canonical confusion, resolved.
- Validation β
parse/safeParse, issues, message resolution. - Config β
Sapphireoptions and per-call overrides. - Escape hatch β
.adapter(name, opts)for ORM-specific options.
Adapters
- Mongo (
@ascendance-hub/sapphire-bson) β native MongoDB driver$jsonSchemacollection validators. - Mongoose (
@ascendance-hub/sapphire-mongoose) β MongooseSchemaoutput, with refs, timestamps, and composite indexes. - JSON Schema (
@ascendance-hub/sapphire-json-schema) β JSON Schema 2020-12 output for AJV, MCP tools, and frontend form generators. - Drizzle (
@ascendance-hub/sapphire-drizzle) βpgTable/mysqlTable/sqliteTableoutput with lazy refs and composite indexes.
Recipes
- Form validation β collect every issue from
safeParseand pivot to per-field UI errors. - Share types with the frontend β one schema feeds Mongo,
Infer<>types, and JSON Schema for forms or MCP tools. - One schema, many adapters β register multiple adapters and emit each from the same IR.
- Writing a custom adapter β walk the IRβs 12 kinds and plug into the registry.
- Custom error messages β i18n and branded messages through the 5-level resolution hierarchy.
- Migrating from Zod β side-by-side mapping plus what each library does that the other doesnβt.
Meta
- Architecture β the 3-layer model (DSL β IR β adapter) with a Mermaid diagram.
- Design decisions β why the API looks the way it does.
- Contributing β repo setup and how to write a third-party adapter.