Provides shape-based validation for the values, retrieval templates and retrieved data crossing a service boundary.
The schema function describes the payloads a shape admits as a JSON Schema, and the validate
function returns either the value, typed as the shape describes it, or a trace of every violation found. A shape
states once what a service accepts, so the description and the checks stay in one place instead of being restated
across the interfaces and entry points that exchange data with clients.
Shapes are stated with the per-kind modules value gathers:
A shape also declares schema-driven consumers, such as API descriptions, language model tool interfaces and
structured output formats, without a hand-written schema restating it. The schema function describes the
payloads a resource shape admits as a JSON Schema (draft 2020-12). Its strict variant keeps to the subset of JSON
Schema accepted by structured output generators, which reject the keywords and formats outside it:
import { schema } from'@metreeca/blue';
schema(Product); // the payloads Product admits schema(Product, { strict:true }); // the same, for strict structured output generators
Collecting entry shapes and their dependencies
Model-wide encodings, such as vocabularies, schemas and documentation, cover every shape a set of entry shapes
depends on rather than only the entry shapes themselves. The model function collects the entry shapes and
the shapes they extend, link to or embed, each once and in a stable order, ready to be encoded as a whole:
The same shape answers three questions; the model option settles which is being asked:
import { validate } from'@metreeca/blue';
validate(product, { shape:Product })({ // a resource in its own right value:product=>console.log(product.name), trace:trace=>console.error(trace) });
validate(response, { shape:Product, model: { name: {} } }); // a retrieval, narrowed by what was asked for validate(request, { shape:Product, model:true }); // a retrieval template, before it is issued
omitted, or false, matches value against the shape as a resource in its own right
a template matches value against the members that template asked for, and types what comes back accordingly
true matches value against the shape as a template, asking whether the shape can serve what it requests
Caution
A template may draw on the full query language, including aggregate transforms and nested expansion. Hold an
untrusted caller to what the service will serve: plain refuses the aggregate transforms, depth caps the
nesting, and limit caps the page.
Linked data validation.
Provides shape-based validation for the values, retrieval templates and retrieved data crossing a service boundary. The schema function describes the payloads a shape admits as a JSON Schema, and the validate function returns either the value, typed as the shape describes it, or a trace of every violation found. A shape states once what a service accepts, so the description and the checks stay in one place instead of being restated across the interfaces and entry points that exchange data with clients.
Shapes are stated with the per-kind modules value gathers:
Describing a shape as JSON Schema
A shape also declares schema-driven consumers, such as API descriptions, language model tool interfaces and structured output formats, without a hand-written schema restating it. The schema function describes the payloads a resource shape admits as a JSON Schema (draft 2020-12). Its strict variant keeps to the subset of JSON Schema accepted by structured output generators, which reject the keywords and formats outside it:
Collecting entry shapes and their dependencies
Model-wide encodings, such as vocabularies, schemas and documentation, cover every shape a set of entry shapes depends on rather than only the entry shapes themselves. The model function collects the entry shapes and the shapes they extend, link to or embed, each once and in a stable order, ready to be encoded as a whole:
Matching a value against a shape
The same shape answers three questions; the
modeloption settles which is being asked:false, matchesvalueagainst the shape as a resource in its own rightvalueagainst the members that template asked for, and types what comes back accordinglytruematchesvalueagainst the shape as a template, asking whether the shape can serve what it requestsA template may draw on the full query language, including aggregate transforms and nested expansion. Hold an untrusted caller to what the service will serve:
plainrefuses the aggregate transforms,depthcaps the nesting, andlimitcaps the page.See