shexjs / shex.js

shex.js javascript package
MIT License
58 stars 17 forks source link

Can't use local object to be validated #60

Open matthieu-fesselier opened 5 years ago

matthieu-fesselier commented 5 years ago

I didn't manage to use a local object to be validated. This works:

ShEx.Loader.load([validationShape], [], [], [dataURL]).then(function (loaded) {
  var db = ShEx.Util.makeN3DB(loaded.data);
  var validator = ShEx.Validator.construct(loaded.schema, { results: "api" });
  var result = validator.validate(db, [{node: dataID, shape: shapeURL}]);
  return result;
}).catch(e => console.error(e));

But this throw an error:

ShEx.Loader.load([validationShape], [], [], [dataObject]).then(function (loaded) {
  var db = ShEx.Util.makeN3DB(loaded.data);
  var validator = ShEx.Validator.construct(loaded.schema, { results: "api" });
  var result = validator.validate(db, [{node: dataID, shape: shapeURL}]);
  return result;
}).catch(e => console.error(e));

Throw TypeError: Cannot use 'in' operator to search for 'productions' in undefined

Here is dataObject:

{
  title: "Title test",
  text: "Text test", 
  @id: "http://127.0.0.1:8000/conversations/7/"
}

Am I doing something wrong?

ericprud commented 5 years ago

ShEx.Loader.load is a convenience function to load schema and data remotely. If you've already got the data loaded, you should be able to just

let theGraph = someTriplifier(dataObject); # turn the dataObject into triples
ShEx.Loader.load([validationShape], [], [], []).then(function (loaded) {
  var db = ShEx.Util.makeN3DB(theGraph);
  var validator = ShEx.Validator.construct(loaded.schema, { results: "api" });
  var result = validator.validate(db, [{node: dataID, shape: shapeURL}]);
  return result;
}).catch(e => console.error(e));

If course the magic is in that someTriplifier() function I invented. If dataObject is JSON-LD, you should be able to create an @context and use toRDF() from the jsonld npm package.

matthieu-fesselier commented 4 years ago

Thanks for the informations and sorry for the late reply. The jsonld.toRDF() returns a string, but in the makeN3DB()function, you are using a N3Store, is that right? How can I create this store? When I try to use N3, I have this error during the validation:

TypeError: db.getTriplesByIRI is not a function

Is it a N3 version problem?

ericprud commented 4 years ago

I obviously dropped the ball on this issue, but A. yeah, it looked like a version issue and B. shex.js now uses a 1.0.x N3.js