Skip to content

Commit

Permalink
Merge pull request #214 from zazuko/shacl-validate-command
Browse files Browse the repository at this point in the history
feat(shacl): validate command
  • Loading branch information
tpluscode authored Nov 27, 2023
2 parents 880569f + 78e9681 commit ede9271
Show file tree
Hide file tree
Showing 9 changed files with 271 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-crabs-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"barnard59-shacl": minor
---

Added a pipeline command which validates quads from standard input
51 changes: 30 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions packages/shacl/lib/getShapes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { resolve } from 'path'

export default async function getShapes(pathOrUri) {
let url

try {
url = new URL(pathOrUri)
} catch {
const path = resolve(this.basePath, pathOrUri)
return this.env.fromFile(path)
}

const response = await this.env.fetch(url)
let contentType = response.headers['content-type']
if (!contentType) {
this.logger.warn(`No content-type header found for ${url}. Trying n-triples`)
contentType = 'application/n-triples'
}

const parserStream = this.env.formats.parsers.import(contentType, response.body)
if (!parserStream) {
throw new Error(`No parser found for ${contentType}`)
}

return parserStream
}
8 changes: 8 additions & 0 deletions packages/shacl/manifest.ttl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@base <https://barnard59.zazuko.com/operations/shacl/> .
@prefix b59: <https://barnard59.zazuko.com/vocab#> .
@prefix code: <https://code.described.at/> .
@prefix p: <https://pipeline.described.at/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
Expand All @@ -16,3 +17,10 @@
code:implementedBy [ a code:EcmaScriptModule;
code:link <node:barnard59-shacl/report.js#shacl>
].

<command/validate>
a b59:CliCommand ;
b59:command "validate" ;
rdfs:label "Validates the RDF in standard input against a SHACL document" ;
b59:source "barnard59-shacl/pipeline/validate.ttl" ;
.
19 changes: 15 additions & 4 deletions packages/shacl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,28 @@
"dependencies": {
"@rdfjs/fetch": "^3.0.0",
"is-stream": "^3.0.0",
"readable-stream": "3 - 4",
"rdf-validate-shacl": "^0.5.1"
"rdf-validate-shacl": "^0.5.1",
"readable-stream": "3 - 4"
},
"peerDependencies": {
"barnard59-base": "^2.1.0",
"barnard59-formats": "^2.1.0",
"barnard59-rdf": "^3.0.0"
},
"devDependencies": {
"@rdfjs/to-ntriples": "^2.0.0",
"barnard59-env": "^1.0.0",
"@tpluscode/rdf-string": "^1.1.0",
"assert-throws-async": "^3.0.0",
"barnard59-core": "^5.1.0",
"barnard59-env": "^1.0.0",
"barnard59-test-support": "*",
"chai": "^4.3.4",
"express": "^4.18.2",
"express-as-promise": "^1.2.0",
"get-stream": "^8.0.1",
"rdf-dataset-ext": "^1.1.0",
"sinon": "^15"
"sinon": "^15",
"string-to-stream": "^3.0.1"
},
"mocha": {
"require": "../../test/mocha-setup.cjs"
Expand Down
61 changes: 61 additions & 0 deletions packages/shacl/pipeline/validate.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix p: <https://pipeline.described.at/> .
@prefix base: <https://barnard59.zazuko.com/operations/base/> .
@prefix n3: <https://barnard59.zazuko.com/operations/formats/n3/> .
@prefix ntriples: <https://barnard59.zazuko.com/operations/formats/ntriples/> .
@prefix getDataset: <https://barnard59.zazuko.com/operations/rdf/getDataset> .
@prefix shacl: <https://barnard59.zazuko.com/operations/shacl/> .
@prefix code: <https://code.described.at/> .

@base <http://barnard59.zazuko.com/pipeline/shacl/> .

_:shapes a p:Variable ;
p:name "shapes" ;
rdfs:label "URL or path of the shapes graph" ;
.

<validate>
a p:Pipeline, p:Readable ;
p:variables [ p:variable _:shapes ] ;
p:steps
[
p:stepList
(
[ base:stdin () ]
<_validate>
)
] ;
.

<_validate>
a p:Pipeline, p:Writable, p:Readable ;
p:steps
[
p:stepList
(
[ n3:parse () ]
[ getDataset: () ]
[ shacl:report ( _:getShapes ) ]
[ base:flatten () ]
[ ntriples:serialize () ]
)
] ;
.

_:getShapes a p:Pipeline, p:ReadableObjectMode ;
p:steps
[
p:stepList
(
[
a p:Step ;
code:implementedBy
[
a code:EcmaScriptModule ;
code:link <file:../lib/getShapes.js#default> ;
] ;
code:arguments ( "shapes"^^p:VariableName ) ;
]
)
]
.
19 changes: 19 additions & 0 deletions packages/shacl/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,22 @@ By default, the pipeline will stop when 500 validation errors are encountered ac
[ code:name "maxErrors" ; code:value 0 ]
] .
```


## Commands

Peer dependencies must be explicitly installed

```
npm i barnard59-base barnard59-formats barnard59-rdf
```

### validate

Reads standard input and validates the data against the given SHACL shapes.

```bash
cat data.ttl | barnard59 shacl validate --shapes https://example.com/shapes.ttl
```

Shapes can also be path relative to the working directory.
Loading

0 comments on commit ede9271

Please sign in to comment.