Ontology SHACL Validation
Validates RDF instance data against SHACL shapes. If you don't have shapes yet,
the script auto-generates minimal ones from owl:Class + rdfs:domain/range axioms.
Single script: scripts/shacl_validate.py
Auto-generated shapes set sh:nodeKind appropriately: sh:BlankNodeOrIRI for
object properties, sh:Literal for datatype properties. This prevents false
violations when instance data uses blank nodes.
What It Detects
| Issue | Example |
|---|---|
| Missing required property | :Person has no :name but owl:minCardinality 1 → violation |
| Wrong datatype | :age "twenty" but SHACL shape expects xsd:integer |
| Cardinality violation | Multiple values for a functional property |
| Missing classes | Resources of a class that has no instances (info-only) |
Setup
cd <your-ontology-repo>
python3 -m venv .venv
source .venv/bin/activate
pip install rdflib pyshacl
After finishing: deactivate the venv and remove it:
deactivate && rm -rf .venvSkip this if the user asks to keep the environment.
Usage
# Auto-generate shapes from schema axioms
python scripts/shacl_validate.py . -o SHACL_REPORT.md
# Use your own shapes file
python scripts/shacl_validate.py . --shapes shapes.ttl -o report.md
# JSON for CI
python scripts/shacl_validate.py . --format json
Standardized Report
All skills support --format report which outputs a common JSON schema:
{
"skill": "skill-name",
"summary": {"errors": N, "warnings": N, "info": N},
"issues": [{"file": ".ttl", "element": ":Class", "message": "...",
"severity": "error|warning|info", "check": "RULE",
"suggestion": "fix"}]
}
This format is consumed by ontology-full-audit to produce unified reports.
Output Files
Never write files into the repository without permission. Before generating
any report or output file, ask the user where to save it (e.g. -o ../report.md
or an absolute path outside the repo). The default output path in script
examples is only a suggestion — always confirm with the user first.
Important Rules
Auto-generated shapes are minimal. They only cover cardinality constraints from
owl:minCardinality/owl:maxCardinalityrestrictions. For richer validation (pattern, datatype, class membership), write explicit SHACL shapes.SHACL validates instances, not schemas. The script separates shape definitions from data triples automatically. If you have no instances yet, the validation will pass trivially.
Combine with OOPS. OOPS checks schema design pitfalls. SHACL checks data quality. Together they cover the full ontology lifecycle.