@neverblink/linkml
v0.18.8
Published
A Scala 3 cross-platform library and CLI for LinkML schema validation and multi-format code generation (Scala, JSON Schema, SHACL, RDFS)
Maintainers
Readme
@neverblink/linkml
JavaScript / TypeScript bindings for ohi LinkML – LinkML schema validation and multi-format code generation (JSON Schema, SHACL, RDFS, Scala).
Installation
npm install @neverblink/linkmlUsage
import { LinkML } from "@neverblink/linkml";
const schema = `
id: https://example.org/my-schema
name: my-schema
prefixes:
linkml: https://w3id.org/linkml/
imports:
- linkml:types
default_range: string
classes:
Person:
tree_root: true
attributes:
name:
age:
range: integer
`;
// Parse the schema once into a reusable handle. The second argument
// maps file names to YAML source for any models referenced
// via LinkML \`imports:\`. Pass {} when there are none.
const { view, report } = LinkML.loadFromString(schema, {});
if (!view) throw new Error(JSON.stringify(report, null, 2)); // fatal problems: nothing to generate from
// Then run any generator on the loaded schema.
const jsonSchema = view.jsonSchema();
console.log(jsonSchema);
const shacl = view.shacl({ format: "nt" });
console.log(shacl);Loading
There are two ways to load a schema into a SchemaView handle:
loadFromString(schema, imports)– start from the schema's YAML text. Imported models must be provided inimports(filename → YAML).loadFromPath(path, imports)– start from a path intoimports, which also holds the root schema itself. The root schema is read fromimports[path]. Because the root is tracked from the start of import resolution, this variant is immune to cyclic imports that reference the root schema back.
Keys of imports are import paths as seen from the root schema. The .yaml and .yml extensions
are optional and interchangeable, and each imported schema is loaded only once, even when it is
reached through different relative paths.
Both take an optional fourth argument, importMap, which maps an import to the key of imports
to load it from. Use it when a schema imports another one by URI or CURIE. Keys are full URIs or
paths, not CURIEs. A key that ends with / or # matches every import that starts with it. To use
a JSON file, pass JSON.parse of its text. See
Import maps.
// loadFromPath: the root lives in `imports` under its own path.
const { view } = LinkML.loadFromPath("model.yaml", {
"model.yaml": schema,
"person.yaml": personSchema, // referenced via `imports: - person`
});
// importMap: `units:core` (or `https://example.org/units/core`) is read from "deps/units.yaml".
const { view: withUnits } = LinkML.loadFromPath(
"model.yaml",
{ "model.yaml": schema, "deps/units.yaml": unitsSchema },
true,
{ "https://example.org/units/core": "deps/units.yaml" },
);Available functions
Load a schema into a SchemaView (see above), then call any generator on it. Every generator
takes an optional options object, such as { pruning_mode: "tree_root", format: "json" }. The
generators, their options and the defaults are the functions of
model/converter. Their types, such as LinkMlGeneratorOptions, are in index.d.ts.
| Method of SchemaView | Returns | Notes |
|------------------------|---------|-------|
| jsonSchema(options?) | string | JSON Schema |
| shacl(options?) | string | SHACL shapes, ttl (default) or nt |
| rdfs(options?) | string | RDFS, ttl (default) or nt |
| owl(options?) | string | OWL 2 ontology, ttl (default) or nt |
| linkml(options?) | string | derived/pruned LinkML schema |
| scala(options?) | Files | filename → generated Scala |
| frictionless(options?) | Files | filename → Frictionless data package (datapackage.json and schemas/*.json) |
| graphql(options?) | string | GraphQL |
| typescript(options?) | string | TypeScript types for the JSON data (no runtime code) |
| pydantic(options?) | string | Python classes (pydantic) that load and dump the JSON data |
| java(options?) | Files | filename → Java classes (Jackson) that load and dump the JSON data |
| rust(options?) | Files | path → the files of a Rust crate, with serde and PyO3 support |
| erDiagram(options?) | string | Mermaid entity relationship diagram |
| docs(options?) | Files | filename → Markdown documentation (index.md and a page per element) |
| ossie(options?) | string | Apache Ossie ontology, yaml (default) or json |
| translation(options?) | string | Translation dictionary for generator outputs |
| stats(options?) | SchemaStats | counts of the elements, their documentation, mappings, languages and imports |
| serialize(options?) | string | the schema as it was loaded, without its imports merged in, yaml (default) or json |
The functions of LinkML:
| Function | Returns | Notes |
|------------------------------------------------------------------------------|--------------------------|--------------------------------------------------------------------------------|
| loadFromString(schema, imports, inferMessages?, importMap?) | LoadResult | parse from YAML text; { view?, report } |
| loadFromPath(path, imports, inferMessages?, importMap?) | LoadResult | parse from a path in imports; cycle-safe for the root |
| lint(view, inferMessages?) | SchemaValidationReport | validation report, typed by issue kind (see below) |
| buildInfo() | object | BuildInfo (JSON) – version and build metadata |
| loadFromOssie(document, options?) | LoadResult | load the LinkML schema of an Apache Ossie ontology; { view?, report, notImported } |
| loadFromOwl(document, options?) | LoadResult | load the LinkML schema of an OWL ontology in Turtle or N-Triples (see the OWL guide) |
| fromOssie(document, options?) | ImportResult | deprecated: use loadFromOssie and serialize |
| fromOwl(document, options?) | ImportResult | deprecated: use loadFromOwl and serialize |
The loaders, loadFromOssie and loadFromOwl, take the document to import, then an options object
like the generators, such as { schema_id: "https://example.org/x" }. Their options are in
model/converter too, except format and full_uris, which only shape the text of the schema:
pass those to view.serialize(). Both return a LoadResult like loadFromString, with what could
not be imported in notImported: as one line for each kind in warnings, and as an ImportReport
with one entry for each thing in report. Each warning's warning_type names its kind, so a
switch on it narrows the warning to that kind.
fromOssie and fromOwl, which return { schema, warnings, report } with the schema as text, are
deprecated. Use the loaders and view.serialize() instead.
See index.d.ts for full type signatures. The validation report that lint and
loadFromString/loadFromPath return is fully typed: each issue's issue_type names its kind
(e.g. "UnknownReference"), and checking it narrows the issue to that kind's fields. For how to use the generated TypeScript,
see the TypeScript guide, for pydantic
the Pydantic guide, for java
the Java guide, and for rust
the Rust guide. For owl and loadFromOwl, see
the OWL generator and the OWL importer.
Errors
A generator or importer that can't convert a schema or ontology, for a reason you can fix, throws
an Error with an issue property. It is a SchemaConversionError issue, in the same shape as the
issues of a validation report, and its message says what is wrong. linkml, ossie, owl
and the loaders of the importers can throw it. Any other error is a wrong argument, such as an unknown
format or metadata profile, or a bug.
import { LinkML } from "@neverblink/linkml";
try {
LinkML.loadFromOssie("- not\n- an ontology\n");
throw new Error("loadFromOssie should have failed");
} catch (e) {
if (e.issue?.issue_type !== "SchemaConversionError") throw e; // a wrong argument or a bug
console.log(e.issue.message); // Not a readable Ossie ontology: ...
}Browser use
The module works in Node.js as-is. In the browser it references process.cwd
once, so provide a minimal shim before importing:
<script>
var process = { cwd: () => "/" };
</script>License
Apache-2.0 © NeverBlink
