This document defines the SHACL Shapes Constraint Language, a language for validating RDF graphs against a set of conditions. These conditions are provided as shapes and other constructs expressed in the form of an RDF graph. RDF graphs that are used in this manner are called "shapes graphs" in SHACL and the RDF graphs that are validated against a shapes graph are called "data graphs". As SHACL shape graphs are used to validate that data graphs satisfy a set of conditions they can also be viewed as a description of the data graphs that do satisfy these conditions. Such descriptions may be used for a variety of purposes beside validation, including user interface building, code generation and data integration.

Revision History

The detailed list of changes and their diffs can be found in the Git repository.

Document Outline

The introduction includes a Terminology section.

The sections 2 - 4 cover the SHACL Core language and may be read independently from the later sections.

The sections 5 onwards are about the additional features that SHACL Full has in addition to the Core language. These advanced features include SPARQL-based constraints, constraint components, targets and functions.

The examples in this document use Turtle [[!turtle]]. The reader should be familiar with basic RDF concepts [[!rdf11-concepts]] such as triples and, for the advanced concepts of SHACL, with SPARQL [[!sparql11-overview]].

Introduction

This document specifies SHACL (Shapes Constraint Language), a language for describing and validating RDF graphs. This section introduces SHACL with an overview of the key terminology and an example to illustrate basic concepts.

Terminology

Throughout this document, the following terminology is used.

Basic RDF Terminology
This document uses the terms RDF graph, RDF triple, IRI, literal, blank node, node of an RDF graph, RDF term, and subject, predicate, and object of RDF triples, and datatype as defined in RDF 1.1 Concepts and Abstract Syntax [[!rdf11-concepts]].
Property Value and Path
An RDF term n has a value v for property p in an RDF graph if there is an RDF triple in the graph with subject n, predicate p, and object v. (In this document, the verbs specify or declare are sometimes used to express the fact that an RDF term has property values in a graph.) A property path is a possible route in a graph between two graph nodes. SHACL supports a subset of the property path syntax from SPARQL 1.1, including inverse paths and sequences. The values of a property path path for a given RDF term s are the distinct bindings produced by a SPARQL processor for the variable o from a TriplesBlock of the form s path ?o.
SHACL Lists
A SHACL list is an IRI or a blank node that is either rdf:nil, or has exactly one value m for the property rdf:first and exactly one value Ln for the property rdf:rest that is also a SHACL list. When the list is rdf:nil, the list has no members (empty list) and cannot have any value for either rdf:first or rdf:rest. The members of a non-empty SHACL list are m followed by the members of the list Ln. A list L is defined as recursive when values of the path rdf:rest+ starting from L, contain L. A SHACL list cannot be recursive.
Binding, Solution
In this document, a binding is a pair (variable, RDF term), consistent with the term's use in SPARQL. A solution is a set of bindings, one row in the body of the result table of a SPARQL query. Variables are not required to be bound in a solution. The results table is a SolutionSequence, a list of solutions, possibly unordered.
SHACL Subclass, SHACL superclass
A node Sub in an RDF graph is a SHACL subclass of another node Super in the graph if there is a sequence of triples in the graph each with predicate rdfs:subClassOf such that the subject of the first triple is Sub, the object of the last triple is Super, and the object of each triple except the last is the subject of the next. If Sub is a SHACL subclass of Super in an RDF graph then Super is a SHACL superclass of Sub in the graph.
SHACL Type
The SHACL types of an RDF term in an RDF graph are its values for rdf:type in the graph as well as the SHACL superclasses of these values in the graph.
SHACL Class
Nodes in an RDF graph that are subclasses, superclasses, or types of nodes in the graph are often referred to as SHACL class.
SHACL Class Instance
A node in an RDF graph is a SHACL instance of a SHACL class in the graph if one of its SHACL types is the class.
Expected Type
In a shapes graph, the non-literal values of a property or a property path can have an expected type. These nodes are treated as RDFS instances of specific classes, even when these nodes are not SHACL instances of these classes. For example, the objects of triples with sh:shape as predicate have sh:Shape as expected type and there does not need to be a triple with the object node as the subject, rdf:type as predicate and sh:Shape as object in the shapes graph.
SHACL Core and SHACL Full
The SHACL specification is divided into SHACL Core and SHACL Full. SHACL Core consists of frequently needed features for the representation of shapes, constraints and targets. All SHACL implementations MUST at least cover the Core. SHACL Full consists of all features of SHACL Core plus a collection of advanced features including SPARQL-based constraints, extension mechanisms to declare new constraint components and target types, user-defined functions and derived properties.

Document Conventions

Within this document, the following namespace prefix bindings are used:

Prefix Namespace
rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#
rdfs: http://www.w3.org/2000/01/rdf-schema#
sh: http://www.w3.org/ns/shacl#
xsd: http://www.w3.org/2001/XMLSchema#
ex: http://example.com/ns#

Note that the URI of the graph defining the SHACL vocabulary itself is equivalent to the namespace above, i.e. it includes the #. References to the SHACL vocabulary, e.g. via owl:imports should include the #.

The Turtle serialization of the SHACL vocabulary will be uploaded to web URL of the graph that it represents.

Throughout the document, color-coded boxes containing RDF graphs in Turtle will appear. These fragments of Turtle documents use the prefix bindings given above.

# This box represents an input shapes graph

# Triples that can be omitted are marked as grey e.g.
<s> <p> <o> .
# This box represents an input data graph.
# When highlighting is used in the examples:

# Elements highlighted in blue are focus nodes
ex:Bob a ex:Person .

# Elements highlighted in red are focus nodes that fail validation
ex:Alice a ex:Person .
# This box represents an output results graph

SHACL Definitions appear in blue boxes:

SPARQL or TEXTUAL DEFINITIONS
# This box contains SPARQL or textual definitions. 

TODO: needs more work.

This specification describes conformance criteria for:

For the SHACL Core and SHACL Full language, this specification describes the expected structure of both languages. For example, each value of sh:targetClass is an IRI states that the values of sh:targetClass are expected to be IRIs. For the SHACL Core and SHACL Full processors, this specification describes the expected behavior of both processors when they consume expected input. There are cases where processors have well defined behavior on unexpected input. For example, The SHACL Full processor MUST produce a failure if the resulting SPARQL query string cannot be parsed into a valid SPARQL 1.1 query. However, this specification cannot prescribe all unexpected cases.

TODO: link to test cases.

SHACL Example

The following example data graph contains three SHACL instances of the class ex:Person.

ex:Alice
	a ex:Person ;
	ex:child ex:Calvin ;
	ex:ssn "987-65-432A" .
  
ex:Bob
	a ex:Person ;
	ex:child ex:Calvin ;
	ex:ssn "123-45-6789" ;
	ex:ssn "124-35-6789" .
  
ex:Calvin
	a ex:Person ;
	ex:school ex:TrinityAnglicanSchool .

The following conditions are shown in the example:

The aforementioned conditions can be represented as constraints in the following shapes graph:

ex:PersonShape
	a sh:Shape ;
	sh:targetClass ex:Person ;    # Applies to all persons
	sh:property [
		sh:predicate ex:ssn ;     # This constraint is about the values of the ex:ssn property
		sh:maxCount 1 ;
		sh:datatype xsd:string ;
		sh:pattern "^\\d{3}-\\d{2}-\\d{4}$" ;
	] ;
	sh:property [
		sh:predicate ex:child ;
		sh:class ex:Person ;
		sh:nodeKind sh:IRI ;
	] ;
	sh:property [
		rdfs:comment "A person's parents are represented via ex:child used in the inverse direction." ;
		sh:path [ sh:inversePath ex:child ] ;
		sh:name "parent" ;
		sh:maxCount 2 ;
	] ;
	sh:closed true ;
	sh:ignoredProperties ( rdf:type ) .

We can use the shape declaration above to illustrate some of the key terminology used by SHACL. The focus nodes for the shape ex:PersonShape are all SHACL instances of the class ex:Person. These focus nodes are the targets of the shape and are specified using the property sh:targetClass. The shape has three property constraints with the property sh:property, one of which uses a path expression. The shape itself is also a constraint on the focus nodes using the parameters sh:closed and sh:ignoredProperties.

Some of the property constraints specify parameters from multiple constraint components in order to restrict multiple aspects of the property values. For example, in the property constraint for ex:ssn, parameters from three constraint components are used. The parameters of these constraint components are sh:datatype, sh:pattern and sh:maxCount. For each focus node the property values of ex:ssn will be validated against all three components. The constraint on the inverse property values of sh:child uses only one constraint component identified by the sh:maxCount parameter. Note that this constraint uses the non-validating property sh:name to suggest a human-readable name for the property when used in the inverse direction.

SHACL validation based on the provided data graph and shapes graph would produce the following validation report. See the section Validation Report for details on the format.

[] a sh:ValidationReport ;
	sh:conforms "false"^^xsd:boolean ;
	sh:result
	[  	a sh:ValidationResult ;
		sh:resultSeverity sh:Violation ;
		sh:focusNode ex:Alice ;
		sh:resultPath ex:ssn ;
		sh:value "987-65-432A" ;
		sh:sourceConstraintComponent sh:RegexConstraintComponent ;
		sh:sourceShape ex:PersonShape ;
	] ,
	[	a sh:ValidationResult ;
		sh:resultSeverity sh:Violation ;
		sh:focusNode ex:Bob ;
		sh:resultPath ex:ssn ;
		sh:sourceConstraintComponent sh:MaxCountConstraintComponent ;
		sh:sourceShape ex:PersonShape ;
	] ,
	[	a sh:ValidationResult ;
		sh:resultSeverity sh:Violation ;
		sh:focusNode ex:Calvin ;
		sh:resultPath ex:school ;
		sh:value ex:TrinityAnglicanSchool ;
		sh:sourceConstraintComponent sh:ClosedConstraintComponent ;
		sh:sourceShape ex:PersonShape ;
	] .

The validation results are enclosed in a validation report. The first validation result is produced because ex:Alice has a value for ex:ssn that does not match the regular expression specified by the property sh:regex. The second validation result is produced because ex:Bob has more than the permitted number of values for the property ex:ssn as specified by the sh:maxCount of 1. The third validation result is produced because the shape ex:PersonShape has the the property sh:closed set to true but ex:Calvin uses the property ex:school which is neither one of the predicates from any of the property constraints of the shape, nor one of the properties listed using sh:ignoredProperties.

Relationship between SHACL and RDFS inferencing

SHACL uses the RDF and RDFS vocabularies, but full RDFS inferencing is not required. However, SHACL processors MUST identify SHACL instances of a class both in the data graph and the shapes graph without modifying either graph during the validation process. Furthermore, SHACL processors MAY operate on RDF graphs that include entailments - either pre-computed before being submitted to a SHACL processor or performed on the fly as part of SHACL processing. To support processing of entailments, SHACL includes the property sh:entailment to indicate what inferencing is required by a given shapes graph. SHACL implementations MAY, but are not required to, support entailment regimes.

Relationship between SHACL and SPARQL

This specification uses parts of SPARQL 1.1 in the normative definition of the semantics of the SHACL Core constraints and targets. However, SPARQL is not required for the implementation of the SHACL Core language.

SPARQL variables using the $ marker represent external bindings that are pre-bound or, in the case of $PATH, substituted in the SPARQL query before execution.

In some places, the specification assumes that the provided SPARQL engines are preserving the identity of blank nodes, so that repeated invocations of queries consistently identify and communicate the same blank nodes.

The definition of some constraints requires or is simplified through access to the shapes graph during query execution. SHACL Full processors MAY pre-bind the variable shapesGraph to provide access to the shapes graph. Access to the shapes graph is not a requirement for supporting the SHACL Core language. The variable shapesGraph can also be used in SPARQL-based constraints and SPARQL-based constraint components. However, such constraints may not be interoperable across different SHACL Full processors or not applicable to remote RDF datasets.

SHACL additionally introduces mechanisms to specify constraints, targets, derived values and new functions in SPARQL. Implementations that cover only the SHACL Core features are not required to implement these mechanisms.

The button below can be used to show or hide the SPARQL definitions.

Part 1: SHACL Core

Shapes and Constraints

The following introduction is non-normative.

The following informal diagram provides an overview of some of the key classes in the SHACL vocabulary. Each box represents a class. The content of the boxes under the class name lists some of the properties that instances of these classes may have, together with their value types. The arrows indicate rdfs:subClassOf triples.

sh:deactivated : xsd:boolean
sh:message : xsd:string or rdf:langString
sh:severity : sh:Severity
sh:property : sh:PropertyConstraint
sh:targetClass : rdfs:Class
sh:targetObjectsOf : rdf:Property
sh:targetSubjectsOf : rdf:Property
sh:closed : xsd:boolean
sh:not : sh:Shape
sh:or : rdf:List
sh:predicate or sh:path : rdfs:Resource
sh:name : xsd:string or rdf:langString
sh:description : xsd:string or rdf:langString
sh:group : sh:PropertyGroup

The Turtle serialization of the SHACL vocabulary contains the complete SHACL vocabulary.

Shapes

A shape is a node in a shapes graph that fulfills any of the following conditions:

A shape can have zero or more targets, constraints and parameters of constraint components that specify how a set of RDF terms from the data graph are validated against the shape. Shapes can also have non-validating properties, such as labels and comments.

Focus Nodes

An RDF term that is validated against a shape using the triples from a data graph is called a focus node.

The set of focus nodes for a shape may be identified as follows:

Targets

A target provides one way to specify focus nodes for a shape.

SHACL Core includes four kinds of targets: node targets, class-based targets, subjects-of targets, and objects-of targets. The SHACL Full language additionally includes an advanced general target mechanism based on SPARQL.

When multiple targets are declared by a shape, the target of a shape is the union of all RDF terms produced by these individual targets. RDF terms specified by targets are not required to exist as nodes in the data graph.

Targets of a shape are ignored whenever a focus node is provided directly as input to the validation process for that shape. This includes the cases where the shape is a value of one of the shape-expecting constraint parameters (such as sh:shape) and a focus node is determined during the validation of the corresponding constraint component (such as sh:ShapeConstraintComponent).

Node targets (sh:targetNode)

A node target is specified using the sh:targetNode predicate. Each value of sh:targetNode is either an IRI or a literal. Each value of a node target identifies an RDF term to be validated against the shape that is the subject of the sh:targetNode triple.

With the example data below, only ex:Alice is the target of the provided shape:

ex:PersonShape
	a sh:Shape ;
	sh:targetNode ex:Alice .
ex:Alice a ex:Person .
ex:Bob a ex:Person .

The following SPARQL query specifies the semantics of node targets. The variable targetNode is assumed to be pre-bound to the given value of sh:targetNode. All bindings of the variable this from the solution become focus nodes.

SPARQL DEFINITION
SELECT DISTINCT ?this    # ?this is the focus node
WHERE {
	BIND ($targetNode AS ?this)    # $targetnote is pre-bound to ex:Alice
}

Class-based Targets (sh:targetClass)

A class target is specified with the sh:targetClass predicate. Each value of sh:targetClass is an IRI. For every value c of a class target, all SHACL instances of c in the data graph are validated against the subject of the sh:targetClass triple.

ex:PersonShape
	a sh:Shape ;
	sh:targetClass ex:Person .
ex:Alice a ex:Person .
ex:Bob a ex:Person .
ex:NewYork a ex:Place .

In this example, only ex:Alice and ex:Bob are focus nodes. Note that, according to the SHACL instance definition, all the rdfs:subClassOf declarations needed to walk the class hierarchy need to exist in the data graph. However, the ex:Person a rdfs:Class triple is not required to exist in either graphs.

In the following example, the selected focus node is only ex:Who.

ex:Doctor rdfs:subClassOf ex:Person .
ex:Who a ex:Doctor .
ex:House a ex:Nephrologist .

The following SPARQL query specifies the semantics of class targets. The variable targetClass is assumed to be pre-bound to the given value of sh:targetClass. All bindings of the variable this from the solution become focus nodes.

SPARQL DEFINITION
SELECT DISTINCT ?this    # ?this is the focus node
WHERE {
	?this rdf:type/rdfs:subClassOf* $targetClass .    # $targetClass is pre-bound to ex:Person
}

Implicit Class Targets

When, in the shapes graph, a shape is a SHACL instance of both sh:Shape and rdfs:Class then the shape is a class target of itself.

ex:Person
	a rdfs:Class, sh:Shape .
ex:Alice a ex:Person .
ex:NewYork a ex:Place .

In this example, only ex:Alice is a focus node, because it is a SHACL instance of ex:Person which is both a class and a shape in the shapes graph.

Subjects-of targets (sh:targetSubjectsOf)

A subjects-of target is specified with the predicate sh:targetSubjectsOf, the values of which are IRIs. For every value p of such a target, the focus nodes are defined as the set of subjects in the data graph that appear in a triple with p as a predicate.

ex:TargetSubjectsOfExampleShape
	a sh:Shape ;
	sh:targetSubjectsOf ex:knows .
ex:Alice ex:knows ex:Bob .
ex:Bob ex:livesIn ex:NewYork .

In the example above, only ex:Alice is validated against the given shape, because it is the subject of a triple that has ex:knows as its predicate.

The following SPARQL query specifies the semantics of subjects-of targets. The variable targetSubjectsOf is assumed to be pre-bound to the given value of sh:targetSubjectsOf. All bindings of the variable this from the solution become focus nodes.

SPARQL DEFINITION
SELECT DISTINCT ?this    # ?this is the focus node
WHERE {
	?this $targetSubjectsOf ?any .    # $targetSubjectsOf is pre-boundto ex:knows
}
Objects-of targets (sh:targetObjectsOf)

An objects-of target is specified with the predicate sh:targetObjectsOf, the values of which are IRIs. For every value p of such a target, the focus nodes are defined as the set of objects in the data graph that appear in a triple with p as a predicate.

ex:TargetObjectsOfExampleShape
	a sh:Shape ;
	sh:targetObjectsOf ex:knows .
ex:Alice ex:knows ex:Bob .
ex:Bob ex:livesIn ex:NewYork .

In the example above, only ex:Bob is validated against the given shape, because it is the object of a triple that has ex:knows as its predicate.

The following SPARQL query specifies the semantics of objects-of targets. The variable targetObjectsOf is assumed to be pre-bound to the given value of sh:targetObjectsOf. All bindings of the variable this from the solution become focus nodes.

SPARQL DEFINITION
SELECT DISTINCT ?this    # ?this is the focus node
WHERE {
	?any $targetObjectsOf ?this .    # $targetObjectsOf is pre-bound to ex:knows
}

Constraints

A constraint is a node in the shapes graph that determines how to validate a focus node based on the values of properties and other characteristics of the node. Constraints can, for example, require that a focus node be an IRI or that a focus node has a particular value for a property and also a minimum number of values for the property.

The SHACL Core language defines two types of constraints:

In addition to those, SHACL Full can be used to specify SPARQL-based constraints. sh:Constraint is the SHACL superclass of all SHACL constraint types.

Property constraints and shapes may be based on one or more constraint components. A constraint component declares one or more properties, called parameters, and declares validators, which provide instructions (for example expressed via SPARQL queries) on how the parameters are used to validate data. Constraint components have an IRI which is used, among others, in validation reports. For example, the component sh:MinCountConstraintComponent declares the parameter sh:minCount to represent the restriction that the focus node has at least a minimum number of values for a particular property. The list of constraint components included in SHACL Core is described in section 4.

Validating an focus node against a constraint involves validating the focus node against each of the components for which the constraint has parameter values for.

Constraints can also have non-validating properties (such as names and default values) that do not lead to validation results.

Property Constraints (sh:predicate or sh:path)

Property constraints specify conditions that need to be met with respect to nodes that can be reached from the focus node either by directly following a given property (specified using sh:predicate) or a given property path (specified using sh:path).

Property constraints are associated with a shape with the property sh:property. Each value of sh:property is an IRI or a blank node that is the subject of precisely one triple with either predicate sh:predicate or sh:path. Each value of sh:predicate is an IRI. Each value of sh:path is a SHACL property path.

The following example illustrates the two syntax variations of property constraints.

ex:ExampleShapeWithPropertyConstraints
	a sh:Shape ;
	sh:property [
		sh:predicate ex:email ;
		sh:name "e-mail" ;
		sh:description "We need at least one email value" ;
		sh:minCount 1 ;
	] ;
	sh:property [
		sh:path (ex:knows ex:email) ;
		sh:name "Friend's e-mail" ;
		sh:description "We need at least one email for everyone you know" ;
		sh:minCount 1 ;
	] .

sh:PropertyConstraint is the class of property constraints. The objects of triples with sh:property as predicate have sh:PropertyConstraint as expected type.

Multiple Parameters

Some constraint components declare only a single parameter. For example sh:ClassConstraintComponent has the single parameter sh:class. These parameters may be used multiple times in the same constraint node. The interpretation of such declarations is conjunction, i.e. all constraints apply. The following example specifies that the values of ex:customer have to be SHACL instances of both ex:Customer and ex:Person.

ex:InvoiceShape
	a sh:Shape ;
	sh:property [
		sh:predicate ex:customer ;
		sh:class ex:Customer ;
		sh:class ex:Person ;
	] .

Some constraint components such as sh:PatternConstaintComponent declare more than one parameter. Constraints are not allowed to have more than one value for any of the parameters of such components.

Property Paths

SHACL includes an RDF vocabulary to represent property paths equivalent to a subset of SPARQL 1.1 property paths. SHACL supports the following SPARQL 1.1 property path constructs: PredicatePath, InversePath, SequencePath, AlternativePath, ZeroOrMorePath, OneOrMorePath and ZeroOrOnePath.

A SHACL property path p is a node in an RDF graph when it satisfies the antecedent of one of the following rules. For each rule, the mapping from a SHACL property to a SPARQL property paths is provided.

  1. If p is an IRI then the property path is mapped to a PredicatePath with p as predicate.
  2. If p is a blank node that is the subject of exactly one triple with rdf:first as predicate and p as object, and p is a SHACL list L with at least two members and each member of L is a SHACL property path then, for the members of L: v1, v2, ..., vn, p is mapped to a SequencePath of v1 followed by v2, ..., followed by vn.
  3. If p is a blank node that is the subject of exactly one triple with sh:alternativePath as predicate, L as object and no other triples, and L is a SHACL list with at least two members and each member of L is a SHACL property path then, for the members of L: v1, v2, ..., vn, p is mapped to a AlternativePath of v1 followed by v2, ..., followed by vn.
  4. If p is a blank node that is the subject of exactly one triple with sh:inversePath as predicate, v as object and no other triples, then p is mapped to an InversePath of v.
  5. If p is a blank node that is the subject of exactly one triple with sh:zeroOrMorePath as predicate, v as object and no other triples, then p is mapped to a ZeroOrMorePath of v.
  6. If p is a blank node that is the subject of exactly one triple with sh:oneOrMorePath as predicate, v as object and no other triples, then p is mapped to an OneOrMorePath of v.
  7. If p is a blank node that is the subject of exactly one triple with sh:zeroOrOnePath as predicate, v as object and no other triples, then p is mapped to a ZeroOrOnePath of v.

A node p is not a SHACL property path if p is a blank node and any path constructs of p directly or transitively reference p.

Two SHACL property paths are considered equivalent when they produce the exact same SPARQL 1.1 property path syntax.

The following example illustrates some valid SHACL property paths, together with their SPARQL 1.1 equivalents.

SPARQL Property path: ex:parent
SHACL Property path: ex:parent

SPARQL Property path: ^ex:parent
SHACL Property path: [ sh:inversePath ex:parent ]

SPARQL Property path: ex:parent/ex:firstName
SHACL Property path: ( ex:parent ex:firstName )

SPARQL Property path: rdf:type/rdfs:subClassOf*
SHACL Property path: ( rdf:type [ sh:zeroOrMorePath rdfs:subClassOf ] )

SPARQL Property path: ex:father|ex:mother
SHACL Property path: [ sh:alternativePath ( ex:father ex:mother  ) ]

Declaring the Severity of a Constraint

Constraints can specify one value for the property sh:severity in the shapes graph. Each value of this property is an IRI. SHACL includes the three IRIs listed in the table below to represent severities. These are declared in the SHACL vocabulary as SHACL instances of sh:Severity.

Severity Description
sh:Info A non-critical constraint violation indicating an informative message.
sh:Warning A non-critical constraint violation indicating a warning.
sh:Violation A constraint violation.

The specific values of sh:severity have no impact on the validation, but MAY be used by user interface tools to categorize validation results. The values of sh:severity are used by SHACL processors to populate the sh:resultSeverity field of validation results, see section on severity in validation results. For every constraint, sh:Violation is the default if sh:severity is unspecified. The following example illustrates this.

ex:MyShape
	a sh:Shape ;
	sh:targetNode ex:MyInstance ;
	sh:property [
		# Violations of sh:minCount and sh:datatype are produced as warnings
		sh:predicate ex:myProperty ;
		sh:minCount 1 ;
		sh:datatype xsd:string ;
		sh:severity sh:Warning ;
	] ;
	sh:property [
		# The default severity here is sh:Violation
		sh:predicate ex:myProperty ;
		sh:maxLength 10 ;
		sh:message "Too many characters"@en ;
		sh:message "Zu viele Zeichen"@de ;
	] .
ex:MyInstance
	ex:myProperty "http://toomanycharacters"^^xsd:anyURI .
[] a sh:ValidationReport ;
	sh:conforms "false"^^xsd:boolean ;
	sh:result
	[	a sh:ValidationResult ;
		sh:resultSeverity sh:Warning ;
		sh:focusNode ex:MyInstance ;
		sh:resultPath ex:myProperty ;
		sh:value "http://toomanycharacters"^^xsd:anyURI ;
		sh:sourceConstraintComponent sh:DatatypeConstraintComponent ;
		sh:sourceShape ex:MyShape ;
	] ,
	[	a sh:ValidationResult ;
		sh:resultSeverity sh:Violation ;
		sh:focusNode ex:MyInstance ;
		sh:resultPath ex:myProperty ;
		sh:value "http://toomanycharacters"^^xsd:anyURI ;
		sh:resultMessage "Too many characters"@en ;
		sh:resultMessage "Zu viele Zeichen"@de ;
		sh:sourceConstraintComponent sh:MaxLengthConstraintComponent ;
		sh:sourceShape ex:MyShape ;
	] .

Declaring Messages for a Constraint

Constraints can have values for the property sh:message, and these values are either xsd:string literals or literals with a language tag. If a constraint has at least one value for sh:message in the shapes graph, then all validation results produced as a result of the constraint will have exactly these messages as their value of sh:resultMessage, i.e. the values will be copied from the shapes graph into the results graph. A constraint should not have more than one value for sh:message with the same language tag.

The example from the previous section uses this mechanism to supply the second validation result with two messages. See the section on sh:resultMessage in the validation results on further details on how the values of sh:resultMessage are populated.

Deactivating a Constraint or Shape

SHACL includes a mechanism to deactivate a constraint or shape: If the constraint or shape has the value true for the property sh:deactivated then all focus nodes conform to it and the validation of the constraint or shape will never produce any validation results.

Use cases of this feature include shape reuse and debugging. In scenarios where constraints from other graphs or files are imported into a given shapes graph, sh:deactivated can be set to true in the local shapes graph for imported constraints to exclude constraints that do not apply in the current application context. This makes it possible to reuse SHACL graphs developed by others even if you disagree with certain assumptions made by the original authors. If a shape author anticipates that a constraint may need to be disabled or modified by others, it is a good practice to use IRIs instead of blank nodes for the actual constraints. For example, a constraint on the property ex:name at the shape ex:Person may have the IRI ex:Person-name. Another typical use case of sh:deactivated is during the development and testing of shapes, to (temporarily) disable certain constraints.

The following example illustrates the use of sh:deactivated to deactivate a constraint. In cases where shapes are imported from other graphs, the sh:deactivated true triple would be in the importing graph.

