From 146f1c892cc880a83bcd1c91394b78d590af18c8 Mon Sep 17 00:00:00 2001 From: Alvin Sebastian Date: Thu, 8 Aug 2024 00:49:05 +0000 Subject: [PATCH] Expose Validator class and validate function --- index.js | 3 +++ lib/validator.js | 44 +++++++++++++++++++++++++++++++------------- package.json | 2 +- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 73f9ab2..fae32ae 100644 --- a/index.js +++ b/index.js @@ -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, }; diff --git a/lib/validator.js b/lib/validator.js index 24d8791..798d3e0 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -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']; @@ -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 }; diff --git a/package.json b/package.json index aa0ba61..3682557 100644 --- a/package.json +++ b/package.json @@ -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": {