Skip to content

Instantly share code, notes, and snippets.

@matthieubosquet
Last active November 29, 2022 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthieubosquet/2f99c557a85a3a7ac994e526c7f089bc to your computer and use it in GitHub Desktop.
Save matthieubosquet/2f99c557a85a3a7ac994e526c7f089bc to your computer and use it in GitHub Desktop.

SHACL Cheat Sheet

The SHACL vocabulary uses prefix sh: and namespace http://www.w3.org/ns/shacl#, it is a W3C recommendation.

The Shapes Constraint Language (SHACL) is a validation framework for RDF graphs.

Table of Contents

Constraint

Constraint components can be used both in property shape and node shape definitions unless stated otherwise. Property & node shapes relate to the concept of value nodes which defines the nodes in the data graph in scope relative to the focus node to run a validator constraint using the shapes graph.

Property shapes have 1 sh:path value and node shapes have none.

Value Type

sh:class
Validates instance or subclass of an rdfs:Class. Can be checked via the SPARQL property path rdf:type/rdfs:subClassOf*. Example possible value: ex:PostalAddress.
sh:datatype
Validates datatype. Example possible values: xsd:string, xsd:boolean, xsd:decimal, xsd:integer, rdf:HTML or any of the XML Schema Built-in Datatypes.
sh:nodeKind
Validates kind of RDF term: IRI, Literal or Blank node. Can be checked via SPARQL using FILTER, isIRI(), isLiteral() and isBlank(). Possible values: sh:IRI, sh:Literal, sh:BlankNode, sh:BlankNodeOrIRI, sh:BlankNodeOrLiteral, sh:IRIOrLiteral.

Cardinality

⚠️ Property shapes only since it refers to the number of nodes that can be reached via the property shapes' property path from the focus node.

sh:minCount
Validates minimum cardinality of property path. Minimum cardinality of 0 is always satisfied. Possible values: literals with datatype xsd:integer.
sh:maxCount
Validates maximum cardinality of property path. Possible values: literals with datatype xsd:integer.

Value Range

Validate value nodes that are comparable via the >, >=, < and <= operators (numeric values).

sh:minExclusive
Validates minimum exclusive value of a literal node. Can be checked via SPARQL using: FILTER ($value > $minExclusive). Possible values: 0, -32, 43.7.
sh:minInclusive
Validates minimum inclusive value of a literal node. Can be checked via SPARQL using: FILTER ($value >= $minInclusive). Possible values: 0, -32, 43.7.
sh:maxExclusive
Validates maximum exclusive value of a literal node. Can be checked via SPARQL using: FILTER ($value < $maxExclusive). Possible values: 0, -32, 43.7.
sh:maxInclusive
Validates maximum inclusive value of a literal node. Can be checked via SPARQL using: FILTER ($value <= $maxInclusive). Possible values: 0, -32, 43.7.

String-based

Validate strings.

sh:minLength
Validates minimum string length. Can be checked via SPARQL using: FILTER (STRLEN(str($value)) >= $minLength). Possible values: literals with datatype xsd:integer.
sh:maxLength
Validates maximum string length. Can be checked via SPARQL using: FILTER (STRLEN(str($value)) <= $maxLength). Possible values: literals with datatype xsd:integer.
sh:pattern
Validates string against a regular expression. Can be checked via SPARQL using: FILTER (!isBlank($value) && IF(bound($flags), regex(str($value), $pattern, $flags), regex(str($value), $pattern))). Possible values: see XPath and XQuery regular expression syntax and regular expression flags.
sh:languageIn
Validates literal language tags. Example value: sh:property [ sh:path ex:prefLabel ; sh:languageIn ( "en" "mi" ); ] (values of ex:prefLabel can be either in English or Māori).
sh:uniqueLang
Validates no pair of value nodes use the same language tag. Possible value: true.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment