This utility generates a JSON Schema document from a SHACL specification. Detailed documentation is available below.
The algorithm follow these steps to generate the JSON Schema:
@context
since we are targeting JSON-LDhttps://json-schema.org/draft/2020-12/schema
container_language
declaration is always added with a patternProperty
declaration with properties
being language codes to deal with "@container": "language"
properties.
title
is populated from an owl:Ontology dct:title
or rdfs:label
version
is populated from an owl:Ontology owl:versionInfo
description
is populated from an owl:Ontology dct:description
Each node shape with at least one non-deactivated property shape is turned into an object schema in the $defs
section of the schema, with the URI
local name as the name of the schema. Node shapes with no non-deactivated property shapes are turned into string schemas being simple iri-reference
.
title
is populated from the node shape rdfs:label
description
is populated from the node shape rdfs:comment
id
propertysh:pattern
associated to the node shape, it is turned into a pattern
constraint on the id keyskos:example
associated to the node shape, it is turned into an example
constraint on the id keyadditionalProperties
is set to false, except if the flag "never set additional properties" is set.Non-deactivated property shapes are processed this way:
shacl-play:shortName
annotation, otherwise the local name of the property in sh:path
is used as the JSON keytitle
is populated from the property shape sh:name
description
is populated from the property shape sh:description
sh:hasValue
, a const
schema is created with the value.
If a JSON-LD context was provided, an attempt is made to simplify the value to its actual mapping from the context, either because it is directly declared in the vocab, or because a prefix is declared.
sh:in
, an enum
schema is created with the list of possible values.
If a JSON-LD context was provided, an attempt is made to simplify the list of possible values to their actual mapping from the context, either because they are directly declared in the vocab, or because a prefix is declared.sh:pattern
, a string schema is generated with a pattern constraint.
If a JSON-LD context was provided, the pattern constraint is "reduced" so that patterns matching complete IRIs may match only the end of IRI if they are shortened due to @base in the contextsh:node
, then :
shacl-play:embed shacl-play:EmbedNever
, then a string schema with format iri-reference
is generated$ref
schema with a reference to one of the schemas in the #/$defs
section@container : @set
.
If the compaction test returns an array, then an array will be declared in the output schema
sh:qualifiedMaxCount
and it is > 1, then wrap the generated schema into an array
schemash:maxCount
or a sh:maxCount
that is > 1, then wrap the generated schema into an array
schemash:pattern
, then turn it into a string schema with a pattern
constraint.sh:datatype
, or an sh:qualifiedValueShape
that has an sh:datatype
then:
container_language
in the #/$defs
sectionsh:nodeKind
pointing to sh:IRI, generate a string schema of format iri-reference
.empty
schemash:minCount
, or the sh:qualifiedMinCount
of the property is > 0, the JSON key is added to the list of requiredProperties
of the schemaWith the provided root node shape IRI, create a reference to the corresponding schema from the #/$defs
section.