ex:PersonShape
	a sh:Shape ;
	sh:targetClass ex:Person ;
	sh:property ex:PersonShape-name .

ex:PersonShape-name
	a sh:PropertyConstraint ;
	sh:predicate ex:name ;
	sh:minCount 1 ;
	sh:deactivated true .

With the following data, no constraint violation will be reported even though the instance does not have any value for ex:name.

ex:JohnDoe a ex:Person .

Validation and Graphs

The definition for validating a data graph against a shapes graph as well as a focus node from the data graph against a shape from the shapes graph is provided below:

VALIDATION DEFINITION
Validation is the process of determining whether a data graph, or focus node, conform to a shapes graph or specific shapes in a shapes graph. A system that is capable of performing validation is called a processor, and the verb processing is sometimes used to refer to the validation process.

After validation, SHACL processors MUST return a validation report containing all validation results. For simpler validation scenarios, SHACL processors SHOULD provide an additional validation interface that returns only true for 'does conform' or false for 'does not conform'. Only SHACL implementations that can return all of the mandatory properties of the Validation Report Vocabulary are standards-compliant.

A validation may also result in a failure, which is reported by a SHACL processor to indicate that a request could not be handled. Failures are not represented as part of the validation report but through implementation-specific channels.

To validate a data graph against the shapes graph, a SHACL processor requires the shapes graph and the data graph as arguments for the validation process. Optionally, two additional arguments may be provided for validating a specific focus node from the data graph against a specific shape from the shapes graph.

SHACL can be used with RDF graphs that are obtained by any means, e.g. from the file system, HTTP requests, or RDF datasets. SHACL makes no assumptions about whether a graph contains triples that are entailed from the graph under any RDF entailment regime.

During validation, the data graph and the shapes graph MUST remain immutable, i.e. both graphs at the end of the validation MUST be identical to the graph at the beginning of validation. SHACL processors MUST NOT change the graphs that they use to construct the shapes graph or the data graph, even if these graphs are part of an RDF store that allows changes to its stored graphs. SHACL processors MAY store the graphs that they create, such as a graph containing validation results, and this operation MAY change existing graphs in an RDF store, but not any of the graphs that were used to construct the shapes graph or the data graph. SHACL processing is thus idempotent.

Shapes Graph

A shapes graph is an RDF graph containing zero or more shapes that is passed into a SHACL validation process so that a data graph can be validated against the shapes. Shapes graphs can be reusable validation modules that can be cross-referenced with the predicate owl:imports. As a pre-validation step, SHACL processors SHOULD extend the originally provided shapes graph by transitively following and importing all referenced shapes graphs through the owl:imports predicate. The resulting graph forms the input shapes graph for validation and MUST NOT be further modified during the validation process.

In addition to shape declarations, the shapes graph may contain additional information for the SHACL processor such as entailment directives.

Recursive shapes

The following properties are the so-called shape-expecting constraint parameters in SHACL Core:

A shape directly uses another shape when that other shape is the value of any of the shape-expecting constraint parameters of the shape itself or one of the property constraints of the shape. In the cases of sh:and, sh:or and sh:partition this includes the members of the lists that are the parameter values. The uses relationship between shapes is the transitive closure of the directly uses relationship. A shape is recursive if it uses itself.

The handling of recursive shapes is not defined in SHACL and is left to SHACL processor implementations. For example, SHACL processors may support recursion scenarios or produce an error when they detect recursion.

Data Graph

The data graph is an RDF graph that a SHACL processor can validate. SHACL processors treats it as a general RDF graph and makes no assumption about its nature. For example, it can be an in-memory graph or a named graph from an RDF dataset or a SPARQL endpoint.

The data graph is expected to include all the ontology axioms related to the data and especially all the rdfs:subClassOf triples in order for SHACL to correctly identify class targets and validate Core SHACL constraints.

Linking to shapes graphs (sh:shapesGraph)

A data graph can include triples used to suggest one or more graphs to a SHACL processor with the predicate sh:shapesGraph. Every value of this property is an IRI representing a graph that should be included into the shapes graph used to validate the data graph.

In the following example, a SHACL processor may use the union of ex:graph-shapes1 and ex:graph-shapes2 graphs (and their owl:imports) as the shapes graph when validating the given graph.

<http://example.com/myDataGraph>
	sh:shapesGraph ex:graph-shapes1 ;
	sh:shapesGraph ex:graph-shapes2 .

SHACL provides a way for schema (i.e. ontology or vocabulary) creators to suggest a set of shapes graphs for validating data graphs that uses that schema. These suggestions may be taken into account by users for specifying a shapes graph in order to validate a data graph. The suggestions are instantiated in the schema documents where every value for the property sh:shapesGraph denotes a suggested shapes graph. When the schema is identified by a schema IRI or a version IRI, this IRI should be the subject of these triples.

Validation Report

The validation report is the result of the validation process that reports the conformance and the set of all validation results. The validation report is described with the SHACL Validation Report Vocabulary as defined in this section. This vocabulary defines the RDF properties to represent structural information that may provide guidance on how to identify or fix violations in the data graph.

The following graph represents an example of a validation report for the validation of a data graph that conforms to a shapes graph.

[] a sh:ValidationReport ;
	sh:conforms "true"^^xsd:boolean .

The following graph represents an example of a validation report for the validation of a data graph that does not conform to a shapes graph. Note that the specific value of sh:resultMessage is not mandated by SHACL and considered implementation-specific.

[] a sh:ValidationReport ;
	sh:conforms "false"^^xsd:boolean ;
	sh:result [
		a sh:ValidationResult ;
		sh:resultSeverity sh:Violation ;
		sh:focusNode ex:Bob ;
		sh:resultPath ex:age ;
		sh:value "twenty two" ;
		sh:resultMessage "ex:age expects a literal of datatype xsd:integer." ;
		sh:sourceConstraintComponent sh:DatatypeConstraintComponent ;
		sh:sourceShape ex:PersonShape .
	] .

The validation results produced by a SHACL processor MUST be the product of validation of the data graph only. Some SHACL processors MAY also report errors in the shapes graph, but those errors MUST NOT be included in the data validation results.

Any validation results produced by the processing of shapes as values of shape-expecting constraint parameters (such as sh:shape) are temporary, i.e. they are not added to the results graph of the surrounding validation process. However, some implementations may add those nested validation results as annotations to the surrounding validation results, via sh:detail.

Validation Report (sh:ValidationReport)

The result of a validation process is an RDF graph with exactly one SHACL instance of sh:ValidationReport. The RDF graph can contain additional information related to the validation report or the validation results, for example, provenance metadata.

Conforms (sh:conforms)

A validation report has exactly one value for the property sh:conforms that is of datatype xsd:boolean. When the value of this property is true, the validation is successful and reports no validation results. When the value of this property is false, the validation is not successful and reports at least one validation result.

Result (sh:result)

For every validation result that is produced by a validation process (except those mentioned above), a validation report has a value for the property sh:result. Each value of sh:result is a SHACL instance of the class sh:ValidationResult.

Validation Result (sh:ValidationResult)

SHACL defines sh:ValidationResult as a subclass of sh:AbstractResult to report individual SHACL validation results. SHACL implementations may use other SHACL subclasses of sh:AbstractResult, for example, to report successfully completed constraint checks or accumulated results.

All the properties described in the remaining sub-sections of this section can be specified in a sh:ValidationResult. The properties sh:focusNode and sh:resultSeverity are the only properties that are mandatory for all validation results. The property sh:sourceConstraintComponent is mandatory for validation results produced by violations of constraint components.

Focus node (sh:focusNode)

Each validation result has exactly one value for the property sh:focusNode that is equal to the focus node that has caused the result. This is the focus node that was validated when the validation result was produced.

Path (sh:resultPath)

Validation results may have a value for the property sh:resultPath pointing at a well-formed property path starting with the given sh:focusNode. For results produced by a property constraint, this path is always identical to either the sh:predicate or sh:path of the constraint.

Value (sh:value)

Validation results may include, as a value of the property sh:value, a specific RDF term that has caused the result. The values of sh:value are populated by a SHACL processor based on the rules outlined in the section on Core Constraint Components. In most of these cases, the values of sh:value are the value nodes that have violated a constraint. For SPARQL-based constraints, the values of sh:value are derived using a Mapping of SPARQL Result Variables.

Source (sh:sourceConstraint, sh:sourceShape)

Validation results may include, as a value of the property sh:sourceShape, the shape that the given sh:focusNode was validated against. Furthermore, validation results may include, as a value of the property sh:sourceConstraint, the constraint that the given sh:focusNode was validated against.

Constraint Component (sh:sourceConstraintComponent)

For validation results produced as a result of a constraint component, the property sh:sourceConstraintComponent has as value the IRI of the constraint component that caused the result. For example, results produced due to a violation of a constraint based on a value of sh:minCount would have the value sh:MinCountConstraintComponent.

Detail (sh:detail)

The property sh:detail may link a (parent) result with one or more other (child) results that provide further details about the cause of the (parent) result. Depending on the capabilities of the SHACL processor, this may include violations of nested constraints that have been evaluated via sh:shape.

Message (sh:resultMessage)

Validation results may have values for the property sh:resultMessage to communicate additional textual details to humans. While sh:resultMessage may have multiple values, there should not be two values with the same language tag. These values are produced by a validation engine based on the values of sh:message of the constraints in the shapes graph, see Declaring Messages for a Constraint. In cases where a constraint does not have any values for sh:message in the shapes graph the following policy applies:

  • For validation results produced as a result of SPARQL-based constraints and constraint components, the messages are derived from a Mapping of SPARQL Result Variables.
  • For validation results produced as a result of constraint components from the SHACL Core, a SHACL processor MAY automatically generate other values for sh:resultMessage.

Severity (sh:resultSeverity)

Each validation result has exactly one value for the property sh:resultSeverity, and this value is an IRI. The values are derived from the shapes graph as described in the section Declaring the Severity of a Constraint.

Core Constraint Components

This section defines the built-in SHACL Core constraint components that MUST be supported by all SHACL Core processors. Each constraint component is identified by an IRI that is referenced in the validation results via sh:sourceConstraintComponent.

The choice of constraint components that were included into the SHACL Core was made based on the requirements collected by the [[shacl-ucr]] document. Special attention was paid to the balance between trying to cover as many common use cases as possible and keeping the size of the Core language manageable. Not all use cases (such as describing constraints on members of an rdf:List) can be expressed by the Core language alone. Instead, SHACL Full provides an extension mechanism, described in the second part of this specification. It is expected that additional reusable libraries of constraint components will be maintained by third parties.

All constraint components can be used both in property constraints and shapes. However, some components may always result in violations in a particular constraint type. For example, sh:closed does not make sense in property constraints.

The textual description of each component refers to the concept of value nodes. Value nodes and their rules for the creation of validation results are defined as:

SPARQL definitions for built-in constraint components

The SPARQL definitions in this section represent potential validators. Many constraint components are written as SPARQL ASK queries. These queries are interpreted against each value node, bound to the variable value. If an ASK query does not evaluate to true for a value node, a validation result is produced based on the rules outlined in the section on ASK-based validators. Constraint components that are described using a SELECT query are interpreted based on the rules outlined in the section on SELECT-based validators. In particular, for property constraints, the variable PATH is substituted with a path expression based on the values of either sh:predicate or sh:path in the constraint. All SPARQL queries also assume the variable bindings and result variable mapping rules detailed in the section on SPARQL-based Constraints. The variable this represents the currently validated focus node. Based on the parameter IRIs on the tables, pre-bound variables are derived using the syntax rules for parameter names.

Value Type Constraint Components

The constraint components in this section have in common that they can be used to restrict the type of value nodes. Note that it is possible to represent multiple value type options using sh:or.

sh:class

The condition specified by sh:class is that each value node is a SHACL instance of a given type.

Constraint Component IRI: sh:ClassConstraintComponent

Parameters:
Property Summary
sh:class Type of all value nodes
TEXTUAL DEFINITION
The values of sh:class are IRIs. A validation result MUST be produced for each value node that is either a literal, or a non-literal that is not a SHACL instance of the given class in the data graph.
SPARQL DEFINITION (Must evaluate to true for each value node $value)
ASK {
	$value rdf:type/rdfs:subClassOf* $class .
}
ex:ClassExampleShape
	a sh:Shape ;
	sh:targetNode ex:Bob, ex:Alice, ex:Carol ;
	sh:property [
		sh:predicate ex:knows ;
		sh:class ex:Person ;
	] .
ex:Alice a ex:Person .
ex:Bob ex:knows ex:Alice .
ex:Carol ex:knows ex:Bob .

sh:datatype

sh:datatype specifies a condition to be satisfied by the datatype of each value node.

Constraint Component IRI: sh:DatatypeConstraintComponent

Parameters:
Property Summary
sh:datatype Datatype of all value nodes (e.g., xsd:integer)
TEXTUAL DEFINITION
The values of sh:datatype are IRIs of datatypes, such as xsd:string. A validation result MUST be produced for each value node that is not a literal, or is a literal with a mismatching datatype, whereby the datatype of a literal is determined following the datatype function of SPARQL 1.1. (This also means that using rdf:langString as value of sh:datatype can be used to test if value nodes have a language tag.) A literal matches a datatype if the literal's datatype has the same IRI and, for the datatypes supported by SPARQL 1.1, is not an ill-typed literal.
ex:DatatypeExampleShape
	a sh:Shape ;
	sh:targetNode ex:Alice, ex:Bob, ex:Carol ;
	sh:property [
		sh:predicate ex:age ;
		sh:datatype xsd:integer ;
	] .
ex:Alice ex:age "23"^^xsd:integer .
ex:Bob ex:age "twenty two" .
ex:Carol ex:age "23"^^xsd:int .

sh:nodeKind

sh:nodeKind specifies a condition to be satisfied by the RDF node kind of each value node.

Constraint Component IRI: sh:NodeKindConstraintComponent

