1.0.0-alpha.1 • Published 2 years ago

jsonld-schema-test v1.0.0-alpha.1

Weekly downloads
-
License
-
Repository
github
Last release
2 years ago

JSON-LD Schema

It is a library for validating JSON-LD data graphs against a JSON-LD W3C SHACL shapes graph. It enables use-cases like metadata definition and validation based on a schema defined using standard specs (SHACL).

Usage

The library handles SHACL based data graph validation including data graphs loading and parsing.

Validation

import { ShaclSchema } from 'jsonld-schema'

// Validate a data graph against a shapes graph - both as JSON-LD strings
async function validate1() {
    const shapesGraph = "" // Supply JSON-LD SHACL shapes graph
    // Initialise schema instance using the shapes graph
    const schema = await ShaclSchema.fromShapesGraph(shapesGraph);
    const dataGraph = "" // Supply JSON-LD data graph to be validated
    // Validate data graph against schema
    const report = await schema.validateDataGraph(dataGraph)

  // Check conformance: `true` or `false`
    console.log(report.conforms)

    for (const result of report.results) {
    // See https://www.w3.org/TR/shacl/#results-validation-result for details
    // about each property
        console.log(result.resultMessage)
        console.log(result.resultPath)
        console.log(result.focusNode)
        console.log(result.resultSeverity)
        console.log(result.sourceConstraintComponent)
        console.log(result.sourceShape)
        console.log(result.value)
    }
}

validate1();

// Schema validation using shapes graph IRI, and data IRI/body
async function validate2() {
    // Initialise schema instance using the shapes graph IRI
    const schema = await ShaclSchema.fromShapesGraph("https://futureverse-metadata-schema.s3.ap-southeast-2.amazonaws.com/do-not-change/EmojiRange/1.0");
    const dataGraph = "" // Supply JSON-LD data graph to be validated
    
    // Validate data graph string against SHACL shape graph
    const report1 = await schema.validateDataGraph(dataGraph)
    
    // Validate data graph IRI against schema
    const report2 = await schema.validateDataGraphByIri("https://futureverse-metadata-schema.s3.ap-southeast-2.amazonaws.com/do-not-change/emoji-sapphire-metadata.jsonld")

}

validate2();

// Schema validation using data grapg IRI containing reference to SHACL shape in a predicate IRI (http://www.w3.org/ns/shacl#shapesGraph)
async function validate3() {
    // Initialise schema instance using the shapes graph IRI
    const schema = await ShaclSchema.fromDataGraphIri("https://futureverse-metadata-schema.s3.ap-southeast-2.amazonaws.com/do-not-change/emoji-sapphire-metadata.jsonld");

    // Validate data graph against shapes graph dereferenecd from the IRI
    const report = await schema.validate()

}

validate3();

Running the tests

$ npm test

Limitations

jsonld-schema does not support SHACL-SPARQL constraints as it is based on the library rdf-validate-shacl which doesn't support it currently.