{"id":20546299,"url":"https://github.com/comake/shacl-to-json-schema","last_synced_at":"2025-08-23T04:17:51.159Z","repository":{"id":180208405,"uuid":"663635220","full_name":"comake/shacl-to-json-schema","owner":"comake","description":null,"archived":false,"fork":false,"pushed_at":"2023-10-19T10:49:10.000Z","size":587,"stargazers_count":4,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-11-16T04:25:05.876Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/comake.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-07-07T18:56:38.000Z","updated_at":"2024-10-10T23:41:34.000Z","dependencies_parsed_at":null,"dependency_job_id":"6c8a8ebb-0431-442a-8446-13c2391eada8","html_url":"https://github.com/comake/shacl-to-json-schema","commit_stats":null,"previous_names":["comake/shacl-to-json-schema"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/comake%2Fshacl-to-json-schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/comake%2Fshacl-to-json-schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/comake%2Fshacl-to-json-schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/comake%2Fshacl-to-json-schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/comake","download_url":"https://codeload.github.com/comake/shacl-to-json-schema/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234260511,"owners_count":18804402,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-16T02:00:46.225Z","updated_at":"2025-01-16T19:41:04.921Z","avatar_url":"https://github.com/comake.png","language":"TypeScript","funding_links":[],"categories":["Shape Conversion Tools"],"sub_categories":[],"readme":"# SHACL To JSON Schema\n\nThis 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.\n\n## Install\n\n```shell\nnpm install @comake/shacl-to-json-schema\n```\n\n## Usage\n\nThe `nodeShapeToJSONSchema` function exported by this library supports converting SHACL NodeShapes encoded as JSON-LD into JSON Schema.\n\nFor example, using Typescript:\n```ts\nimport { nodeShapeToJSONSchema } from '@comake/shacl-to-json-schema';\n\nconst nodeShape = {\n  '@type': 'shacl:NodeShape',\n  'http://www.w3.org/ns/shacl#targetClass': 'https://example.com/Class',\n  'http://www.w3.org/ns/shacl#property': [\n    {\n      'http://www.w3.org/ns/shacl#datatype': {\n        '@id': 'http://www.w3.org/2001/XMLSchema#string'\n      },\n      'http://www.w3.org/ns/shacl#maxCount': {\n        '@value': 1,\n        '@type': 'http://www.w3.org/2001/XMLSchema#integer'\n      },\n      'http://www.w3.org/ns/shacl#name': {\n        '@value': 'field',\n        '@type': 'http://www.w3.org/2001/XMLSchema#string'\n      }\n      'http://www.w3.org/ns/shacl#path': {\n        '@id': 'https://example.com/field'\n      }\n    }\n  ]\n};\n\nconst schema = nodeShapeToJSONSchema(nodeShape);\nconsole.log(schema);\n\n// Output:\n// {\n//   type: 'object',\n//   properties: {\n//     'https://example.com/field': { \n//       type: 'string'\n//     }\n//   }\n// }\n```\n\nBy 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`:\n\n```ts\nconst schema = nodeShapeToJSONSchema(nodeShape, { useNames: true });\nconsole.log(schema);\n\n// Output:\n// {\n//   type: 'object',\n//   properties: {\n//     field: {   \u003c---- Uses \"field\" instead of \"https://example.com/field\"\n//       type: 'string'\n//     }\n//   }\n// }\n```\n\n\n\n## Support\n\nCurrently this library only supports properties that include a `shacl:datatype`, `shacl:node`, or `shacl:nodeKind`.\n\nSupported `shacl:datatype` values:\n- `xsd:string` generates a JSON Schema with type `string`\n- `xsd:integer` generates a JSON Schema with type `integer`\n- `xsd:negativeInteger` generates a JSON Schema with type `integer`\n- `xsd:positiveInteger` generates a JSON Schema with type `integer`\n- `xsd:int` generates a JSON Schema with type `integer`\n- `xsd:decimal` generates a JSON Schema with type `number`\n- `xsd:float` generates a JSON Schema with type `number`\n- `xsd:double` generates a JSON Schema with type `number`\n- `xsd:boolean` generates a JSON Schema with type `boolean`\n- `xsd:dateTime` generates a JSON Schema with type `string` and format `data-time`\n- `xsd:date` generates a JSON Schema with type `string` and format `date`\n- `xsd:time` generates a JSON Schema with type `string` and format `time`\n- `rdf:JSON` generates a JSON Schema with type `string`, `number`, `boolean`, `object`, or `array`\n\nSupported `shacl:nodeKind` values:\n- `sh:BlankNode` generates a JSON Schema with type `object`\n- `sh:IRI` generates a JSON Schema with type `string`\n- `sh:Literal` generates a JSON Schema with type `string`, `number`, `boolean`, `object`, or `array`\n- `sh:BlankNodeOrIRI` generates a JSON Schema with type `string` or `object`\n- `sh:BlankNodeOrLiteral` generates a JSON Schema with type `string`, `number`, `boolean`, `object`, or `array`\n- `sh:IRIOrLiteral` generates a JSON Schema with type `string`, `number`, `boolean`, `object`, or `array`\n\nSupport for `shacl:maxLength`, which adds `maxLength` to the resulting JSON Schema property when the `shacl:datatype` is one of:\n- `xsd:string`\n- `xsd:dateTime`\n- `xsd:date`\n- `xsd:time`\n\nSupport for `shacl:in` for all datatypes, which produces a JSON Schema `enum`\n\nSupport for `shacl:minExclusive`, `shacl:minInclusive`, `shacl:maxExclusive`, `shacl:maxInclusive` for all numeric datatypes\n\nOptional 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.\n\nSupport for `shacl:minCount` and `shacl:maxCount`:\n- If `shacl:maxCount` is unset or greater than 1, the property will be treated as an array. If so, `shacl:maxCount` and `shacl:minCount` values are used to set `maxItems` and `minItems` in the resulting JSON Schema.\n- If `shacl:minCount` is greater than 0, the property will be required in the resulting JSON Schema\n\nSupport for `shacl:closed`. When true, the resulting JSON Schema will have `additionalProperties` set to false.\n\n## Not Supported\n\nNo support for SHACL predicates `shacl:lessThanOrEquals`, `shacl:lessThan`, `shacl:flag`, `shacl:equal`, `shacl:class`, and `shacl:languageIn`.\n\nThere 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.\n\n## API\n\n### nodeShapeToJSONSchema\n\nConverts SHACL NodeShapes encoded as JSON-LD into JSON Schema.\n\n#### Parameters\n\n| Parameter | Type | Required | Description |\n| :--- | :--- | :--- | :--- |\n| `shape` | `JSON` | Required | The SHACL NodeShape to convert encoded as JSON-LD. |\n| `options` | `object` |  | A `ConversionOptions` object (see below).  |\n\n#### ConversionOptions\n\nThese are the available options to configure the behavior of the conversion (in Typescript):\n\n```ts\ninterface ConversionOptions {\n  /**\n   * When true, the names of fields in the generated JSON Schema \n   * will use the shacl:name of each property instead of the shacl:path uri\n   */\n  useNames?: boolean;\n  /**\n   * When true, each field will include a title equal \n   * to it's corresponding property's shacl:name\n   */\n  addTitles?: boolean;\n  /**\n   * When true, each field will include a description equal \n   * to it's corresponding property's shacl:description\n   */\n  addDescriptions?: boolean;\n}\n```\n\n#### Return Value\n\nA JSON Schema object","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcomake%2Fshacl-to-json-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcomake%2Fshacl-to-json-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcomake%2Fshacl-to-json-schema/lists"}