Parameters:
Property Summary
sh:nodeKind Node kind (IRI, blank node, literal or combinations of these) of all value nodes
TEXTUAL DEFINITION
The values of sh:nodeKind are one of the following six instances of the class sh:NodeKind: sh:BlankNode, sh:IRI and sh:Literal as well as the following values that represent combinations of the former three, i.e. either-or: sh:BlankNodeOrIRI, sh:BlankNodeOrLiteral and sh:IRIOrLiteral. A validation result MUST be produced for each value node that does not match the given node kind.
SPARQL DEFINITION (Must evaluate to true for each value node $value)
ASK {
	FILTER ((isIRI($value) && $nodeKind IN ( sh:IRI, sh:BlankNodeOrIRI, sh:IRIOrLiteral ) ) ||
		(isLiteral($value) && $nodeKind IN ( sh:Literal, sh:BlankNodeOrLiteral, sh:IRIOrLiteral ) ) ||
		(isBlank($value)   && $nodeKind IN ( sh:BlankNode, sh:BlankNodeOrIRI, sh:BlankNodeOrLiteral ) )) .
}
ex:NodeKindExampleShape
	a sh:Shape ;
	sh:targetNode ex:Bob, ex:Alice ;
	sh:property [
		sh:predicate ex:knows ;
		sh:nodeKind ex:IRI ;
	] .
ex:Bob ex:knows ex:Alice .
ex:Alice ex:knows "Bob" .

Cardinality Constraint Components

The following constraint components represent restrictions on the number of values that the focus node may have for the given property or property path. SHACL has no default cardinality restrictions on properties. Shapes have a cardinality always equal to 1 by design.

sh:minCount

sh:minCount specifies the minimum number of value nodes that satisfy the condition. If the minimum cardinality value is 0 then this constraint is always satisfied and so may be omitted.

Constraint Component IRI: sh:MinCountConstraintComponent

Parameters:
Property Summary
sh:minCount The minimum cardinality.
TEXTUAL DEFINITION
The values of sh:minCount are literals with datatype xsd:integer. A validation result MUST be produced if the number of value nodes is less than the value of sh:minCount.
SPARQL DEFINITION (Must return no results for the given $PATH)
SELECT $this
WHERE {
	OPTIONAL {
		$this $PATH ?value .
	}
} 
GROUP BY $this
HAVING (COUNT(DISTINCT ?value) < $minCount)
ex:MinCountExampleShape
	a sh:Shape ;
	sh:targetNode ex:Alice, ex:Bob ;
	sh:property [
		sh:predicate ex:name ;
		sh:minCount 1 ;
	] .
ex:Alice ex:name "Alice" .
ex:Bob ex:givenName "Bob"@en .

sh:maxCount

sh:maxCount specifies the maximum number of value nodes that satisfy the condition. If this parameter is omitted then there is no limit on the number of triples.

Constraint Component IRI: sh:MaxCountConstraintComponent

Parameters:
Property Summary
sh:maxCount The maximum cardinality.
TEXTUAL DEFINITION
The values of sh:maxCount are literals with datatype xsd:integer. A validation result MUST be produced if the number of value nodes is greater than the value of sh:maxCount.
SPARQL DEFINITION (Must return no results for the given $PATH)
SELECT $this
WHERE {
	$this $PATH ?value .
}
GROUP BY $this
HAVING (COUNT(DISTINCT ?value) > $maxCount)
ex:MaxCountExampleShape
	a sh:Shape ;
	sh:targetNode ex:Bob ;
	sh:property [
		sh:predicate ex:birthDate ;
		sh:maxCount 1 ;
	] .
ex:Bob ex:birthDate "May 5th 1990" .

Value Range Constraint Components

The following constraint components specify value range conditions to be satisfied by value nodes that are comparable via operators such as <, <=, > and >=.

sh:minExclusive, sh:minInclusive, sh:maxExclusive, sh:maxInclusive

The properties from the following table specify conditions to be satisfied by the range of value nodes. The supported datatypes of these value nodes are xsd:string, xsd:boolean, xsd:dateTime and all numeric datatypes such as xsd:integer.

Constraint Component IRIs: sh:MinExclusiveConstraintComponent, sh:MinInclusiveConstraintComponent, sh:MaxExclusiveConstraintComponent, sh:MaxInclusiveConstraintComponent

Parameters:
Property Summary Definition
sh:minExclusive The minimum exclusive value <
sh:minInclusive The minimum inclusive value <=
sh:maxExclusive The maximum exclusive value >
sh:maxInclusive The maximum inclusive value >=
TEXTUAL DEFINITION
A validation result MUST be produced for each value node that does not match the literal range specified by the table above, using the semantics of the SPARQL operators <, <=, > and >=. A validation result MUST also be produced if the value node cannot be compared to the specified range.

Note that if the comparison cannot be performed, for example when someone compares a string with an integer, then the SHACL processor will produce a validation result. This is different from, say, a plain SPARQL query, in which such failures would silently not lead to any results.

The following SPARQL definition covers sh:minExclusive - the other variations can be derived by replacing the < operator and the minExclusive variable.

SPARQL DEFINITION (Must evaluate to true for each value node $value)
ASK {
	FILTER ($minExclusive < $value)
}
ex:NumericRangeExampleShape
	a sh:Shape ;
	sh:targetNode ex:Bob, ex:Alice, ex:Ted ;
	sh:property [
		sh:predicate ex:age ;
		sh:minInclusive 0 ;
		sh:maxInclusive 150 ;
	] .
ex:Bob ex:age 23 .
ex:Alice ex:age 220 .
ex:Ted ex:age "twenty one" .

String-based Constraint Components

The constraint components in this section have in common specify conditions on the string representation of value nodes.

sh:minLength

sh:minLength specifies the minimum string length of each value node that satisfy the condition. This can be applied to any literals and IRIs, but not to blank nodes.

Constraint Component IRI: sh:MinLengthConstraintComponent

Parameters:
Property Summary
sh:minLength The minimum length. If the value is 0 then there is no restriction on the string length but this constraint is still violated if the value node is a blank node.
TEXTUAL DEFINITION
The values of sh:minLength are literals with datatype xsd:integer. A validation result MUST be produced for each value node where the length of its string representation (as defined by the SPARQL str function) is less than the specified minimum length, or if the value node is a blank node.
SPARQL DEFINITION (Must evaluate to true for each value node $value)
ASK {
	FILTER (STRLEN(str($value)) >= $minLength) .
}

sh:maxLength

sh:maxLength specifies the maximum string length of each value node that satisfy the condition. This can be applied to any literals and IRIs, but not to blank nodes.

Constraint Component IRI: sh:MaxLengthConstraintComponent

Parameters:
Property Summary
sh:maxLength The maximum length. If this constraint is omitted then there is no restriction on the string length and no requirement that the value node is a literal or IRI.
TEXTUAL DEFINITION
The values of sh:maxLength are literals with datatype xsd:integer. A validation result MUST be produced for each value node where the length of its string representation (as defined by the SPARQL str function) is greater than the specified maximum length, or if the value node is a blank node.
SPARQL DEFINITION (Must evaluate to true for each value node $value)
ASK {
	FILTER (STRLEN(str($value)) <= $maxLength) .
}
ex:PasswordExampleShape
	a sh:Shape ;
	sh:targetNode ex:Bob, ex:Alice ;
	sh:property [
		sh:predicate ex:password ;
		sh:minLength 8 ;
		sh:maxLength 10 ;
	] .
ex:Bob ex:password "123456789" .
ex:Alice ex:password "1234567890ABC" .

sh:pattern

sh:pattern specifies a regular expression that each value node matches to satisfy the condition.

Constraint Component IRI: sh:PatternConstraintComponent

Parameters:
Property Summary
sh:pattern Regular expression that all value nodes need to match
sh:flags An optional string of flags, interpreted as in SPARQL 1.1 REGEX
TEXTUAL DEFINITION
The values of sh:pattern are literals with datatype xsd:string that are valid pattern arguments for the SPARQL REGEX function. A validation result MUST be produced for each value node that is a blank node or where the string representation (as defined by the SPARQL str function) does not match the given regular expression (as defined by the SPARQL REGEX function). If sh:flags is present then this is a literal with datatype xsd:string that is interpreted according to the third argument of the SPARQL REGEX function.
SPARQL DEFINITION (Must evaluate to true for each value node $value)
ASK {
	FILTER (!isBlank($value) && IF(bound($flags), regex(str($value), $pattern, $flags), regex(str($value), $pattern)))
}
ex:PatternExampleShape
	a sh:Shape ;
	sh:targetNode ex:Bob, ex:Alice, ex:Carol ;
	sh:property [
		sh:predicate ex:bCode ;
		sh:pattern "^B" ;    # starts with 'B'
		sh:flags "i" ;       # Ignore case
	] .
ex:Bob ex:bCode "b101" .
ex:Alice ex:bCode "B102" .
ex:Carol ex:bCode "C103" .

sh:stem

sh:stem specifies the condition that each value node is an IRI and the IRI starts with a given string value.

Constraint Component IRI: sh:StemConstraintComponent

Parameters:
Property Summary
sh:stem String value that an IRI has to start with
TEXTUAL DEFINITION
The values of sh:stem are literals with datatype xsd:string. A validation result MUST be produced for each value node that is not an IRI or the string representation of the IRI does not start with the given string.
SPARQL DEFINITION (Must evaluate to true for each value node $value)
ASK {
	FILTER (isIRI($value) && STRSTARTS(str($value), $stem))
}
ex:StemExampleShape
	a sh:Shape ;
	sh:targetNode ex:Bob, ex:Alice, ex:Carol ;
	sh:property [
		sh:predicate ex:w3cHomepage ;
		sh:stem "https://www.w3.org/People/" ;
	] .
ex:Alice ex:w3cHomepage <https://www.w3.org/People/Alice> .
ex:Bob ex:w3cHomepage <https://example.com/People/Bob> .
ex:Carol ex:w3cHomepage "https://www.w3.org/People/Carol" .

sh:languageIn

The condition specified by sh:languageIn is that the allowed language tags for each value node are limited by a given list of language tags.

Constraint Component IRI: sh:LanguageInConstraintComponent

Parameters:
Property Summary
sh:languageIn A SHACL list of language ranges
TEXTUAL DEFINITION
Each value of sh:languageIn is a SHACL list. Each member of such list is a literal with datatype xsd:string. A validation result MUST be produced for each value node that is either not a literal or that does not have a language tag matching any of the provided language ranges following the filtering schema defined by the SPARQL langMatches function.
SPARQL DEFINITION (Must evaluate to true for each value node $value)
ASK {
	BIND (lang($value) AS ?valueLang) .
	FILTER EXISTS {
			GRAPH $shapesGraph {
				$languageIn (rdf:rest*)/rdf:first ?lang .
				FILTER (langMatches(?valueLang, ?lang))
			} 
		}
}

The following example shape states that all values of ex:prefLabel can be either in English or Māori.

ex:NewZealandLanguagesShape
	a sh:Shape ;
	sh:targetNode ex:Mountain, ex:Berg ;
	sh:property [
		sh:predicate ex:prefLabel ;
		sh:languageIn ( "en" "mi" ) ;
	] .

From the example instances, ex:Berg will lead to constraint violations for all of its labels.

ex:Mountain
	ex:prefLabel "Mountain"@en ;
	ex:prefLabel "Hill"@en-NZ ;
	ex:prefLabel "Maunga"@mi .

ex:Berg
	ex:prefLabel "Berg" ;
	ex:prefLabel "Berg"@de ;
	ex:prefLabel ex:BergLabel .

sh:uniqueLang

The property sh:uniqueLang can be set to true to specify that no pair of value nodes may use the same language tag. The values of sh:uniqueLang are of datatype xsd:booleans.

Constraint Component IRI: sh:UniqueLangConstraintComponent

Parameters:
Property Summary
sh:uniqueLang true to activate this constraint
TEXTUAL DEFINITION
The values of sh:uniqueLang are literals with datatype xsd:boolean. If sh:uniqueLang is set to true then a validation result MUST be produced for each non-empty language tag that is used by at least two value nodes.
SPARQL DEFINITION (Must return no results for the given $PATH)
SELECT DISTINCT $this ?lang
WHERE {
	{
		FILTER ($uniqueLang) .
	}
	$this $PATH ?value .
	BIND (lang(?value) AS ?lang) .
	FILTER (bound(?lang) && ?lang != "") . 
	FILTER EXISTS {
		$this $PATH ?otherValue .
		FILTER (?otherValue != ?value && ?lang = lang(?otherValue)) .
	}
}
ex:UniqueLangExampleShape
	a sh:Shape ;
	sh:targetNode ex:Alice, ex:Bob ;
	sh:property [
		sh:predicate ex:label ;
		sh:uniqueLang true ;
	] .
ex:Alice
	ex:label "Alice" ;
	ex:label "Alice"@en ;
	ex:label "Alice"@fr .

ex:Bob
	ex:label "Bob"@en ;
	ex:label "Bobby"@en .

Property Pair Constraint Components

The constraint components in this section specify conditions on the sets of value nodes in relation to other properties. Value nodes of shapes are always defined as a set of size 1 and may produce unexpected results when used with constraint components of this category.

sh:equals

sh:equals specifies the condition that the set of all value nodes is equal to the set of objects of the triples that have the focus node as subject and the value of sh:equals as predicate.

Constraint Component IRI: sh:EqualsConstraintComponent

Parameters:
Property Summary
sh:equals Property to compare with
TEXTUAL DEFINITION
The values of sh:equals are IRIs. Let P be a value of sh:equals. A validation result MUST be produced for each value node that does not exist as a value of P at the focus node and for each value of P at the focus node that is not one of the value nodes.
SPARQL DEFINITION (Must return no results for the given $PATH)
SELECT DISTINCT $this ?value
WHERE {
	{
		$this $PATH ?value .
		MINUS {
			$this $equals ?value .
		}
	}
	UNION
	{
		$this $equals ?value .
		MINUS {
			$this $PATH ?value .
		}
	}
}

The following example illustrates the use of sh:equals in a shape to specify that certain focus nodes need to have the same set of values for ex:firstName and ex:givenName.

ex:EqualExampleShape
	a sh:Shape ;
	sh:targetNode ex:Bob ;
	sh:property [
		sh:predicate ex:firstName ;
		sh:equals ex:givenName ;
	] .
