Commit 4f8bd9b7 authored by PTSEFTON's avatar PTSEFTON
Browse files

Started on ro-crate-checking and reporting

parent 5657e8dd
var jsonldHelper = require("./jsonldhelper");
const defaults = require("./defaults")
class Checker {
constructor(json) {
this.helper = new jsonldHelper();
this.helper.init(json);
this.checklist = [];
}
hasContext() {
var checkItem = new CheckItem(
{
name: "Has @context",
message: "The json-ld has an appropriate context"
}
)
if (this.helper.json_ld["@context"] && this.helper.json_ld["@context"] === defaults.context) {
checkItem.status = true;
}
return checkItem;
}
hasRootDataset() {
var checkItem = new CheckItem(
{
name: "Has root Dataset",
message: "There is a JSON-LD item with @type of Dataset (http://schema.org/dataset)"
}
)
if (this.helper.root_node) {
checkItem.status = true;
}
return checkItem;
}
hasName() {
var checkItem = new CheckItem(
{
name: "Has name",
message: "The root Dataset has a name (http://schema.org/name)"
}
)
if (this.helper.root_node && this.helper.root_node.name && this.helper.root_node.name.length > 0) {
checkItem.status = true;
}
return checkItem;
}
hasCreator() {
var checkItem = new CheckItem(
{
name: "Has valid Creators",
message: "The root Dataset has at least one Creator (http://schema.org/creator) referred to by @id, and all creators have @type Person (http://schema.org/Person) or Organization (http://schema.org/Organization)"
}
)
if (this.helper.root_node && this.helper.root_node.creator) {
const targets = this.helper.value_as_array(this.helper.root_node.creator);
for (let t of targets) {
if (t["@id"] && this.helper.item_by_id[t["@id"]]) {
var creator = this.helper.item_by_id[t["@id"]];
const types = this.helper.value_as_array(creator["@type"]);
console.log("TYPES", types)
if (types.includes("Person") || types.includes("Organization")){
checkItem.status = true;
}
else {
checkItem.status = false;
break;
}
}
else {
checkItem.status = false;
break;
}
}
//checkItem.status = allTargetsOk;
}
return checkItem;
}
hasLicense() {
var checkItem = new CheckItem(
{
name: "Has a license ",
message: "The root Dataset has a License with a URL or a local copy"
}
)
if (this.helper.root_node && this.helper.root_node.license) {
const targets = this.helper.value_as_array(this.helper.root_node.license);
for (let t of targets) {
var license = this.helper.item_by_id[t["@id"]];
const types = this.helper.value_as_array(license["@type"]);
// TODO check that license path is a path or URL is a URL and check name and description are legit
if (types.includes("CreativeWork") && license.name && licence.description && (license.path || license.URL )){
checkItem.status = true;
break;
}
}
}
return checkItem;
}
check () {
this.checklist.push(this.hasContext());
this.checklist.push(this.hasRootDataset());
this.checklist.push(this.hasName());
this.checklist.push(this.hasCreator());
this.checklist.push(this.hasLicense());
}
report() {
var report = [];
for (var item of this.checklist) {
const tick = item.status ? "✔️" : "";
report.push(`${tick} ${item.name}: ${item.message}]`);
}
return report.join("\n");
}
}
class CheckItem {
constructor(data) {
this.name = data.name;
this.message = data.message;
this.status = false;
}
}
module.exports = Checker;
/* This is part of Calcyte a tool for implementing the DataCrate data packaging
spec. Copyright (C) 2018 University of Technology Sydney
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const assert = require('assert');
const fs = require ('fs-extra');
const path = require('path');
const Checker = require('../lib/checker');
const chai = require('chai');
const expect = chai.expect;
chai.use(require('chai-fs'));
const defaults = require("../lib/defaults")
describe('Incremental checking', function () {
it('should have a @context', async function () {
//json = JSON.parse(fs.readFileSync("test_data/sample-ro-crate-metadata.jsonld"));
json = {}
var checker = new Checker(json);
assert(!checker.hasContext().status, "Does not have a @context");
// Now with context
json["@context"] = defaults.context;
var checker = new Checker(json);
assert(checker.hasContext(),"Has a @context");
// Don't have a dataset tho yet
assert(!checker.hasRootDataset().status, "Does not have a root dataset");
var dataset = {"@type": "Dataset", "path": "./"}
json["@graph"] = [dataset];
var checker = new Checker(json);
assert(checker.hasRootDataset().status, "Does have a root dataset");
// No name yet
assert(!checker.hasName().status, "Does not have a name");
dataset.name = "";
var checker = new Checker(json);
assert(!checker.hasName().status, "Does not have a name");
dataset.name = "Name!";
var checker = new Checker(json);
assert(checker.hasName().status, "Does have a name");
assert(!checker.hasCreator().status, "Does have a name");
// Creator
var creator1 = {"@id": "http://orcid.org/some-orcid", "name": "Some Person"}
dataset.creator = [{"@id": "http://orcid.org/some-orcid"}];
json["@graph"].push(creator1);
var checker = new Checker(json);
assert(!checker.hasCreator().status, "Does not have one or more creators with @type Person or Organization");
// One good creator and one dodgy one
var creator2 = {"@id": "http://orcid.org/some-other-orcid", "name": "Some Person", "@type": "Person"}
dataset.creator.push({"@id": "http://orcid.org/some-other-orcid"});
var checker = new Checker(json);
json["@graph"].push(creator1);
assert(!checker.hasCreator().status, "Does not have one or more creators with @type Person or Organization");
// One good creator
dataset.creator = [creator2];
json["@graph"] = [dataset, creator2];
var checker = new Checker(json);
assert(checker.hasCreator().status, "Does have a creator with @type Person or Organization");
// License
assert(!checker.hasLicense().status, "Does not have a license with @type CreativeWork");
var license = {"@id": "http://example.com/some_kind_of_license", "@type": "CreativeWork", "URL": "http://example.com/some_kind_of_license"}
dataset.license = {"@id": "http://example.com/some_kind_of_license"}
json["@graph"].push(license);
var checker = new Checker(json);
assert(!checker.hasLicense().status, "Does not have a license with @type CreativeWork");
checker.check();
console.log(checker.report());
});
});
after(function () {
//TODO: destroy test repoPath
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment