
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
shex.js javascript implementation of Shape Expressions (try online)
This shex npm package is a meta-package which includes the top-level @shexjs/ packages.
npm install --save shex
You can validate RDF data using the bin/validate executable or the lib/ShExValidation library described below.
Validate something in HTTP-land:
./node_modules/shex/bin/validate \
-x http://shex.io/examples/Issue.shex \
-d http://shex.io/examples/Issue1.ttl \
-s http://shex.io/examples/IssueShape \
-n http://shex.io/examples/Issue1
That validates node http://shex.io/examples/Issue in http://shex.io/examples/Issue1.ttl against shape http://shex.io/examples/IssueShape in http://shex.io/examples/Issue.shex.
The result is a JSON structure which tells you exactly how the data matched the schema.
{
"type": "test",
"node": "http://shex.io/examples/Issue1",
"shape": "http://shex.io/examples/IssueShape",
"solution": {
...
}
}
Had we gotten a null, we'd know that the document was invalid with respect to the schema.
See the ShExJ primer for a description of ShEx validation and the ShExJ specification for more details about the results format.
validate's -n and -s arguemtns are evaluated as IRIs relative to the (first) data and schema sources respectively.
The above invocation validates the node <Issue1> in http://shex.io/examples/Issue1.ttl.
This and the shape can be written as relative IRIs:
./node_modules/shex/bin/validate \
-x http://shex.io/examples/Issue.shex \
-d http://shex.io/examples/Issue1.ttl \
-s IssueShape \
-n Issue1
Parsing from the old interwebs involves a painful mix of asynchronous callbacks for getting the schema and the data and parsing the data (shorter path below):
var shexc = "http://shex.io/examples/Issue.shex";
var shape = "http://shex.io/examples/IssueShape";
var data = "http://shex.io/examples/Issue1.ttl";
var node = "http://shex.io/examples/Issue1";
var http = require("http");
var shex = require("shex");
var n3 = require('n3');
// generic async GET function.
function GET (url, then) {
http.request(url, function (resp) {
var body = "";
resp.on('data', function (chunk) { body += chunk; });
resp.on("end", function () { then(body); });
}).end();
}
var Schema = null; // will be loaded and compiled asynchronously
var Triples = null; // will be loaded and parsed asynchronously
function validateWhenEverythingsLoaded () {
if (Schema !== null && Triples !== null) {
console.log(new shex.Validator(Schema).validate(Triples, node, shape));
}
}
// loaded the schema
GET(shexc, function (b) {
// callback parses the schema and tries to validate.
Schema = shex.Parser(shexc).parse(b)
validateWhenEverythingsLoaded();
});
// load the data
GET(data, function (b) {
// callback parses the triples and tries to validate.
var db = n3.Store();
n3.Parser({baseIRI: data, format: "text/turtle"}).parse(b, function (error, triple, prefixes) {
if (error) {
throw Error("error parsing " + data + ": " + error);
} else if (triple) {
db.addQuad(triple)
} else {
Triples = db;
validateWhenEverythingsLoaded();
}
});
});
See? That's all there was too it!
OK, that's miserable. Let's use the ShExLoader to wrap all that callback misery:
const shexc = "http://shex.io/examples/IssueSchema"; // schema location
const data = "http://shex.io/examples/Issue1"; // data location
const node = "http://shex.io/examples/Issue1#Issue1"; // node in that data
const N3 = require("n3");
const ShExLoader = require("@shexjs/loader")({ // initialize with:
fetch: require('node-fetch'), // fetch implementation
rdfjs: N3, // RdfJs Turtle parser
});
const { ctor: RdfJsDb } = require('@shexjs/neighborhood-rdfjs');
const {ShExValidator} = require("@shexjs/validator");
ShExLoader.load({shexc: [shexc]}, {turtle: [data]})
.then(function (loaded) {
var db = RdfJsDb(loaded.data);
var validator = new ShExValidator(loaded.schema, db, { results: "api" });
const smap = [ // array of node/shape pairs
{node: node, // JSON-LD @id for node
shape: ShExValidator.Start} // schemas's start shape
]
var result = validator.validateShapeMap(smap); // success if no "errors"
console.log(JSON.stringify(result, null, 2));
} );
Note that the shex loader takes an array of ShExC schemas, either file paths or URLs, and an array of JSON schemas (empty in this invocation) and an array of Turtle files.
As with validation (above), you can convert by either executable or library.
ShEx can be represented in the compact syntax
PREFIX ex: <http://ex.example/#>
<IssueShape> { # An <IssueShape> has:
ex:state (ex:unassigned # state which is
ex:assigned), # unassigned or assigned.
ex:reportedBy @<UserShape> # reported by a <UserShape>.
}
or in JSON:
{ "type": "schema", "start": "http://shex.io/examples/IssueShape",
"shapes": {
"http://shex.io/examples/IssueShape": { "type": "shape",
"expression": { "type": "eachOf",
"expressions": [
{ "type": "tripleConstraint", "predicate": "http://ex.example/#state",
"valueExpr": { "type": "valueClass", "values": [
"http://ex.example/#unassigned", "http://ex.example/#assigned"
] } },
{ "type": "tripleConstraint", "predicate": "http://ex.example/#reportedBy",
"valueExpr": { "type": "valueClass", "reference": "http://shex.io/examples/UserShape" }
}
] } } } }
You can convert between them with shex-to-json:
./node_modules/shex/bin/shex-to-json http://shex.io/examples/Issue.shex
and, less elegantly, back with json-to-shex.
As with validation, the ShExLoader wrapes callbacks and simplifies parsing the libraries:
var shexc = "http://shex.io/examples/Issue.shex";
var shex = require("shex");
shex.Loader.load({shexc: [shexc]}, null).then(function (loaded) {
console.log(JSON.stringify(loaded.schema, null, " "));
});
There's no actual conversion; the JSON representation is just the stringification of the parsed schema.
Command line arguments which don't start with http:// or https:// are assumed to be file paths. We can create a local JSON version of the Issues schema:
./node_modules/shex/bin/shex-to-json http://shex.io/examples/Issue.shex > Issue.json
and use it to validate the Issue1.ttl as we did above:
./node_modules/shex/bin/validate \
-j Issue.json \
-d http://shex.io/examples/Issue1.ttl \
-s http://shex.io/examples/IssueShape \
-n http://shex.io/examples/Issue1
Of course the data file can be local as well.
Happy validating!
Materialize is used to transform from a source schema to a target schema after validation is done.
The syntax is:
materialize `-t <target schema>`|-h [-j `<JSON Vars File>`] [-r `<RDF root IRI>`]
Materialize reads the output from the validate tool from STDIN and maps it to the specified target schema.
If supplied, a JSON vars file will be referenced to fill in constant values not specified from the source. This is useful in assigning default fields to the target when there is no equivalent value in the source schema and source data.
Here is an example of a simple JSON vars file:
{
"urn:local:Demographics:constSys": "System",
}
If this vars file content is used, then any time a variable in the target file with value "urn:local:Demographics:constSys" is seen, the value "System will be substituted.
The RDF root IRI specifies the root node from which all nodes in the schema will descend.
The default root if none is specified is: tag:eric@w3.org/2016/root
Here are some examples:
materialize -h
validate -x source_schema.shex -l data.jsonld -s ProblemShape | materialize -t target_schema.shex -j vars.json
cat problem.val | materialize -t target_schema.shex -j vars.json -r http://hl7.org/fhir/shape/problem
It should validate, which involves the IMPORT of 3circRefS2-IS3 and 3circRefS3.
3circRefS2-IS3 also IMPORTs 3circRefS3 which shows that IMPORT is idempotent (has a side effect only the first time).
This repo uses lerna to manage multiple NPM packages. These packages are located in packages/*:
shape-map -- a ShapeMap parser@shexjs/parser -- parse ShExC into ShExJ@shexjs/writer -- serialize ShExK as ShExC@shexjs/term -- RDF terms uses in ShEx@shexjs/util -- some utilities for transforming schemas or validation output@shexjs/visitor -- a visitor for schemas@shexjs/validator -- validate nodes in an RDF graph against shapes in a schema@shexjs/eval-simple-1err -- eval-simple-1err@shexjs/eval-threaded-nerr -- eval-threaded-nerr@shexjs/loader -- an API for loading and using ShEx schemas@shexjs/node -- additional API functionality for a node environment@shexjs/cli -- a set of command line tools for transformaing and validating with schemas@shexjs/webapp -- the shex-simple WEBApp@shexjs/shape-path-query -- traverse ShEx schemas with a path language@shexjs/extension-test -- a small language for testing semantic actions in ShEx implementations (more)@shexjs/extension-map -- an extension for transforming data from one schema to another (more)@shexjs/extension-eval -- simple extension which evaluates Javascript semantic action code (more)Here are some commands you'll need:
lerna bootstrap -- look for all packages in packages/*npm ci -- to install root node_modules per package-lock.jsonlerna list -- in case you ever wonder what packages flashed past your eyespromise-worker) to one of our managed packages (webapp)
lerna add promise-worker --scope=@shexjs/webapplerna bootstrap --scope @shexjs/webapp --no-ci --force-local (why)npm remove promise-workerhoist:true in lerna.config). Lerna moves devDeps required in more than one package (e.g. the webpack deps in the webapp and extension-map packages) to the root so they won't appear in e.g. packages/extension-map/node_modules.lerna add -dev pseudo-worker --scope=@shexjs/webapp(cd packages/shex-webapp && npm install --save-dev pseudo-worker)FAQs
Shape Expressions meta package
The npm package shex receives a total of 36 weekly downloads. As such, shex popularity was classified as not popular.
We found that shex demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.