ex:Bob
	ex:firstName "Bob" ;
	ex:givenName "Bob" .

sh:disjoint

sh:disjoint specifies the condition that the set of value nodes is disjoint with the the set of objects of the triples that have the focus node as subject and the value of sh:equals as predicate.

Constraint Component IRI: sh:DisjointConstraintComponent

Parameters:
Property Summary
sh:disjoint The property to compare the values with
TEXTUAL DEFINITION
The values of sh:disjoint are IRIs. A validation result MUST be produced for each value node that also exists as a value of the property specified using sh:disjoint at the focus node.
SPARQL DEFINITION (Must return no results for the given $PATH)
SELECT DISTINCT $this ?value
WHERE {
	$this $PATH ?value .
	$this $disjoint ?value .
}

The following example illustrates the use of sh:disjoint in a shape to specify that certain focus nodes cannot share any values for ex:prefLabel and ex:altLabel.

ex:DisjointExampleShape
	a sh:Shape ;
	sh:targetNode ex:USA, ex:Germany ;
	sh:property [
		sh:predicate ex:prefLabel ;
		sh:disjoint ex:altLabel ;
	] .
ex:USA
	ex:prefLabel "USA" ;
	ex:altLabel "United States" .

ex:Germany
	ex:prefLabel "Germany" ;
	ex:altLabel "Germany" .

sh:lessThan

sh:lessThan specifies the condition that each value node is smaller than all the objects of the triples that have the focus node as subject and the value of sh:lessThan as predicate.

Constraint Component IRI: sh:LessThanConstraintComponent

Parameters:
Property Summary
sh:lessThan The property to compare the values with
TEXTUAL DEFINITION
The values of sh:lessThan are IRIs. A validation result MUST be produced for each pair of value nodes and the values of the property specified using sh:lessThan at the given focus node, where the first value is not less than the second value, based on SPARQL's < operator. A validation result MUST also be produced if the two values cannot be compared.
SPARQL DEFINITION (Must return no results for the given $PATH)
SELECT DISTINCT $this ?value
WHERE {
	$this $PATH ?value .
	$this $lessThan ?otherValue .
	FILTER (!(?value < ?otherValue)) .
}

The following example illustrates the use of sh:lessThan in a shape to specify that all values of ex:startDate are "before" the values of ex:endDate.

ex:LessThanExampleShape
	a sh:Shape ;
	sh:property [
		sh:predicate ex:startDate ;
		sh:lessThan ex:endDate ;
	] .

sh:lessThanOrEquals

sh:lessThanOrEquals specifies the condition that each value node is smaller than or equal to all the objects of the triples that have the focus node as subject and the value of sh:lessThanOrEquals as predicate.

Constraint Component IRI: sh:LessThanOrEqualsConstraintComponent

Parameters:
Property Summary
sh:lessThanOrEquals The property to compare the values with
TEXTUAL DEFINITION
The values of sh:lessThanOrEquals are IRIs. A validation result MUST be produced for each pair of value nodes and the values of the property specified using sh:lessThanOrEquals at the focus node, where the first value is not less than or equal to the second value, based on SPARQL's <= operator. A validation result MUST also be produced if the two values cannot be compared.
SPARQL DEFINITION (Must return no results for the given $PATH)
SELECT DISTINCT $this ?value
WHERE {
	$this $PATH ?value .
	$this $lessThan ?otherValue .
	FILTER (!(?value <= ?otherValue)) .
}

Logical Constraint Components

The constraint components in this section implement the common logical operators and, or and not.

sh:not

sh:not specifies the condition that each value node cannot conform to a given shape. This is comparable to negation and the logical "not" operator.

Constraint Component IRI: sh:NotConstraintComponent

Parameters:
Property Summary
sh:not The shape to negate
TEXTUAL DEFINITION
The values of sh:not are either IRIs or blank nodes. The expected type of these nodes is sh:Shape. A validation result MUST be produced for each value node that produces no validation results for the shape given via sh:not. A failure MUST be reported if the validation of the shape produces a failure.

The following example illustrates the use of sh:not in a shape to specify the condition that certain focus nodes cannot have any value of ex:property.

ex:NotExampleShape
	a sh:Shape ;
	sh:targetNode ex:InvalidInstance1 ;
	sh:not [
		a sh:Shape ;
		sh:property [
			sh:predicate ex:property ;
			sh:minCount 1 ;
		] ;
	] .
ex:InvalidInstance1 ex:property "Some value" .

sh:and

sh:and specifies the condition that each value node conforms to all provided shapes. This is comparable to conjunction and the logical "and" operator.

Constraint Component IRI: sh:AndConstraintComponent

Parameters:
Property Summary
sh:and A SHACL list of shapes to validate the value nodes against
TEXTUAL DEFINITION
The value of each sh:and is a SHACL list. Each member of such list is an IRI or a blank node and its expected type is sh:Shape. A validation result MUST be produced for each value node if the following condition is false: The validation of the value node against all of the members of the SHACL list that is the value of sh:and produces a validation result for at least one member. A failure MUST be produced if the validation of one of the members produces a failure.

Note that although sh:and has an rdf:List of shapes as its value, the order of those shapes does not impact the validation results.

The following example illustrates the use of sh:and in a shape to specify the condition that certain focus nodes have exactly one value of ex:property. This is achieved via the conjunction of a separate named shape (ex:SuperShape) which specifies the minimum count, and a blank node shape that additionally specifies the maximum count. As shown here, sh:and can be used to implement a specialization mechanism between shapes.

ex:SuperShape
	a sh:Shape ;
	sh:property [
		sh:predicate ex:property ;
		sh:minCount 1 ;
	] .

ex:ExampleAndShape
	a sh:Shape ;
	sh:targetNode ex:ValidInstance, ex:InvalidInstance ;
	sh:and (
		ex:SuperShape
		[
			a sh:Shape ;
			sh:property [
				sh:predicate ex:property ;
				sh:maxCount 1 ;
			]
		]
	) .
ex:ValidInstance
	ex:property "One" .

# Invalid: more than one property
ex:InvalidInstance
	ex:property "One" ;
	ex:property "Two" .

sh:or

sh:or specifies the condition that each value node conforms to at least one of the provided shapes. This is comparable to disjunction and the logical "or" operator.

Constraint Component IRI: sh:OrConstraintComponent

Parameters:
Property Summary
sh:or A SHACL list of shapes to validate the value nodes against
TEXTUAL DEFINITION
The value of each sh:or is a SHACL list. Each member of such list is an IRI or a blank node and its expected type is sh:Shape. A validation result MUST be produced for each value node if the following condition is false: The validation of the value node against all of the members in the SHACL list that is the value of sh:or produces no validation results for at least one member. A failure MUST be produced if the validation of one of the members produces a failure.

Note that although sh:or has an rdf:List of shapes as its value, the order of those shapes does not impact the validation results.

The following example illustrates the use of sh:or in a shape to specify the condition that certain focus nodes have at least one value of ex:firstName or at least one value of ex:givenName.

ex:OrConstraintExampleShape
	a sh:Shape ;
	sh:targetNode ex:Bob ;
	sh:or (
		[
			sh:property [
				sh:predicate ex:firstName ;
				sh:minCount 1 ;
			]
		]
		[
			sh:property [
				sh:predicate ex:givenName ;
				sh:minCount 1 ;
			]
		]
	) .
ex:Bob ex:firstName "Robert" .

The next example shows how sh:or can be used in a property constraint to state that the values of the given property ex:address may be either literals with datatype xsd:string or SHACL instances of the class ex:Address.

ex:PersonAddressShape
	a sh:Shape ;
	sh:targetClass ex:Person ;
	sh:property [
		sh:predicate ex:address ;
		sh:or (
			[
				sh:datatype xsd:string ;
			]
			[
				sh:class ex:Address ;
			]
		)
	] .
ex:Bob ex:address "123 Prinzengasse, Vaduz, Liechtenstein" .

Shape-based Constraint Components

The constraint components in this section can be used to specify complex conditions by validating the value nodes against certain shapes.

sh:shape

sh:shape specifies the condition that each value node conforms to the given shape.

Constraint Component IRI: sh:ShapeConstraintComponent

Parameters:
Property Summary
sh:shape The shape that all value nodes need to conform to
TEXTUAL DEFINITION
The values of sh:shape are IRIs or blank nodes. The expected type of these values is sh:Shape. A validation result MUST be produced for each value node where validating the value node against the shape specified by sh:shape produces any validation results. A failure MUST be produced if the validation of any value node has produced a failure.

In the following example, all values of the property ex:someProperty will validate with no results for the shape specified by a blank node that ensures that the property ex:nestedProperty has at least one value.

ex:ShapeExampleShape
	a sh:Shape ;
	sh:property [
		sh:predicate ex:someProperty ;
		sh:shape [
			a sh:Shape ;   # Optional
			sh:property [
				sh:predicate ex:nestedProperty ;
				sh:minCount 1 ;
			]
		]
	] .
ex:ShapeExampleValidResource
	ex:someProperty [
		ex:nestedProperty 42 ;
	] .

sh:qualifiedValueShape, sh:qualifiedMinCount, sh:qualifiedMaxCount

Feature at risk: There is an ongoing proposal (see sh:partition) that strongly overlaps with the QCRs proposed here. The WG may decide to support only one of these options, but not both.

sh:qualifiedValueShape specifies the condition that a specified number of value nodes conforms to the given shape. Each sh:qualifiedValueShape can have: one value for sh:qualifiedMinCount, one value for sh:qualifiedMaxCount or, one value for each, at the same subject.

Constraint Component IRI: sh:QualifiedValueShapeConstraintComponent

Parameters:
Property Summary
sh:qualifiedValueShape The shape that the specified number of value nodes needto conform to
sh:qualifiedMinCount The minimum number of value nodes that conform to the shape. If this constraint is omitted, there is specified condition on the minimum number of values.
sh:qualifiedMaxCount The maximum number of value nodes that can conform to the shape. If this constraint is omitted, there is no specified condition on the maximum number of values.
TEXTUAL DEFINITION of sh:qualifiedMinCount
The values of sh:qualifiedValueShape are IRIs or blank nodes. The expected type of these values is sh:Shape. The values of sh:qualifiedMinCount are literals with datatype xsd:integer. Let C be the number of value nodes where validating the value node against the shape specified by sh:qualifiedValueShape produces no validation results. A failure MUST be produced if the validation of any of the value nodes produces a failure. A validation result MUST be produced if C is less than the specified sh:qualifiedMinCount.
TEXTUAL DEFINITION of sh:qualifiedMaxCount
The values of sh:qualifiedMaxCount are literals with datatype xsd:integer. Let C be the number of value nodes where validating the value node against the shape specified by sh:qualifiedValueShape produces no validation results. A failure MUST be produced if the validation of any of the value nodes produces a failure. A validation result MUST be produced if C is greater than the specified sh:qualifiedMaxCount.

In the following example shape can be used to specify the condition that the property ex:parent has exactly two values, and at least one of them is female.

ex:QualifiedValueShapeExampleShape
	a sh:Shape ;
	sh:targetNode ex:QualifiedValueShapeExampleValidResource ;
	sh:property [
		sh:predicate ex:parent ;
		sh:minCount 2 ;
		sh:maxCount 2 ;
		sh:qualifiedValueShape [
			a sh:Shape ;   # Optional
			sh:property [
				sh:predicate ex:gender ;
				sh:hasValue ex:female ;
			]
		] ;
		sh:qualifiedMinCount 1 ;
	] .
ex:QualifiedValueShapeExampleValidResource
	ex:parent ex:John ;
	ex:parent ex:Jane .

ex:John
	ex:gender ex:male .

ex:Jane
	ex:gender ex:female .

sh:partition

In some cases a given property may be multi-valued and it may be required that the set of values be partitioned into two or more subsets, each of which satisfies certain constraints.

For example, suppose that in the Library of Congress BIBFRAME (bf:) Cultural Heritage vocabulary each person (bf:Person) needs to be identified by (bf:identifiedBy) exactly one identifier from id.loc.gov and may have another identifier from viaf.org. No other identifiers are allowed. Thus the set of all identifiers is partitioned into two subsets, the first of which contains exactly one member and the second of which contains zero or one members. The following example shows a snippet of some valid BIBFRAME data.

<bf_Person1>
  	bf:identifiedBy <http://id.loc.gov/authorities/names/n80103961#RWO> ;
 	bf:identifiedBy <https://viaf.org/viaf/268367832/#Knape,_Joachim> .

The following example shows a snippet of some invalid BIBFRAME data.

<bf_Person1>
  	bf:identifiedBy <http://id.loc.gov/authorities/names/n80103961#RWO> ;
 	bf:identifiedBy <https://viaf.org/viaf/268367832/#Knape,_Joachim> ;
	bf:identifiedBy "this is a mistake" . # error

Qualified cardinality constraints provide a basis for expressing this type of partitioning requirement, but using them imposes a burden on the shapes author. In the BIBFRAME example the author would need to express the requirement that the set of all identifiers that are from neither id.loc.gov nor viaf.org is empty, i.e. it has a maximum cardinality of 0. Clearly, as more subsets of values are involved, the burden on the author increases. The sh:partition constraint makes it easier to express this type of requirement than it would be to use multiple qualified cardinality constraints. In effect, sh:partition chains together a sequence of qualified cardinality constraints and removes the set of value nodes matched by each from further consideration. If every value node gets matched in this process, then the sh:partition constraint reports no violations. Otherwise, any value nodes remaining are reported as violations of the constraint. The BIBFRAME example constraint is expressed as follows.

ex:BibframeShape a sh:Shape ;
	sh:property [
		sh:predicate bf:identifiedBy ;
		sh:partition (
			[sh:minCount 1; sh:maxCount 1; sh:pattern "^http://id.loc.gov/"]
			[sh:maxCount 1; sh:pattern "^https://viaf.org/"]
		)
] .

