Constraint Component Reference

This section describes each of the constraint components that Graph Lakehouse supports for node and property shapes. Certain constraints are valid only in property shapes. And other constraints are valid in both node and property shapes. When a constraint is applied to a particular property—by including the sh:path <property_uri> predicate in the shape—the specified condition applies only to that property. For example the following snippet from a shapes graph creates a condition that requires values for the age property in the Person class to be between 0 and 130 (inclusive):

ex:PersonShape a sh:NodeShape ;
  sh:targetClass ex:Person ;
  sh:property [
    sh:path ex:age ;
    sh:minInclusive 0 ;
    sh:maxInclusive 130 ;
] 

If a constraint is applied to a node shape (the sh:path <property_uri> predicate is excluded), the condition applies to all properties associated with the focus node. For example the following constraints apply to all properties related to the Person node. All properties are required to have values between 0 and 130 (inclusive):

ex:PersonShape a sh:NodeShape ;
  sh:targetClass ex:Person ;
  sh:property [
    sh:minInclusive 0 ;
    sh:maxInclusive 130 ;
]

Constraint Types

Cardinality Constraints

Constraint Shape Type Data Type Description
sh:maxCount property int This constraint sets a limit on the maximum number of values for a property. The following example limits the lastName property to one value.

sh:property [ sh:path ex:lastName ; sh:maxCount 1; sh:datatype xsd:string; ]

sh:minCount property int This constraint requires a minimum number of values for a property. The following example requires the lastName property to have one value.

sh:property [ sh:path ex:lastName ; sh:minCount 1; sh:maxCount 1; sh:datatype xsd:string ]

Logical Constraints

Constraint Shape Type Data Type Description
sh:or node, property URI list This constraint requires a node or property to conform to at least one of the listed shapes. The following example requires the child property to contain a value that conforms to the biological or adopted shapes.

sh:property [ sh:path ex:child ; sh:or (ex:biological ex:adopted) ]

sh:and node, property URI list This constraint requires a node or property to conform to all of the listed shapes. The following example requires the employee property to conform to the person and organization shapes.

sh:property [ sh:path ex:employee ; sh:and (ex:person ex:organization) ]

sh:not node, property URI This constraint defines a condition where a node or property must not conform to any of the listed shapes. The following example specifies that the president property cannot conform to the felon shape.

sh:property [ sh:path ex:president ; sh:not ex:felon ; ]

sh:xone node, property URI list This constraint defines a condition where a node or property must conform to one and only one of the listed shapes. The following example specifies that the child property must conform to either the biological or adopted shape but cannot conform to both shapes.

sh:property [ sh:path ex:child ; sh:xone (ex:biological ex:adopted) ]

Other Constraints

Constraint Shape Type Data Type Description
sh:in node, property URI or literal list This constraint restricts a node or property value to be one of those specified. The following example restricts the continent property to contain one of three possible values.

sh:property [ sh:path ex:continent ; sh:in (ex:Asia, ex:Europe, ex:NorthAmerica); ]

sh:closed,
sh:ignoredProperties
node boolean,
URI list
The closed and optional ignoredProperties constraints can be used to limit the properties that are allowed for a node. If sh:closed true, only the properties that are described in the shape are valid. You can include ignoredProperties if you want to list any properties that are not described in the shape but should be allowed for the target. In the following example, the only allowed properties for Employee are rdf:type, id, and record.

ex:EmployeeShape a sh:NodeShape; sh:targetClass ex:Employee; sh:closed true; sh:ignoredProperties (rdf:type) sh:property [ sh:path ex:id ; sh:datatype xsd:long; ] sh:property [ sh:path ex:record ; sh:class sh:EmployeeRecord; ]

sh:hasValue node, property URI, literal This constraint requires a node or property to have at least one value that matches the specified sh:hasValue. The following example requires the genre property for the Book node to have at least one value that is ex:Mystery.

ex:BookShape a sh:NodeShape ; sh:targetClass ex:Book ; sh:property [ sh:path ex:genre ; sh:hasValue ex:Mystery ; ]

sh:sparql,
sh:select
node, property URI,
string
This SPARQL-based constraint can be used to set restrictions based on the specified SPARQL SELECT query. The following pre-bound variables are known in the SPARQL query:
  • $this: subject
  • $PATH: predicate
  • ?value: object

The following example creates a requirement for email addresses to be strings that end in .com.

sh:property [ sh:path ex:email ; sh:sparql [ a sh:SPARQLConstraint ; sh:message "Email is a string ending in .com" ; sh:select """ SELECT $this ?value WHERE { $this $PATH ?value . FILTER(DATATYPE(?value) != xsd:string || (lcase(substr(str(?value),strlen(str(?value))-3)) not in (".com",".net",".gov",".edu"))). } """ ; ]; ]

Property Pair Constraints

Constraint Shape Type Data Type Description
sh:equals property URI This constraint requires a property to have a value that is equal to the specified value (value1 = value2). The following example requires the value for the firstName property to equal the value of givenName.

