Skip to content

Commit

Permalink
Expose Validator class and validate function
Browse files Browse the repository at this point in the history
  • Loading branch information
alvinsw committed Aug 8, 2024
1 parent 2c58b62 commit 146f1c8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
const {ROCrate} = require('./lib/rocrate.js');
const {Checker} = require('./lib/checker.js');
const {Utils} = require('./lib/utils.js');
const {Validator, validate} = require('./lib/validator.js');
const Defaults = require('./lib/defaults.js');

module.exports = {
ROCrate,
Checker,
Utils,
Validator,
validate,
Defaults,
};

Expand Down
44 changes: 31 additions & 13 deletions lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,18 @@ class Validator {

if (typeof json === 'object') {
this.json = json;
return;
}
try {
this.json = JSON.parse(json);
} catch (error) {
this.results.push({
id: "validJson",
status: "error",
message: "Crate is not JSON: " + error,
clause: 'not a json'
});
return;
} else if (typeof json === 'string') {
try {
this.json = JSON.parse(json);
} catch (error) {
this.results.push({
id: "validJson",
status: "error",
message: "Crate is not JSON: " + error,
clause: 'not a json'
});
return;
}
}
var okSoFar = true;
const graph = this.json['@graph'];
Expand Down Expand Up @@ -404,10 +404,28 @@ class Validator {
}

async validate() {
if (!this.crate) return false;
await this.hasContext();
this.rootDataEntity();
return true;
}
}

module.exports = {Validator};
async function validate(crate, files) {
let validator;
// check if crate is an instance of ROCrate class
if (crate instanceof ROCrate) {
validator = new Validator(crate);
} else {
validator = new Validator();
validator.parseJSON(crate);
}

await validator.validate();
if (files) {
await validator.checkFiles(files);
}
return validator.results;
}

module.exports = { Validator, validate };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ro-crate",
"version": "3.4.0",
"version": "3.4.1",
"description": "Research Object Crate (RO-Crate) utilities for making and consuming crates",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 146f1c8

Please sign in to comment.