The value of the sh:partition constraint parameter is a SHACL list that has zero or more members. Each member of the list specifies conditions on a subset of the value nodes and may contain the following parameters:

  • zero or one sh:minCount. This specifies the minimum cardinality of the corresponding subset.
  • zero or one sh:maxCount. This specifies the maximum cardinality of the corresponding subset.
  • any combination of parameters associated with node validation constraints. A node validation constraint is any constraint defined by a boolean function on nodes. These include the built-in constraints defined by sh:nodeKind, sh:partition, sh:minExclusive, etc. The corresponding subset consists of those remaining nodes for which the boolean function is true.

Note that a node that contains no parameters matches all nodes. Such a node is useful as the last member of the list where it acts as a default matching rule in the case where nodes that do not match any of the preceeding constraints are allowed. Note also that a qualified cardinality constraint declared using sh:qualifiedValueShape, sh:qualifiedMinCount, and sh:qualifiedMaxCount is equivalent to a sh:partition constraint that contains two nodes with the first one containing the corresponding parameters and the last one being the default matching rule that matches any set of nodes.

Each member of the list is used by the SHACL processor to match a subset of the value nodes. The SHACL processor matches as many nodes as possible and then compares the result with the specified minimum and maximum cardinalities if specified. This is referred to as a greedy matching algorithm. Greedy pattern matching is commonly used with textual regular expressions. Nodes that match are removed from further matching. Thus the set of all value nodes becomes partitioned by the matching algorithm. The following paragraphs define this algorithm more precisely.

Let D be a data graph and let F be a focus node in D. Let S be a shapes graph, let T be a shape in S, and let C be a sh:partition constraint in T. Let N be the set of value nodes for C in D at F. Recall that N depends on how C is related to T.

  • If (T, sh:constraint, C) is in S then N consists of just the node F.
  • If (T, sh:property, C) and (C, sh:predicate, P) are in S then N consists of all the nodes X such that (F, P, X) is in D.

Let the value of the sh:partition parameter be the SHACL list with members (Q1, ..., Qn). The SHACL processor MUST perform the following steps to validate the constraint C at F.

  1. Let R denote the set of remaining value nodes. Initialize R to N.
  2. Repeat the following for Q = Q1, ..., Qn
    1. Let P be the conjunction of all the node validation constraints in Q.
    2. Compute R' to be the set of all nodes in R that satisfy P, i.e. R' = {X in R | P(X) = true}
    3. If Q contains a minimum cardinality mmin and the number of nodes in R' is less than mmin, i.e. mmin > #R', then report a constraint violation and exit the loop.
    4. If Q contains a maximum cardinality mmax and the number of nodes in R' is greater then mmax, i.e. mmax < #R', then report a constraint violation and exit the loop.
    5. Remove R' from R, i.e. set R = R \ R'.
  3. If R is non-empty and no violations have been reported yet then report a violation.

Note that the order of nodes within the SHACL list is significant. TODO: This currently violates our definition of rdf:List members. In general, if the members of the SHACL list are reordered then different value node sets will be matched and different violation results will be reported.

Other Constraint Components

This section enumerates Core constraint components that did not fit into the other categories.

sh:closed, sh:ignoredProperties

The RDF data model offers a huge amount of flexibility. Any node can in principle have values for any property. However, in some cases it makes sense to specify conditions on which properties can be applied to nodes. The SHACL Core language includes a property called sh:closed that can be used to specify the condition that each focus node has values only for those properties that have been explicitly enumerated via sh:property.

Constraint Component IRI: sh:ClosedConstraintComponent

Parameters:
Property Summary
sh:closed Set to true to close the shape
sh:ignoredProperties Optional SHACL list of properties that are also permitted in addition to those explicitly enumerated via sh:property
TEXTUAL DEFINITION
The values of sh:closed are literals with datatype xsd:boolean. If sh:closed is true then a validation result MUST be produced for each triple that has the focus node as its subject and a predicate that is not explicitly enumerated as a sh:predicate in any of the sh:property constraints at the surrounding shape. If the parameter sh:ignoredProperties is present then, each value of sh:ignoredProperties is a SHACL list. Each member of such list is an IRI, and the properties enumerated in this SHACL list are also permitted for the subject. The produced validation result MUST have the predicate of the triple as its sh:resultPath, and the object of the triple as its sh:value.
SPARQL DEFINITION (Must return no results)
SELECT $this (?predicate AS ?path) ?value
WHERE {
	{
		FILTER ($closed) .
	}
	$this ?predicate ?value .
	FILTER (NOT EXISTS {
		GRAPH $shapesGraph {
			$currentShape sh:property/sh:predicate ?predicate .
		}
	} && (!bound($ignoredProperties) || NOT EXISTS {
		GRAPH $shapesGraph {
			$ignoredProperties rdf:rest*/rdf:first ?predicate .
		}
	}))
}

The following example illustrates the use of sh:closed in a shape to specify the condition that certain focus nodes only have values for ex:exampleProperty1 and ex:exampleProperty2. The "ignored" property rdf:type would also be allowed.

ex:ClosedShapeExampleShape
	a sh:Shape ;
	sh:targetNode ex:Alice, ex:Bob ;
	sh:closed true ;
	sh:ignoredProperties (rdf:type) ;
	sh:property [
		sh:predicate ex:firstName ;
	] ;
	sh:property [
		sh:predicate ex:lastName ;
	] .
ex:Alice
	ex:firstName "Alice" .

ex:Bob
	ex:firstName "Bob" ;
	ex:middleInitial "J" .

sh:hasValue

sh:hasValue specifies the condition that at least one value node is equal to the given RDF term.

Constraint Component IRI: sh:HasValueConstraintComponent

Parameters:
Property Summary
sh:hasValue A specific required value
TEXTUAL DEFINITION
A validation result MUST be produced if the RDF term specified by sh:hasValue is not among the value nodes.
SPARQL DEFINITION (Must return no results for the given $PATH)
SELECT $this
WHERE {
	FILTER NOT EXISTS { $this $PATH $hasValue }
}
ex:StanfordGraduate
	a sh:Shape ;
	sh:targetNode ex:Alice ;
	sh:property [
		sh:predicate ex:alumniOf ;
		sh:hasValue ex:Stanford ;
	] .
ex:Alice
	ex:alumniOf ex:Harvard ;
	ex:alumniOf ex:Stanford .

sh:in

sh:in specifies the condition that each value node is a member of a provided SHACL list.

Constraint Component IRI: sh:InConstraintComponent

Parameters:
Property Summary
sh:in A SHACL list that has the allowed values as members
TEXTUAL DEFINITION
Each value of sh:in is a SHACL list. A validation result MUST be produced for every value node that is not a member of the given SHACL list. Matching of literals needs to be exact, e.g. "04"^^xsd:byte does not match "4"^^xsd:integer.
SPARQL DEFINITION (Must evaluate to true for each value node $value)
ASK {
	GRAPH $shapesGraph {
		$in (rdf:rest*)/rdf:first $value .
	}
}
ex:InExampleShape
	a sh:Shape ;
	sh:targetNode ex:RainbowPony ;
	sh:property [
		sh:predicate ex:color ;
		sh:in ( ex:Pink ex:Purple ) ;
	] .
ex:RainbowPony ex:color ex:Pink .

Non-Validating Constraint Characteristics

While the previous sections introduced properties that represent validation conditions, this section covers properties that are ignored by SHACL processors. The use of these properties is entirely optional and not subject to formal interpretation contracts. They MAY be used for purposes such as form building or predictable printing of RDF files.

Property constraints may have one or more values for sh:name to provide human-readable labels for the property in the target where it appears. If present, tools SHOULD prefer those locally specified labels over globally specified labels at the rdf:Property itself. For example, if a form displays a node that is in the target of a given shape, and the shape declares a sh:property constraint with an sh:name, then the tool SHOULD use the provided name. Similarly, property constraints may have an sh:description to provide a description of the property in the given context. Both sh:name and sh:description may have multiple values, but should only have one value per language tag.

Property constraints may have one value for the property sh:order to indicate the relative order of the property constraint for purposes such as form building. The values of sh:order are decimals. sh:order is not used for validation purposes. If present, the recommended use of sh:order is to sort the property constraints in an ascending order, for example so that properties with smaller order are placed above (or to the left) of properties with larger order.

Property constraints may link to an SHACL instance of the class sh:PropertyGroup using the property sh:group to indicate that the constraint belongs to a group of related property constraints. Each group may have additional triples that serve application purposes, such as an rdfs:label for form building. Groups may also have an sh:order property to indicate the relative ordering of groups within the same form.

Property constraints may have a single value for sh:defaultValue. The default value does not have fixed semantics in SHACL, but MAY be used by user interface tools to pre-populate input widgets. The value type of the sh:defaultValue should align with the specified sh:datatype or sh:class of the same constraint.

The following example illustrates the use of these various features together.

ex:PersonFormShape
	a sh:Shape ;
	sh:property [
		sh:predicate ex:firstName ;
		sh:name "first name" ;
		sh:description "The person's given name(s)" ;
		sh:order 0 ;
		sh:group ex:NameGroup ;
	] ;
	sh:property [
		sh:predicate ex:lastName ;
		sh:name "last name" ;
		sh:description "The person's last name" ;
		sh:order 1 ;
		sh:group ex:NameGroup ;
	] ;
	sh:property [
		sh:predicate ex:streetAddress ;
		sh:name "street address" ;
		sh:description "The street address including number" ;
		sh:order 11 ;
		sh:group ex:AddressGroup ;
	] ;
	sh:property [
		sh:predicate ex:locality ;
		sh:name "locality" ;
		sh:description "The suburb, city or town of the address" ;
		sh:order 12 ;
		sh:group ex:AddressGroup ;
	] ;
	sh:property [
		sh:predicate ex:postalCode ;
		sh:name "postal code" ;
		sh:name "zip code"@en-US ;
		sh:description "The postal code of the locality" ;
		sh:order 13 ;
		sh:group ex:AddressGroup ;
	] .

ex:NameGroup
	a sh:PropertyGroup ;
	sh:order 0 ;
	rdfs:label "Name" .

ex:AddressGroup
	a sh:PropertyGroup ;
	sh:order 1 ;
	rdfs:label "Address" .

A form building application MAY use the information above to display information as follows:

Name
first name: John
last name: Doe
Address
street address: 123 Silverado Ave
locality: Cupertino
zip code: 54321

Part 2: SHACL Full

Part 1 of this specification introduced features that are built into the Core of SHACL. The goal of this Core was to provide a high-level vocabulary for common use cases to describe shapes. However, SHACL also provides mechanisms to go beyond the Core vocabulary and represent constraints and targets with greater flexibility. These mechanisms, called SHACL Full, are described in the following sections.

SPARQL-based Constraints

SHACL Full supports two mechanisms to specify constraints using SPARQL:

The following sub-sections are about the latter.

Syntax of SPARQL-based Constraints

Shapes may have values for the property sh:sparql, and these values are either IRIs or blank nodes. The values of sh:sparql have the expected type sh:SPARQLConstraint which is an rdfs:subClassOf sh:Constraint and is the class of all SPARQL-based constraints. SPARQL-based constraints have exactly one value for the property sh:select with a literal of datatype xsd:string as value. As elaborated in the section on prefix handling rules, the value of sh:select is a valid SPARQL 1.1 SELECT query that contains the result variable this in the SELECT clause. The remainder of this section is not normative.

The following example illustrates the syntax of a SPARQL-based constraint.

ex:ValidCountry a ex:Country ;
	ex:germanLabel "Spanien"@de .
  
ex:InvalidCountry a ex:Country ;
	ex:germanLabel "Spain"@en .
ex:LanguageExampleShape
	a sh:Shape ;
	sh:targetClass ex:Country ;
	sh:sparql [
		a sh:SPARQLConstraint ;   # This triple is optional
		sh:message "Values are literals with German language tag." ;
		sh:prefixes ex: ;
		sh:select """
			SELECT $this (ex:germanLabel AS ?path) ?value
			WHERE {
				$this ex:germanLabel ?value .
				FILTER (!isLiteral(?value) || !langMatches(lang(?value), "de"))
			}
			""" ;
	] .

The target of the shape above includes all SHACL instances of ex:Country. For those nodes (represented by the variable this), the SPARQL query walks through the values of ex:germanLabel and verifies that they are literals with a German language code. The validation results for the aforementioned data graph is shown below:

[] a sh:ValidationReport ;
	sh:conforms "false"^^xsd:boolean ;
	sh:result [
		a sh:ValidationResult ;
		sh:resultSeverity sh:Violation ;
		sh:focusNode ex:InvalidCountry ;
		sh:resultPath ex:germanLabel ;
		sh:value "Spain"@en ;
		sh:sourceShape ex:LanguageExampleShape ;
		# ...
	] .

The SPARQL query returns result set solutions for all bindings of ?value that violate the constraint. A validation result is produced for each solution in that result set, following the mapping rules explained later: Each validation result will have $this as the sh:focusNode, ex:germanLabel as sh:resultPath and the violating value as sh:value.

Prefix Declarations for SPARQL Queries

A shapes graph may include declarations of namespace prefixes so that these prefixes can be used to abbreviate the SPARQL queries derived from the same shapes graph. The syntax of such prefix declarations is illustrated by the following example.

ex:
	a owl:Ontology ;
	owl:imports sh: ;
	sh:declare [
		sh:prefix "ex" ;
		sh:namespace "http://example.com/ns#"^^xsd:anyURI ;
	] ;
	sh:declare [
		sh:prefix "schema" ;
		sh:namespace "http://schema.org/"^^xsd:anyURI ;
	] .

The property sh:declare is used to make prefix declarations. The SHACL vocabulary includes the class sh:PrefixDeclaration as type for the values of sh:declare although no rdf:type triple is required for them. The values of sh:declare have exactly one value for the property sh:prefix (literals of datatype xsd:string) and exactly one value for the property sh:namespace (literals of datatype xsd:anyURI). Such a pair of values specifies a single mapping of a prefix to a namespace.

The recommended subject for values of sh:declare is the IRI of the graph containing the shapes that use the prefixes. These IRIs are often declared as an instance of owl:Ontology, but this is not required.

