Docs / JavaScript
OATS for JavaScript
The oatschema package bundles the OATS schemas for JavaScript and TypeScript, with no dependencies and no I/O.
npm install oatschema
npmoatschema/oatschema-jsNode.js 22+, browsers, Deno and Bun
Get a schema
import { getSchema, getSchemaJson, SCHEMA_VERSION } from "oatschema";const schema = getSchema("classification"); // a new plain object on every callschema.$id; // "https://oatschema.org/0.8.0/classification"getSchemaJson("classification"); // the file's text, exactly as releasedSCHEMA_VERSION; // "0.8.0", used when you don't pass a version
The schemas are embedded in the module, so the package works the same in Node.js, browsers, Deno, Bun and any bundler. It ships as ESM with TypeScript types, and schema names are a union type, so typos fail at compile time. Each schema is byte-identical to its release on this site.
Validate a record
The package has no validator of its own: getSchema returns a plain object for any JSON Schema draft 2020-12 validator. With Ajv:
npm install ajv ajv-formats
import Ajv2020 from "ajv/dist/2020.js";import addFormats from "ajv-formats";import { getSchema } from "oatschema";const ajv = new Ajv2020({ allErrors: true, strictTypes: false, strictTuples: false, strictRequired: false });ajv.addKeyword({ keyword: "x-license", schemaType: "object" });ajv.addKeyword({ keyword: "x-version", schemaType: "string" });addFormats(ajv);const validate = ajv.compile(getSchema("classification"));validate({ labels: [{ label: "cat", score: 0.92 }] }); // truevalidate({ labels: [{ label: "cat", score: 1.5 }] }); // falsevalidate.errors; // [{ instancePath: "/labels/0/score", message: "must be <= 1", ... }]
x-licenseandx-versionare the only non-standard keywords in OATS. Registering them keeps Ajv's strict mode on for everything else.- The three relaxed options switch off Ajv's style checks, which reject some valid draft 2020-12 patterns that OATS uses, such as
requiredinside anif. - Compile each schema once per Ajv instance and reuse the function. Compiling a second copy with the same
$idthrows; fetch the first withajv.getSchema(schema.$id).
Pin a schema version
Before OATS 1.0, a new release may reject records an older one accepted, or the reverse, so pass the version a record was written for: getSchema("classification", "0.8.0").
versionmust be exact:"0.8.0", never"0.8"or"latest".- Each
$idincludes its version, so several versions can share one Ajv instance. - The package bundles OATS 0.8.0, the first public release, and every later one. For 0.4.0 to 0.7.0, use the Python package, or fetch them from this site.
More on releases in Versions and URLs.
API
getSchema(name, version?) |
The schema as a new object on every call. |
|---|---|
getSchemaJson(name, version?) |
The schema file's text, exactly as released. |
availableVersions() |
Every bundled schema version, oldest first. |
SCHEMA_VERSION |
The default schema version, the newest bundled. |
SCHEMA_NAMES, SchemaName |
The schema names, as an array and a union type. |
UnknownSchemaError |
Thrown for a name that exists in no bundled version. Carries schemaName. |
SchemaLookupError |
Thrown for a version that isn't bundled, or that lacks the schema. Carries schemaName and schemaVersion. |
Package versions
The package has its own semantic version, which never encodes a schema version; read SCHEMA_VERSION instead. Upgrading never removes a schema version, but it can move the default. Before 1.0, a release that changes the default is a minor bump, so npm's default ^0.x.y range keeps your default fixed.
Other languages: Python and Rust. Schema changes belong in oatschema/oats; package issues go to oatschema/oatschema-js.