@comake/shacl-to-json-schema v1.0.3
SHACL To JSON Schema
This is a simple library to translate SHACL NodeShapes into JSON Schemas. It does not support the full SHACL vocabulary, only those fields which can be applied to JSON Schema.
Install
npm install @comake/shacl-to-json-schemaUsage
The nodeShapeToJSONSchema function exported by this library supports converting SHACL NodeShapes encoded as JSON-LD into JSON Schema.
For example, using Typescript:
import { nodeShapeToJSONSchema } from '@comake/shacl-to-json-schema';
const nodeShape = {
'@type': 'shacl:NodeShape',
'http://www.w3.org/ns/shacl#targetClass': 'https://example.com/Class',
'http://www.w3.org/ns/shacl#property': [
{
'http://www.w3.org/ns/shacl#datatype': {
'@id': 'http://www.w3.org/2001/XMLSchema#string'
},
'http://www.w3.org/ns/shacl#maxCount': {
'@value': 1,
'@type': 'http://www.w3.org/2001/XMLSchema#integer'
},
'http://www.w3.org/ns/shacl#name': {
'@value': 'field',
'@type': 'http://www.w3.org/2001/XMLSchema#string'
}
'http://www.w3.org/ns/shacl#path': {
'@id': 'https://example.com/field'
}
}
]
};
const schema = nodeShapeToJSONSchema(nodeShape);
console.log(schema);
// Output:
// {
// type: 'object',
// properties: {
// 'https://example.com/field': {
// type: 'string'
// }
// }
// }By default the shacl:path of each property will be used as the property key in the resulting JSON Schema. If instead you need the generated JSON Schema to use the shacl:name field as property keys, set the useNames option to true:
const schema = nodeShapeToJSONSchema(nodeShape, { useNames: true });
console.log(schema);
// Output:
// {
// type: 'object',
// properties: {
// field: { <---- Uses "field" instead of "https://example.com/field"
// type: 'string'
// }
// }
// }Support
Currently this library only supports properties that include a shacl:datatype, shacl:node, or shacl:nodeKind.
Supported shacl:datatype values:
xsd:stringgenerates a JSON Schema with typestringxsd:integergenerates a JSON Schema with typeintegerxsd:negativeIntegergenerates a JSON Schema with typeintegerxsd:positiveIntegergenerates a JSON Schema with typeintegerxsd:intgenerates a JSON Schema with typeintegerxsd:decimalgenerates a JSON Schema with typenumberxsd:floatgenerates a JSON Schema with typenumberxsd:doublegenerates a JSON Schema with typenumberxsd:booleangenerates a JSON Schema with typebooleanxsd:dateTimegenerates a JSON Schema with typestringand formatdata-timexsd:dategenerates a JSON Schema with typestringand formatdatexsd:timegenerates a JSON Schema with typestringand formattimerdf:JSONgenerates a JSON Schema with typestring,number,boolean,object, orarray
Supported shacl:nodeKind values:
sh:BlankNodegenerates a JSON Schema with typeobjectsh:IRIgenerates a JSON Schema with typestringsh:Literalgenerates a JSON Schema with typestring,number,boolean,object, orarraysh:BlankNodeOrIRIgenerates a JSON Schema with typestringorobjectsh:BlankNodeOrLiteralgenerates a JSON Schema with typestring,number,boolean,object, orarraysh:IRIOrLiteralgenerates a JSON Schema with typestring,number,boolean,object, orarray
Support for shacl:maxLength, which adds maxLength to the resulting JSON Schema property when the shacl:datatype is one of:
xsd:stringxsd:dateTimexsd:datexsd:time
Support for shacl:in for all datatypes, which produces a JSON Schema enum
Support for shacl:minExclusive, shacl:minInclusive, shacl:maxExclusive, shacl:maxInclusive for all numeric datatypes
Optional support for shacl:name and shacl:description when the options addTitles or addDescriptions are set to true, respectively. Doing so will add JSON Schema title and description to the resulting properties.
Support for shacl:minCount and shacl:maxCount:
- If
shacl:maxCountis unset or greater than 1, the property will be treated as an array. If so,shacl:maxCountandshacl:minCountvalues are used to setmaxItemsandminItemsin the resulting JSON Schema. - If
shacl:minCountis greater than 0, the property will be required in the resulting JSON Schema
Support for shacl:closed. When true, the resulting JSON Schema will have additionalProperties set to false.
Not Supported
No support for SHACL predicates shacl:lessThanOrEquals, shacl:lessThan, shacl:flag, shacl:equal, shacl:class, and shacl:languageIn.
There is also currently no support for complex SHACL Property Paths including Sequence Paths, Alternative Paths, Inverse Paths, Zero-Or-More Paths, One-Or-More Paths, and Zero-Or-One Paths.
API
nodeShapeToJSONSchema
Converts SHACL NodeShapes encoded as JSON-LD into JSON Schema.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
shape | JSON | Required | The SHACL NodeShape to convert encoded as JSON-LD. |
options | object | A ConversionOptions object (see below). |
ConversionOptions
These are the available options to configure the behavior of the conversion (in Typescript):
interface ConversionOptions {
/**
* When true, the names of fields in the generated JSON Schema
* will use the shacl:name of each property instead of the shacl:path uri
*/
useNames?: boolean;
/**
* When true, each field will include a title equal
* to it's corresponding property's shacl:name
*/
addTitles?: boolean;
/**
* When true, each field will include a description equal
* to it's corresponding property's shacl:description
*/
addDescriptions?: boolean;
}Return Value
A JSON Schema object