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.
npm install @comake/shacl-to-json-schemaThe 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'
//     }
//   }
// }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 type- string
- xsd:integergenerates a JSON Schema with type- integer
- xsd:negativeIntegergenerates a JSON Schema with type- integer
- xsd:positiveIntegergenerates a JSON Schema with type- integer
- xsd:intgenerates a JSON Schema with type- integer
- xsd:decimalgenerates a JSON Schema with type- number
- xsd:floatgenerates a JSON Schema with type- number
- xsd:doublegenerates a JSON Schema with type- number
- xsd:booleangenerates a JSON Schema with type- boolean
- xsd:dateTimegenerates a JSON Schema with type- stringand format- data-time
- xsd:dategenerates a JSON Schema with type- stringand format- date
- xsd:timegenerates a JSON Schema with type- stringand format- time
- rdf:JSONgenerates a JSON Schema with type- string,- number,- boolean,- object, or- array
Supported shacl:nodeKind values:
- sh:BlankNodegenerates a JSON Schema with type- object
- sh:IRIgenerates a JSON Schema with type- string
- sh:Literalgenerates a JSON Schema with type- string,- number,- boolean,- object, or- array
- sh:BlankNodeOrIRIgenerates a JSON Schema with type- stringor- object
- sh:BlankNodeOrLiteralgenerates a JSON Schema with type- string,- number,- boolean,- object, or- array
- sh:IRIOrLiteralgenerates a JSON Schema with type- string,- number,- boolean,- object, or- array
Support for shacl:maxLength, which adds maxLength to the resulting JSON Schema property when the shacl:datatype is one of:
- xsd:string
- xsd:dateTime
- xsd:date
- xsd: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.
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.
Converts SHACL NodeShapes encoded as JSON-LD into JSON Schema.
| Parameter | Type | Required | Description | 
|---|---|---|---|
| shape | JSON | Required | The SHACL NodeShape to convert encoded as JSON-LD. | 
| options | object | A ConversionOptionsobject (see below). | 
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;
}A JSON Schema object