Work / Demonstration · SHACL Validation

Ontology Validation · Closed-World Gate

SHACL Validator: enforcing closed-world data contracts

OWL ontologies operate under the Open-World Assumption: a missing field is treated as merely unstated, not invalid. Operational pipelines need a deterministic boundary gate that rejects incomplete or mistyped records before downstream consumers see them.

Worked synthetic dataset

Three invoices against one sh:NodeShape

The interactive Shinylive app runs rdflib and pyshacl directly in WebAssembly. It preloads three synthetic invoice records against ex:InvoiceShape so you can see one passing record and two distinct constraint violations.

Data graph (Turtle)

invoice-1001 is valid; invoice-1002 omits ex:paymentTerms; invoice-1003 provides a plain string instead of xsd:decimal.

@prefix ex: <https://example.org/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

ex:invoice-1001 a ex:Invoice ;
    ex:amountDue "1250.00"^^xsd:decimal ;
    ex:paymentTerms "Net 30" .

ex:invoice-1002 a ex:Invoice ;
    ex:amountDue "980.50"^^xsd:decimal .

ex:invoice-1003 a ex:Invoice ;
    ex:amountDue "twelve hundred" ;
    ex:paymentTerms "Net 15" .

Shapes graph (SHACL)

ex:InvoiceShape targets ex:Invoice and requires both a single decimal ex:amountDue and at least one ex:paymentTerms.

@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix ex: <https://example.org/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

ex:InvoiceShape a sh:NodeShape ;
    sh:targetClass ex:Invoice ;
    sh:property [
        sh:path ex:amountDue ;
        sh:datatype xsd:decimal ;
        sh:minCount 1 ; sh:maxCount 1 ;
        sh:message "Every invoice needs exactly one decimal amount due." ;
    ] ;
    sh:property [
        sh:path ex:paymentTerms ;
        sh:minCount 1 ;
        sh:message "Every invoice needs payment terms." ;
    ] .

Deterministic output

Validation report and SPARQL extraction

pyshacl.validate(data, shacl_graph=shapes) returns a boolean conformance flag and an RDF report graph. The app queries that report graph with SPARQL to produce an auditable row per violation.

Focus node Property path Severity SHACL message
ex:invoice-1001 — Conforms Satisfies both ex:amountDue and ex:paymentTerms constraints.
ex:invoice-1002 ex:paymentTerms Violation Every invoice needs payment terms.
ex:invoice-1003 ex:amountDue Violation Every invoice needs exactly one decimal amount due.
SPARQL query used to extract sh:ValidationResult rows
PREFIX sh: <http://www.w3.org/ns/shacl#>
SELECT ?focus ?path ?severity ?message WHERE {
  ?r a sh:ValidationResult ; sh:focusNode ?focus ; sh:resultSeverity ?severity .
  OPTIONAL { ?r sh:resultPath ?path }
  OPTIONAL { ?r sh:resultMessage ?message }
} ORDER BY STR(?focus)

Where SHACL gates fit in production

Keep your OWL ontology reusable across teams and place workflow-specific required fields, datatypes, and cardinality bounds in SHACL shapes at ingestion, API, and LLM structured-output boundaries.

Scope & limits

  • Synthetic sample: the three invoice records are minimal examples designed to isolate datatype and cardinality checks.
  • Browser execution: Turtle parsing and pyshacl run client-side in Pyodide; nothing is sent to a server.
  • Structure vs. business truth: SHACL verifies shape conformance, not whether an invoice amount matches an external ERP ledger.