Skip to content

Commit

Permalink
Merge pull request #19 from Language-Research-Technology/milestone-1-…
Browse files Browse the repository at this point in the history
…validation

Merge new validator feature
  • Loading branch information
alvinsw authored Aug 8, 2024
2 parents bcea5ed + 2214b76 commit 2c58b62
Show file tree
Hide file tree
Showing 11 changed files with 1,056 additions and 225 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Install the library:

- [**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.

## Usage

Expand Down Expand Up @@ -131,7 +132,7 @@ fs.writeFileSync('ro-crate-metadata.json', JSON.stringify(crate, null, 2));
```
For more usage examples, see the test files under the [test directory](test).

For more details, refer to the full [API documentation](https://arkisto-platform.github.io/ro-crate-js/).
For more details, refer to the full [API documentation](https://language-research-technology.github.io/ro-crate-js/).


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

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

Expand All @@ -41,12 +42,14 @@ 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() {
Expand Down Expand Up @@ -147,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 @@ -178,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 @@ -191,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
Loading

0 comments on commit 2c58b62

Please sign in to comment.