@mikrojs/schema
v0.22.0
Published
Schema DSL for Mikro.js: declare a shape once, infer its type, validate against it, and publish it for a registry to render a form from
Maintainers
Readme
@mikrojs/schema
The schema DSL behind Mikro.js: declare a shape once, infer its TypeScript type, validate values against it, and serialize it as plain JSON for something else to read. Zero dependencies, no Node APIs, so it runs on Node, Bun, Deno, Cloudflare Workers and in a browser.
Apps on a device import mikro/schema instead, which is this same DSL backed by a native
module plus a Result-returning parse(). This package is the host half: the CLI evaluating
mikro.config.ts, a registry validating what an operator saves, and anything else that needs
to read a published schema.
import {number, object, string, validate} from '@mikrojs/schema'
const Config = object({
mqttUrl: string({title: 'Broker URL', format: 'url'}),
interval: number({title: 'Publish interval', unit: 's', default: 60, min: 1, max: 3600}),
})
validate(Config, {mqttUrl: 'mqtt://box.local', interval: 60}, '') // => null
validate(Config, {mqttUrl: 'mqtt://box.local', interval: 0}, '')
// => {ok: false, error: {name: 'ValidationFailed', message: 'below the minimum of 1', path: '.interval'}}validate() returns null when the value is good, and covers shape plus every numeric and
length bound. format is not checked here; see @mikrojs/schema/config.
Annotations
Nodes carry annotations, and they divide in two. Display annotations (title,
description, mask) describe how a value should be presented and never change what
validates, so a consumer may ignore any it does not recognise. Constraints (min, max,
integer, minLength, maxLength, minItems, maxItems, format, unit) do change what
validates, and a consumer must not ignore one it does not recognise: ignoring a constraint
means accepting a value the author ruled out.
unit is a closed enum following SenML with ASCII
keys. It is a display hint and constrains nothing. Never convert a stored value through a
unit's scale — see the note beside UNITS.
@mikrojs/schema/config
The host-side machinery for Mikro.js OTA config, which is what a registry needs:
validateConfig(schema, value)—validate()plusformat, returning{ok, value}or{ok: false, error}parseConfigSchema(value)— validate a schema that arrived as JSON, rejecting malformed annotations and unknownformatorunitvaluesderiveOverlay/parseEffective/materializeDefaults— the sparse-overlay model, where a registry stores only deviations from the author's defaults and serves the complete documentdiffConfigSchemas(previous, next)— what changed between two releases, and whether it needs an operator's attentionUNITS,FORMATS— the unit table and the format names
format lives here rather than in the main entry for two reasons: the expressions are a
denial-of-service surface a device should not carry, and a device has no regular-expression
engine to carry them with.
The wire format is the contract. It is specified in the OTA Registry Spec under "The config schema", and that document, not this package's version number, is what a third-party registry should build against.
