Skip to content

Commit

Permalink
Validate after vocabulary enrichment #289
Browse files Browse the repository at this point in the history
  • Loading branch information
sroertgen committed Jan 30, 2024
1 parent 1c954b0 commit 219b738
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
9 changes: 5 additions & 4 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,19 @@ exports.onPreBootstrap = async ({ createContentDigest, actions, getNode }) => {
console.info(`Found these turtle files:`)
ttlFiles.forEach((e) => console.info(e))
for (const f of ttlFiles) {
const ttlString = fs.readFileSync(f).toString()
const doc = await jsonld.fromRDF(ttlString, { format: "text/turtle" })
const compacted = await jsonld.compact(doc, context.jsonld)

if (config.failOnValidation) {
try {
console.info("Validating: ", f)
await validate("shapes/skohub.shacl.ttl", f)
await validate("shapes/skohub.shacl.ttl", f, doc)
} catch (e) {
console.error(e)
throw e
}
}
const ttlString = fs.readFileSync(f).toString()
const doc = await jsonld.fromRDF(ttlString, { format: "text/turtle" })
const compacted = await jsonld.compact(doc, context.jsonld)

const conceptSchemeIds = compacted["@graph"]
.filter((node) => node.type === "ConceptScheme")
Expand Down
27 changes: 15 additions & 12 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dependencies": {
"@emotion/react": "^11.10.5",
"@gatsbyjs/reach-router": "^2.0.0",
"@rdfjs/dataset": "^2.0.1",
"@rdfjs/parser-n3": "^2.0.1",
"crypto-browserify": "^3.12.0",
"dotenv": "^16.0.3",
Expand All @@ -21,7 +22,7 @@
"gatsby-transformer-sharp": "^5.0.0",
"graceful-fs": "^4.2.8",
"js-yaml": "^4.1.0",
"jsonld": "^8.2.0",
"jsonld": "^8.3.2",
"lodash.escaperegexp": "^4.1.2",
"markdown-to-jsx": "^7.1.8",
"n3": "^1.16.3",
Expand Down
17 changes: 15 additions & 2 deletions src/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const fs = require("fs")
const f = import("rdf-ext")
const PN3 = import("@rdfjs/parser-n3")
const shacl = import("rdf-validate-shacl")
const jsonld = require("jsonld")
const datasetFactory = import("@rdfjs/dataset")
const N3 = require("n3")

async function loadDataset(filePath) {
const ParserN3 = await PN3
Expand All @@ -11,19 +14,29 @@ async function loadDataset(filePath) {
return factory.default.dataset().import(parser.import(stream))
}

async function loadDataFromJsonld(doc) {
const nquads = await jsonld.toRDF(doc, { format: "application/n-quads" })
const parser = new N3.Parser({ format: "N-Triples" })
const parsed = parser.parse(nquads)
const dFactory = await datasetFactory
const dataset = dFactory.default.dataset(parsed)
return dataset
}

/**
* Validates a file against a given shape.
* @param {string} shapePath - Path to shape file
* @param {string} filePath - Path to file to validate
* @param {Object} doc - JSON-LD document
* @returns {boolean} Validation result
* @throws {Error} Throws an error if the validation fails
*/
async function validate(shapePath, filePath) {
async function validate(shapePath, filePath, doc) {
const factory = await f
const SHACLValidator = await shacl

const shapes = await loadDataset(shapePath)
const data = await loadDataset(filePath)
const data = await loadDataFromJsonld(doc)
const validator = new SHACLValidator.default(shapes, {
factory: factory.default,
})
Expand Down

0 comments on commit 219b738

Please sign in to comment.