Prefix declarations can be used by SPARQL-based constraints and similar SPARQL-based features such as the validators of constraint components, derived values constraints, target types and functions. These nodes can use the property sh:prefixes to specify a set of prefix mappings. (An example use of the sh:prefixes property can be found in the example above.) The values of sh:prefixes are either IRIs or blank nodes. A SHACL processor collects a set of prefix mappings as the union of all individual prefix mappings that can be reached by the property path sh:prefixes/owl:imports*/sh:declare starting at the SPARQL-based constraint. If such a collection of prefix declarations contains multiple namespaces for the same value of sh:prefix, then the shapes graph is invalid. (Note that SHACL processors MAY ignore prefix declarations that are never reached). A SHACL processor transforms the values of sh:select (and similar properties such as sh:ask) into SPARQL by prepending PREFIX declarations for all prefix mappings. Each value of sh:prefix is turned into the PNAME_NS, while each value of sh:namespace is turned into the IRIREF in the PREFIX declaration. For the example shapes graph above, a SHACL Full processor would produce lines such as PREFIX ex: <http://example.com/ns#>. The SHACL Full processor MUST produce a failure if the resulting SPARQL query string cannot be parsed into a valid SPARQL 1.1 query. In the rest of this document, the sh:prefixes statements may have been omitted for brevity.

Pre-bound Variables in SPARQL Constraints ($this, $shapesGraph, $currentShape)

The following table enumerates the variables that have special meaning in SPARQL constraints. When SPARQL constraints are processed, the SHACL Full processor pre-binds values for these variables.

Variable Interpretation
$this The focus node.
$shapesGraph Can be used to query the shapes graph as in GRAPH $shapesGraph { ... }. If the shapes graph is a named graph in the same dataset as the data graph then it is the IRI of the shapes graph in the dataset. Not all SHACL Full processors need to support this variable. Processors that do not support $shapesGraph MUST report a failure if they encounter a query that references this variable. Use of GRAPH $shapesGraph { ... } should be handled with extreme caution. It may result in constraints that are not interoperable across different SHACL Full processors and that may not run on remote RDF datasets.
$currentShape The current shape. Typically used in conjunction with $shapesGraph. The same support policies as for $shapesGraph apply for this variable.

Mapping of Result Variables to Validation Results

If one of the solutions of the result set produced by a SELECT query during a validation process contains the binding true for the variable failure, then the SHACL Full processor MUST signal a failure.

Otherwise, each row of the result set produced by a SELECT query MUST be converted into one validation result node. The property values of those nodes are derived by the following rules, through a combination of result variables and the properties linked to the constraint itself. The rules are meant to be executed from top to bottom, so that the first bound value will be used.

Property Production Rules
sh:resultSeverity
  1. The value of sh:severity of the constraint node
  2. sh:Violation as default
sh:focusNode
  1. The value of the variable focusNode
  2. The value of the variable this
sh:resultPath
  1. The value of the variable path, if this value is a IRI
sh:value
  1. The value of the variable value
sh:resultMessage
  1. The value of the variable message
  2. The values of sh:message of the subject of the sh:select or sh:ask triple. These string literals may include the names of any SELECT result variables via {?varName} or {$varName}. If the constraint is based on a constraint component, then the component's parameter names can also be used. These {?varName} and {$varName} blocks SHOULD be substituted with suitable string representations of the values of said variables.
sh:sourceConstraint
  1. The constraint that was validated against
sh:sourceShape
  1. The shape that was validated against

Injecting Annotation Properties into Validation Results

It is possible to inject additional annotation properties into the validation result nodes created for each solution of the SELECT result sets. Any such property needs to be declared via a value of sh:resultAnnotation at the subject of the sh:select or sh:ask triple. The values of sh:resultAnnotation are either IRIs or blank nodes with the following properties. In this table, the Value type column states the required SHACL class or datatype of the property values, and the Count column indicates the minimum and maximum number of values that the properties may have. If these value types and counts are violated then the shapes graph is invalid.

Property Value type Count Description
sh:annotationProperty rdf:Property 1 (mandatory) The annotation property that shall be set
sh:annotationVarName xsd:string 0..1 The name of the SPARQL variable to take the values from
sh:annotationValue 0..unlimited Constant RDF terms that shall be used as default values

For each solution of a SELECT result set, a SHACL Full processor MUST walk through the declared result annotations. The mapping from result annotations to SPARQL variables uses the following rules:

  1. If a sh:resultAnnotation has a value for the property sh:annotationVarName then the SHACL Full processor MUST look for the variable with the same name as the value of sh:annotationVarName
  2. Otherwise, the SHACL Full processor MUST use the local name of the value of sh:annotationProperty as the variable name

If a variable name could be determined, then the SHACL Full processor MUST copy the binding for the given variable as a value for the property specified using sh:annotationProperty into the validation result that is being produced for the current solution. If the variable has no binding in the result set solution, then the value of sh:annotationValue MUST be used, if present.

Here is a slightly complex example, illustrating the use of result annotations.

ex:ShapeWithPathViolationExample
	a sh:Shape ;
	sh:targetNode ex:ExampleRootResource ;
	sh:sparql [
		sh:resultAnnotation [
			sh:annotationProperty ex:time ;
			sh:annotationVarName "time"
		] ;
		sh:select """
			SELECT $this (ex:property1 AS ?path) (?first AS ?value) ?message ?time
			WHERE {
				$this ex:property1 ?first .
				?subject ex:property2 ?first .
				FILTER isBlank(?value) .
				BIND (CONCAT("The ", "message.") AS ?message) .
				BIND (NOW() AS ?time) .
			}
			""" ;
	] .
ex:ExampleRootResource
	ex:property1 ex:ExampleIntermediateResource .

ex:ExampleValueResource
	ex:property2 ex:ExampleIntermediateResource .

Which produces the following validation result nodes:

[] a sh:ValidationReport ;
	sh:conforms "false"^^xsd:boolean ;
	sh:result [
		a sh:ValidationResult ;
		sh:resultSeverity sh:Violation ;
		sh:focusNode ex:ExampleRootResource ;
		sh:resultPath ex:property1 ;
		sh:value ex:ExampleIntermediateResource ;
		sh:resultMessage "The message." ;
		sh:sourceConstraint [ the blank node of the sh:sparql above ] ;
		sh:sourceShape ex:ShapeWithPathViolationExample ;
		ex:time "2015-03-27T10:58:00"^^xsd:dateTime ;  # Example
	] .

SPARQL-based Constraint Components

SPARQL-based constraints as introduced in the previous section provide a lot of flexibility. However, SPARQL-based constraints may be hard to understand for some people or lead to repetition. Constraint components are a way to abstract the complexity of SPARQL and to declare high level reusable components similar to the Core constraint components. Such constraint components can be declared using the SHACL RDF vocabulary and thus shared and reused.

sh:ConstraintComponent is the SHACL class of all constraint components. Each constraint component specifies:

An Example Constraint Component

The following example demonstrates how SPARQL can be used to specify new constraint components using the SHACL Full language. The example implements sh:pattern and sh:flags using a SPARQL ASK query to validate that each value node matches a given regular expression. Note that this is only an example implementation and should not be considered normative.

sh:PatternConstraintComponent
	a sh:ConstraintComponent ;
	sh:parameter [
		sh:predicate sh:pattern ;
	] ;
	sh:parameter [
		sh:predicate sh:flags ;
		sh:optional true ;
	] ;
	sh:validator shimpl:hasPattern .

shimpl:hasPattern
	a sh:SPARQLAskValidator ;
	sh:message "Value does not match pattern {$pattern}" ;
	sh:ask "ASK { FILTER (!isBlank($value) && IF(bound($flags), regex(str($value), $pattern, $flags), regex(str($value), $pattern))) }" .

The following sections introduce the properties that constraint components may have. Some of these properties are independent of SPARQL-based execution and apply to constraint components based on other potential extension languages such as JavaScript too.

Parameter Declarations (sh:parameter)

The parameters of a constraint component are declared via the property sh:parameter. The objects of triples with sh:parameter as predicate have sh:Parameter as expected type.

Each parameter has exactly one value p for the property sh:predicate and the value is an IRI. The local name of an IRI is defined as the longest NCNAME at the end of the IRI, not immediately preceded by the first colon in the IRI. The parameter name is defined as the local name of the value of sh:predicate. To ensure that a correct mapping from parameters into SPARQL variables is possible, every parameter name

An sh:Parameter may have its property sh:optional set to true to indicate that the parameter is not mandatory. Every constraint component has at least one non-optional parameter.

The class sh:Parameter is defined as a SHACL subclass of sh:PropertyConstraint, and all properties that are applicable to property constraints may also be used for parameters. This includes descriptive properties such as sh:name and sh:description but also constraint parameters such as sh:class. Some implementations MAY use these constraint parameters to prevent the execution of constraint components with invalid parameter values.

Parameter pre-binding

Every parameter name is used as the name of a pre-bound variable for the constraint component the parameter belongs to. This variable can be used in the SPARQL queries of the constraint component and a SHACL Full processor MUST pre-bind it to the parameter value.

Label Templates (sh:labelTemplate)

The property sh:labelTemplate can be used at any constraint component to suggest how they could be rendered to humans. The values of sh:labelTemplate are strings (possibly with language tag) that can include the names of the declared parameters using the syntax {?varName} or {$varName}, where varName is the name of the SPARQL variable that corresponds to the parameter. At display time, these {?varName} and {$varName} blocks SHOULD be substituted with the actual parameter values. There may be multiple label templates for the same subject, assuming they do not have the same language tags.

Validators

For every supported context (i.e., property constraint or shape) the constraint component declares a suitable validator. For a given constraint, a validator is selected from the constraint component using the following rules, in order:

  1. For shapes, use one of the values of sh:shapeValidator, if present.
  2. For property constraints, use one of the values of sh:propertyValidator, if present.
  3. Otherwise, use one of the values of sh:validator.

If no suitable validator can be found, a SHACL Full processor ignores the constraint. The SHACL WG is seeking practical feedback on what the default behavior should be, and whether we should report violations in those cases.

SHACL Full includes two types of validators, based on SPARQL SELECT (for sh:shapeValidator and sh:propertyValidator) or SPARQL ASK queries (for sh:validator).

Validators based on SPARQL SELECT Queries

Validators that are SHACL instances of sh:SPARQLSelectValidator have exactly one string representation of a SPARQL SELECT query via the property sh:select. The value of sh:select is a valid SPARQL query using the aforementioned prefix handling rules. The query returns the result variable this in its SELECT clause. This type of validator can be used as values of sh:shapeValidator or sh:propertyValidator.

The following example illustrates the declaration of a constraint component based on a SPARQL SELECT query. It is a generalized variation of the SPARQL-based example constraint from the section on SPARQL-based constraints. That SPARQL query included two constants: the specific property ex:germanLabel and the language tag de. Constraint components make it possible to generalize such scenarios, so that constants get pre-bound with parameters. This allows the query logic to be reused in multiple places, without having to write any new SPARQL.

ex:LanguageConstraintComponentUsingSELECT
	a sh:ConstraintComponent ;
	rdfs:label "Language constraint component" ;
	sh:parameter [
		sh:predicate ex:lang ;
		sh:datatype xsd:string ;
		sh:minLength 2 ;
		sh:name "language" ;
		sh:description "The language tag, e.g. \"de\"." ;
	] ;
	sh:labelTemplate "Values are literals with language \"{$lang}\"" ;
	sh:propertyValidator [
		a sh:SPARQLSelectValidator ;
		sh:message "Values are literals with language \"{?lang}\"" ;
		sh:select """
			SELECT DISTINCT $this ?value
			WHERE {
				$this $PATH ?value .
				FILTER (!isLiteral(?value) || !langMatches(lang(?value), $lang))
			}
			"""
	] .

Once a constraint component has been declared (in a shapes graph), its parameters can be used as illustrated in the following example.

ex:LanguageExampleShape
	a sh:Shape ;
	sh:targetClass ex:Country ;
	sh:property [
		sh:predicate ex:germanLabel ;
		ex:lang "de" ;
	] ;
	sh:property [
		sh:predicate ex:englishLabel ;
		ex:lang "en" ;
	] .

The example shape above specifies the condition that all values of ex:germanLabel carry the language tag de while all values of ex:englishLabel have en as their language. These details are specified via two property constraints that have values for the ex:lang parameter required by the constraint component.

SELECT queries used in the context of property constraints use a special variable named PATH as a placeholder for the predicate or path used by the constraint. The only legal use of this variable is in the predicate position of a triple pattern. A query that uses the variable PATH in any other position is invalid.

A SHACL Full processor executes the provided SPARQL query on the data graph to produce validation results. In the context of property constraints, the SHACL Full processor will first substitute all occurrences of the variable PATH with the provided property path derived from the value of either sh:predicate or sh:path in the constraint. The resulting SPARQL query is then evaluated with the same pre-bound variables as outlined in the section for SPARQL-based Constraints ($this etc). Additionally, the value of each declared parameter of the constraint component needs to be pre-bound for the variable derived by the local name of the parameter's sh:predicate. For example, if a non-optional parameter declares sh:predicate ex:lang then the variable lang needs to be pre-bound. The result set of the SELECT query is turned into validation results using the same rules as outlined in the section for SPARQL-based Constraints. In addition to the result properties listed in that section, the property sh:sourceConstraintComponent MUST point at the IRI of the constraint component that has been evaluated. Furthermore, a SHACL Full processor MUST use any additional annotation properties that are associated with a SPARQL select validator via sh:resultAnnotation.

Validators based on SPARQL ASK Queries

Many constraint components are of the form in which all value nodes are tested individually against some boolean condition. Writing SELECT queries for these becomes burdensome, especially if a constraint component can be used for both property constraints and shapes. SHACL Full provides an alternative, more compact syntax for validators based on ASK queries. This type of validators can be used as values of the property sh:validator.

Validators that are SHACL instances of sh:SPARQLAskValidator point at exactly one string representation of a SPARQL ASK query via the property sh:ask. The value of sh:ask is a valid SPARQL query using the aforementioned prefix handling rules. The ASK queries return true if and only if a given value node (represented by the pre-bound variable value) conforms to the constraint.