sh:property [ sh:path ex:firstName ; sh:equals ex:givenName; ]

sh:disjoint property URI This constraint requires a property to have a value that is not equal to the specified value (value1 != value2). The following example specifies that the prefix label must not equal the label value.

sh:property [ sh:path ex:prefLabel ; sh:disjoint ex:label ; ]

sh:lessThan property URI This constraint requires a property to have a value that is less than the specified value (value1 < value2). The following example requires the startDate value to be less than the endDate value.

sh:property [ sh:path ex:startDate ; sh:lessThan ex:endDate; ]

sh:lessThanOrEquals property URI This constraint requires a property to have a value that is less than or equal to the specified value (value1 <= value2). The following example requires the startDate value to be less than or equal to the endDate value.

sh:property [ sh:path ex:startDate ; sh:lessThanOrEquals ex:endDate; ]

Shape-Based Constraints

Constraint Shape Type Data Type Description
sh:node node, property URI list This constraint requires a node or property to conform to the specified shape. The following example requires that the address property conforms to the AddressShape.

sh:property [

sh:path ex:address ;

sh:minCount 1 ; sh:node ex:AddressShape ; ]

sh:property node, property URI list This constraint is used to define the property shape for a node or property.

String-Based Constraints

Constraint Shape Type Data Type Description
sh:minLength node, property int This constraint requires a literal value or URI to meet a minimum character length. The following example requires the password property to have a value that is at least 8 characters.

sh:property [ sh:path ex:password ; sh:minLength 8 ; ]

sh:maxLength node, property int This constraint sets a limit on the number of characters a literal value or URI can have. The following example limits values for the country property to 60 characters.

sh:property [ sh:path ex:country ; sh:maxLength 60 ; ]

sh:pattern,
sh:flags
node, property string,
string
The pattern and optional flags constraint can be included to require a property or node to match a regular expression pattern. For the supported regex syntax, see the Regular Expression Syntax section of the W3C XQuery 1.0 and XPath 2.0 Functions and Operators specification.

Include flags if you want to include optional modifier flags that further define the pattern. See the Flags section of the W3C Functions and Operators specification.

The following example requires the zipcode to match a string that consists of exactly five digits (0-9).

sh:property [ sh:path ex:zipcode ; sh:pattern "^\\d{5}$"; ]

sh:languageIn property string list This constraint limits the language tags that are allowed for a property. The following example limits the description property to English, German, or French.

sh:property [ sh:path ex:description ; sh:languageIn ("en", "de", "fr"); ]

sh:uniqueLang property boolean This constraint creates a condition where no two values can have the same language tag. Each value must have a unique tag. The following example requires each label value to have a unique language tag.

sh:property [ sh:path ex:label ; sh:uniqueLang true ; ]

Value Range Constraints

Constraint Shape Type Data Type Description
sh:minExclusive node, property literal This constraint sets the minimum value for a node or property, excluding the value that is specified. The following example requires the minimum value for the length property to be greater than 0.

sh:property [ sh:path ex:length ; sh:minExclusive 0; ]

sh:maxExclusive node, property literal This constraint sets the maximum value for a node or property, excluding the value that is specified. The following example requires the maximum value for the price property to be less than 100.00.

sh:property [ sh:path ex:price ; sh:maxExclusive 100.00; ]

sh:minInclusive node, property literal This constraint sets the minimum value for a node or property, including the value that is specified. The following example requires the minimum value for the age property to be greater than or equal to 0.

sh:property [ sh:path ex:age ; sh:minInclusive 0; ]

sh:maxInclusive node, property literal This constraint sets the maximum value for a node or property, including the value that is specified. The following example requires the maximum value for the age property to be less than or equal to 120.

sh:property [ sh:path ex:age ; sh:maxInclusive 120; ]

Value Type Constraints

Constraint Shape Type Data Type Description
sh:nodeKind node, property URI This constraint requires the values for a node or property to be of a certain type. Valid nodeKind values are:
  • sh:IRI: The value must be an IRI.
  • sh:BlankNode: The value must be a blank node.
  • sh:Literal: The value must be a literal.
  • sh:BlankNodeOrIRI: The value must be a blank node or IRI.
  • sh:BlankNodeOrLiteral: The value must be a blank node or literal.

The following example requires the birthDate property to contain literal values.

sh:property [ sh:path ex:birthDate ; sh:nodeKind sh:Literal ; ]

sh:datatype node, property URI

This constraint requires each node or property value to be the specified data type. The following example requires the age property to be an integer.

sh:property [ sh:path ex:age ; sh:datatype xsd:integer ; ]

sh:class node, property URI list This constraint requires each node or property to have an rdf:type that matches one of the values specified in sh:class. The following example requires the address property to be a PostalAddress.

sh:property [ sh:path ex:address ; sh:class ex:PostalAddress; ]