Skip to content

Commit

Permalink
using validator in checker, updated validator
Browse files Browse the repository at this point in the history
  • Loading branch information
moisbo committed Aug 6, 2024
2 parents e567d0f + bcea5ed commit 7689cf4
Show file tree
Hide file tree
Showing 14 changed files with 1,007 additions and 1,849 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
with:
node-version: 18
registry-url: 'https://registry.npmjs.org'
- uses: ButlerLogic/[email protected].1
- uses: ButlerLogic/[email protected].2
with:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
tag_prefix: "v"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16, 18]
node-version: [16, 20, 21]

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Install the library:

## Docs & Other Resources

- [**API documentation**](https://arkisto-platform.github.io/ro-crate-js/)
- [**API documentation**](https://language-research-technology.github.io/ro-crate-js/)
- [**ROCrate documentation and specification**](https://www.researchobject.org/ro-crate/)
- [**Validate Crates**](validation) NEW!!! -- there is a new (2023-09) Validator class in development, we have a rudimentary command line interface to this -- documentation for how to include this as a library will follow soon.

Expand Down
41 changes: 29 additions & 12 deletions lib/checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

const defaults = require('./defaults');
const {Utils} = require('./utils');
require('cross-fetch/polyfill');
const {Validator} = require("./validator");
const _g = typeof window === 'object' ? window : (typeof global === 'object' ? global : {});
const fetch = _g.fetch || require('cross-fetch');

function includesTextCI(arr, texts) {
// string entries in which one of it must exist in the array, eg ['a','b']
Expand All @@ -40,15 +42,23 @@ function includesTextCI(arr, texts) {
class Checker {

/**
*
* @param {ROCrate} crate
*
* @param {ROCrate} crate
*/
constructor(crate) {
this.crate = crate;
this.checklist = [];
this.validator = new Validator(crate);

}

// async validateContext() {
// var checkItem = new CheckItem({
// name: 'Has valid @context entries',
// message: 'Has context entries that resolve to valid json'
// });

// }
async hasContext() {
var checkItem = new CheckItem({
name: 'Has @context',
Expand All @@ -58,6 +68,7 @@ class Checker {
for (let contextUrl of Utils.asArray(this.crate['@context'])) {
if (typeof contextUrl === 'string' || contextUrl instanceof String) {
try {
// @ts-ignore
const response = await fetch(/**@type {string}*/(contextUrl), {
headers: {
'accept': 'application/ld+json, application/ld+json, text/text'
Expand All @@ -68,13 +79,13 @@ class Checker {
if (Utils.asArray(content.name).includes('RO-Crate JSON-LD Context')) {
checkItem.message = `Has a context named "RO-Crate JSON-LD Context", version ${content.version}`;
checkItem.status = true;
break;
//break;
}
} else {
throw new Error(response.statusText);
}
} catch (error) {
console.error(error);
//console.error(error);
checkItem.message = error + ' for ' + contextUrl;
checkItem.status = false;
break;
Expand Down Expand Up @@ -139,7 +150,7 @@ class Checker {
message: 'The root Dataset has a License' +
licenses.map(license => license && license.name && license.description &&
includesTextCI(license['@type'], 'CreativeWork') ?
' (the license is a Creative Work with a name and description as it SHOULD be)' : ''
' (the license is a Creative Work with a name and description as it SHOULD be)' : ''
).join(''),
status: (licenses.length > 0)
});
Expand Down Expand Up @@ -170,7 +181,7 @@ class Checker {
}

async check() {
var checkNames = methods.filter(n => !(n in { hasContext: 0, hasAuthor: 0, hasContactPoint: 0 }));
var checkNames = methods.filter(n => !(n in {hasContext: 0, hasAuthor: 0, hasContactPoint: 0}));
var context = await this.hasContext();
this.checklist = [context].concat(checkNames.map(n => this[n]()));
this.isROCrate = this.checklist.every(c => c.status);
Expand All @@ -183,20 +194,26 @@ class Checker {
}

summarize() {
return this.isROCrate ? 'This is a valid RO-Crate' : 'This is not a valid RO-Crate';
console.log(this.validator.result)
return this.validator.result.errors.length ? 'This is not a valid RO-Crate' : 'This is a valid RO-Crate';
}

report() {
var report = [];
for (var item of this.checklist) {
const tick = item.status ? '✔️' : '❌';
report.push(`${tick} ${item.name}: ${item.message}`);
let statusEmoji = {
"warning": "⚠️",
"error": "❌",
"success": "✔️"
}
for (let item of this.validator.results) {
report.push(`${statusEmoji[item.status]} : ${item.message}`);
}
return report.join('\n');
}

async validate() {
await this.check();
await this.validator.validate();

const summary = this.summarize();
const report = this.report();
return `${summary}\n${report}`;
Expand Down
2 changes: 1 addition & 1 deletion lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -2665,7 +2665,7 @@ const standardContexts = {
"hasFile": "http://pcdm.org/models#hasFile",
"hasMember": "http://pcdm.org/models#hasMember",
"RepositoryCollection": "http://pcdm.org/models#Collection",
"RepositoryObject": "http://pcdm.org/models#object",
"RepositoryObject": "http://pcdm.org/models#Object",
"ComputationalWorkflow": "https://bioschemas.org/ComputationalWorkflow",
"input": "https://bioschemas.org/ComputationalWorkflow#input",
"output": "https://bioschemas.org/ComputationalWorkflow#output",
Expand Down
Loading

0 comments on commit 7689cf4

Please sign in to comment.