Prior to evaluation, a SHACL Full processor transforms the provided ASK query into a SELECT query using the following templates. The resulting SELECT query can then be evaluated using the same algorithm as for SELECT-based validators. The processor drops the ASK keyword, any top-level dataset clauses and solution modifiers, leaving only the GroupGraphPattern including the outermost {...} pair. This block then substitutes ... in the template.

Template for sh:Shape context:

	SELECT $this ?value
	WHERE {
		BIND ($this AS ?value) .
		FILTER NOT EXISTS ...
	}

Template for sh:PropertyConstraint context:

	SELECT DISTINCT $this ?value
	WHERE {
		$this $PATH ?value .
		FILTER NOT EXISTS ...
	}
The WG is awaiting input from the SPARQL Maintenance (EXISTS) Community Group on potential changes to the semantics of EXISTS.

Note that the template above includes a DISTINCT keyword because a SPARQL path expression may return the same ?value multiple times, yet each value node is only validated once.

Once the corresponding template has been applied, the resulting SELECT query will be evaluated using the same approach as outlined above. Actual SHACL implementations may of course use a different approach internally, as long as the results are equivalent to the described approach.

The following example declares a constraint component using an ASK query.

ex:LanguageConstraintComponentUsingASK
	a sh:ConstraintComponent ;
	rdfs:label "Language constraint component" ;
	sh:parameter [
		sh:predicate ex:lang ;
		sh:datatype xsd:string ;
		sh:minLength 2 ;
		sh:name "language" ;
		sh:description "The language tag, e.g. \"de\"." ;
	] ;
	sh:labelTemplate "Values are literals with language \"{$lang}\"" ;
	sh:validator ex:hasLang .
	
ex:hasLang
	a sh:SPARQLAskValidator ;
	sh:message "Values are literals with language \"{$lang}\"" ;
	sh:ask """
		ASK {
			FILTER (isLiteral($value) && langMatches(lang($value), $lang))
		}
		""" .

Note that the validation condition implemented by an ASK query is "in the inverse direction" from its SELECT counterpart: ASK queries return true for value nodes that conform to the constraint, while SELECT queries return those value nodes that do not conform.

Evaluation

A constraint component is triggered for every constraint that has values for all non-optional parameters. TODO: This is unclear.

SPARQL-based Targets (sh:target)

SHACL Full provides facilities to specify custom targets, using sh:target. Similar to constraints, targets may either be SPARQL-based targets or SPARQL-based target types in a higher-level vocabulary.

Targets using sh:SPARQLTarget

SPARQL-based targets are SHACL instances of sh:SPARQLTarget, which is a SHACL subclass of sh:Target. The SPARQL queries linked to a target via sh:select are valid SPARQL 1.1 SELECT queries with this as the only result variable (after applying the prefix handling rules). The resulting focus nodes are the distinct bindings for the variable this.

While the SELECT queries can be used to identify all focus nodes for a given shape, SHACL processors sometimes also need to find all shapes for which a given node needs to be validated against. For this reason, the following semantic restriction applies to SELECT queries used in SPARQL-based targets. Informally, SHACL Full processors can derive an equivalent ASK query from the SELECT query, pre-bind the potential focus node, and check whether the potential focus node needs to be validated against the shape that has the given target. Formally, let A be a SPARQL ASK query that is produced by replacing the SelectClause with ASK in the outermost SELECT query. Let rs be the set of RDF terms returned as bindings for the variable this in the result table of the SELECT query. Then A returns true if and only if the variable this is pre-bound with a value from rs. SHACL Full processors MAY produce a failure if this rule is violated.

The following example illustrates a well-formed SPARQL-based target that produces all persons born in the USA:

ex:USCitizenShape
	a sh:Shape ;
	sh:target [
		a sh:SPARQLTarget ;
		sh:select """
			SELECT ?this
			WHERE {
				?this a ex:Person .
				?this ex:bornIn ex:USA .
			}
			""" ;
	] ;
	...

SPARQL-based Target Types

The class sh:TargetType can be used to declare high-level vocabularies for targets. The class sh:SPARQLTargetType is an rdfs:subClassOf sh:TargetType for target types based on SPARQL. Instances of this class specify a SPARQL SELECT query via the property sh:select, and this query has to fulfill the same syntactic and semantic rules as SPARQL-based Targets.

Similar to constraint components, such targets take parameters and the parameter values become pre-bound variables in the associated SPARQL queries. The parameter values of such targets cannot not be blank nodes, and the same target has no more than one value per parameter. Similar to constraint components, target types may also have values for the property sh:labelTemplate.

The following example declares a new SPARQL-based parameterizable target class that takes one parameter ex:country that gets mapped into the variable country in the corresponding SPARQL query to determine the resulting focus nodes.

ex:PeopleBornInCountryTarget
	a sh:SPARQLTargetType ;
	rdfs:subClassOf sh:Target ;
	sh:labelTemplate "All persons born in {$country}" ;
	sh:parameter [
		sh:predicate ex:country ;
		sh:name "country" ;
		sh:description "The country that the focus nodes are born in." ;
		sh:class ex:Country ;
		sh:minCount 1 ;
		sh:maxCount 1 ;
		sh:nodeKind sh:IRI ;
	] ;
	sh:select """
		SELECT ?this
		WHERE {
			?this a ex:Person .
			?this ex:bornIn $country .
		}
		""" .

ex:USCitizenShape
	a sh:Shape ;
	sh:target [
		a ex:BornInCountryTarget ;
		ex:country ex:USA ;
	] ;
	...

The set of focus nodes produced by such a target type consists of all bindings of the variable this in the result set, when the SPARQL SELECT query has been executed with the pre-bound parameter values.

Derived Values Constraints

It is a common scenario that certain property values are derived from other values. For example, the area of a rectangle is be the product of width and height, or an uncle of a person is a male sibling of a parent. SHACL Full includes a constraint parameter sh:derivedValues that can be used with property constraints to specify such constraints.

Constraint Component IRI: sh:DerivedValuesConstraintComponent

Parameters:
Property Summary
sh:derivedValues A blank node or IRI providing instructions on how to derive the values

The values of sh:derivedValues are SHACL instances of a SHACL subclass of sh:ValuesDeriver. sh:SPARQLValuesDeriver is the only SHACL subclass of sh:ValuesDeriver defined by SHACL Full. Each SHACL instance of sh:SPARQLValuesDeriver has exactly one value for the property sh:select that can be used to produce the values that the property is expected to have. The values of sh:select are SPARQL 1.1 SELECT queries (using the aforementioned prefix declaration mechanism) that return the variable value only. These queries can access the current focus node via the variable this and produce at least one binding for the variable value for all derived values.

TEXTUAL DEFINITION
Let S be the set of RDF terms produced by the values deriver for the focus node. A validation result MUST be produced for every value node that is not in S, and for every member of S that is not a value node. The produced validation results MUST have the focus node as its sh:focusNode, the sh:predicate or sh:path as its sh:resultPath, and the missing or extra value as its sh:value.

The following example illustrates the use of sh:derivedValues to specify that the value of the property ex:area is the product of the value of ex:width and sh:height.

ex:RectangleShape
	a sh:Shape ;
	sh:property [
		sh:predicate ex:width ;
		sh:datatype xsd:integer ;
		sh:maxCount 1 ;
	] ;
	sh:property [
		sh:predicate ex:height ;
		sh:datatype xsd:integer ;
		sh:maxCount 1 ;
	] ;
	sh:property [
		sh:predicate ex:area ;
		sh:datatype xsd:integer ;
		sh:derivedValues [
			a sh:SPARQLValuesDeriver ;
			sh:select """
				SELECT ?value
				WHERE {
					$this ex:width ?width .
					$this ex:height ?height .
					BIND (?width * ?height AS ?value) .
				}
				""" ;
		] ;
	] .

SPARQL Functions

SHACL functions declare operations that produce an RDF term based on zero or more parameters and an input RDF graph (or dataset). Functions can be called within SPARQL queries to encapsulate complex logic of other SPARQL queries, or executable logic in other languages such as JavaScript. However, the general declaration mechanism for SHACL functions is independent from SPARQL and may also be exploited by other environments.

Syntax of SPARQL Functions

Functions that encapsulate a SPARQL query are SHACL instances of sh:SPARQLFunction, which is a SHACL subclass of the more general class sh:Function. Such functions have exactly one value for either sh:ask or sh:select, linking to a SPARQL query.

The following example illustrates the declaration of a function based on a simple mathematical SPARQL query.

ex:exampleFunction
	a sh:SPARQLFunction ;
	rdfs:comment "Computes the sum of its two parameters ?op1 and ?op2." ;
	sh:parameter [
		sh:predicate ex:op1 ;
		sh:datatype xsd:integer ;
		sh:description "The first operand" ;
	] ;
	sh:parameter [
		sh:predicate ex:op2 ;
		sh:datatype xsd:integer ;
		sh:description "The second operand" ;
	] ;
	sh:returnType xsd:integer ;
	sh:select """
		SELECT ($op1 + $op2 AS ?result)
		WHERE {
		}
		""" .

Using the declaration above, SPARQL engines with SHACL Full support can install a new SPARQL function based on the SPARQL 1.1 Extensible Value Testing mechanism. Such engines are then able to handle expressions such as ex:exampleFunction(40, 2), producing 42, as illustrated in the following SPARQL query.

SELECT ?subject
WHERE {
	?subject ex:myProperty ?value .
	FILTER (ex:exampleFunction(?value, 2) = 42) .
}

The following sections introduce the properties that such functions may have.

Function Parameters

The parameters of a function are linked to its sh:Function via the property sh:parameter. The objects of triples with sh:parameter as predicate have sh:Parameter as expected type.

Each parameter has exactly one value for the property sh:predicate. The values of sh:predicate are IRIs, and follow the following restrictions:

Parameters are ordered, corresponding to the notation of function calls in SPARQL such as ex:exampleFunction(?param1, ?param2). The ordering of function parameters is determined as follows:

  1. Parameters are ordered in ascending order by the parameter's numeric values of sh:order.
  2. Parameters that do not declare an sh:order are placed after those that have.
  3. Parameters that do not declare an sh:order are ordered by the local names of their declared sh:predicates.

Each parameter may have its property sh:optional set to true to indicate that the parameter is not mandatory.

sh:returnType

A function may declare a single return type via sh:returnType. This information may serve for documentation purposes, only. However, in some execution languages such as JavaScript, the declared sh:returnType may inform a processor how to cast a native value into an RDF term.

Evaluation of Functions

SHACL instances of sh:SPARQLFunction have exactly one value for either sh:ask or sh:select. The values of this property are strings that can be parsed into SPARQL queries of type ASK (for sh:ask) or SELECT (for sh:select), using the aforementioned prefix declaration mechanism. SELECT queries return exactly one result variable and should not use the SELECT * syntax. In the SPARQL query, the SPARQL processor needs to pre-bind variables based on the provided parameters of the function call. For ASK queries, the function's return value is the result of the ASK query execution, i.e. true or false. For SELECT queries, the function's return value is the binding of the (single) result variable of the first solution in the result set. Since all other bindings will be ignored, such SELECT queries should only return a single result variable and at most one solution. If the result variable is unbound, then the function generates a SPARQL error.

Some processors MAY ignore the specified SPARQL query and rely on an alternative (possibly native) implementation instead, as long as the functions return the same values as the specified SPARQL query. This can be used to optimize frequently needed functions. Some processors MAY even use the SPARQL query to rewrite other SPARQL queries via inlining techniques.

Entailment

By default, SHACL does not assume any entailment regime [[!sparql11-entailment]] to be activated on the data graph. However, the property sh:entailment can be used in the shapes graph to instruct a SHACL processor to ensure that a given entailment is activated on the data graph. The values of sh:entailment are IRIs, with common use cases covered by [[!sparql11-entailment]].

SHACL processors are not required to support any entailment regimes. If an entailment regime is provided in the shapes graph which is not supported by the SHACL processor, the validation MUST produce a failure.

Appendix

Pre-binding of Variables in SPARQL Queries

The following definition of what pre-binding means has not been approved by the WG yet, and is work in progress. The WG is also awaiting input from the SPARQL Maintenance (EXISTS) Community Group.

Some features of the SPARQL-based extension mechanism of SHACL Full rely on the concept of pre-binding of variables. Although variations of this concept are supported by several existing SPARQL implementations, there is no formal definition of pre-binding in the SPARQL 1.1 specifications. The goal of this section is to illustrate the effect of pre-binding to users and implementers. Note however that the following definition is not meant to serve as recommendation for an actual implementation strategy.

Pre-binding a variable with a value means that the SPARQL processor needs to evaluate all occurrences of variables with that same name (including occurrences in inner targets and nested SELECT queries) so that they have the provided value. In other words, whenever a SPARQL processor evaluates a pre-bound variable, it must use the given value.

Pre-bound variables

SHACL Full defines two forms of variable pre-binding:

  1. Global pre-bound variables that are available in all SPARQL-based constraints, SPARQL-based constraint components and SPARQL-based targets.
  2. Local pre-bound variables available only within a SPARQL-based constraint component and defined through the component parameters

The variable PATH has a special treatment in SHACL property constraint components and MUST be processed before any other pre-bound variable. SHACL Full processors MUST perform string substitution of every occurrence of the variable PATH to the generated SPARQL property path before performing any pre-binding.

The variable predicate is not a pre-bound variable in SHACL and will be treated as a normal SPARQL variable. Should we disallow the variable predicate instead to avoid confusion?

Acknowledgements

Many people contributed to this specification, including members of the RDF Data Shapes Working Group. We especially thank the following:

Arnaud Le Hors (chair), Jim Amsden, Iovka Boneva, Karen Coyle, Richard Cyganiak, Michel Dumontier, Holger Knublauch, Dimitris Kontokostas, Jose Labra, Peter Patel-Schneider, Eric Prud'hommeaux, Arthur Ryman (who also served as a co-editor until Feb 2016), Harold Solbrig, Simon Steyskal, Ted Thibodeau