Litestone guide / Misc
JSON Schema
generateJsonSchema turns the schema into one JSON Schema document: types, formats, bounds, required keys and the rest of what a validator can say. It is what a service validates a request against and a form reads its labels from.
enum Role { admin member }
model User {
id Int
email String
role Role (member)
age Int? (0)
}
import { generateJsonSchema } from '@frontierjs/litestone'
const doc = generateJsonSchema(db.$schema, { mode: 'create' })
const user = doc.$defs.User
if (user.properties.email.format !== 'email') throw new Error('expected the @email format')
if (user.required.join() !== 'email') throw new Error('only email is required on create')
if (doc.$defs.Role.enum.join() !== 'admin,member') throw new Error('expected the enum')
A field with a default, an @id the server assigns, or a ? is not required on create.
What else it can do
litestone jsonschemawritesschema.jsonbeside the.lite;--stdoutprintsmodeiscreate,update(a partial patch) orfull;--all-modeswrites all three--format=flatputs every definition at the document root instead of$defs--include-timestampsand--include-deleted-atkeep those columnsaudience,inlineEnumsandtitleare options of the function onlyx-keys carry what JSON Schema has no word for: gates, relations, transitions, versions and labelsx-opaquenames the rules a document cannot state, andx-messagesthe one sentence for several keys- A committed copy can lag the
.lite, so regenerate before citing it
In depth: JSON Schema § Producing one, § Modes, § x- extensions, § Audience, § Type mapping