Skip to content

Commit

Permalink
Use jsonld.js to normalize documents when importing (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjonin authored Mar 28, 2024
1 parent 83e6919 commit 858cc4e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
18 changes: 18 additions & 0 deletions cypress/integration/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,24 @@ describe('JSON Import', function() {
cy.get("#selected-licenses").children().eq(1).children().first().should('have.text', 'MIT');
});

it('works with expanded document version', function () {
cy.get('#codemetaText').then((elem) =>
elem.text(JSON.stringify({
"http://schema.org/name": [
{
"@value": "My Test Software"
}
],
"@type": [
"http://schema.org/SoftwareSourceCode"
]
}))
);
cy.get('#importCodemeta').click();

cy.get('#name').should('have.value', 'My Test Software');
});

it('errors on invalid type', function() {
cy.get('#codemetaText').then((elem) =>
elem.text(JSON.stringify({
Expand Down
8 changes: 4 additions & 4 deletions js/codemeta_generation.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ function importShortOrg(fieldName, doc) {
if (doc !== undefined) {
// Use @id if set, else use name
setIfDefined(fieldName, doc["name"]);
setIfDefined(fieldName, doc["@id"]);
setIfDefined(fieldName, getDocumentId(doc));
}
}

Expand All @@ -238,7 +238,7 @@ function importPersons(prefix, legend, docs) {
docs.forEach(function (doc, index) {
var personId = addPerson(prefix, legend);

setIfDefined(`#${prefix}_${personId}_id`, doc['@id']);
setIfDefined(`#${prefix}_${personId}_id`, getDocumentId(doc));
directPersonCodemetaFields.forEach(function (item, index) {
setIfDefined(`#${prefix}_${personId}_${item}`, doc[item]);
});
Expand All @@ -247,9 +247,9 @@ function importPersons(prefix, legend, docs) {
})
}

function importCodemeta() {
async function importCodemeta() {
var inputForm = document.querySelector('#inputForm');
var doc = parseAndValidateCodemeta(false);
var doc = await parseAndValidateCodemeta(false);
resetForm();

if (doc['license'] !== undefined) {
Expand Down
9 changes: 5 additions & 4 deletions js/validation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ function validateDocument(doc) {
}


function parseAndValidateCodemeta(showPopup) {
async function parseAndValidateCodemeta(showPopup) {
var codemetaText = document.querySelector('#codemetaText').innerText;
var doc;
let parsed, doc;

try {
doc = JSON.parse(codemetaText);
parsed = JSON.parse(codemetaText);
}
catch (e) {
setError(`Could not read codemeta document because it is not valid JSON (${e}). Check for missing or extra quote, colon, or bracket characters.`);
Expand All @@ -90,7 +90,7 @@ function parseAndValidateCodemeta(showPopup) {

setError("");

var isValid = validateDocument(doc);
var isValid = validateDocument(parsed);
if (showPopup) {
if (isValid) {
alert('Document is valid!')
Expand All @@ -100,5 +100,6 @@ function parseAndValidateCodemeta(showPopup) {
}
}

doc = await jsonld.compact(parsed, CODEMETA_CONTEXT_URL);
return doc;
}
6 changes: 5 additions & 1 deletion js/validation/things.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ function getDocumentType(doc) {
return doc["type"] || doc["@type"] || doc["codemeta:type"]
}

function getDocumentId(doc) {
return doc["id"] || doc["@id"];
}

function isCompactTypeEqual(type, compactedType) {
// FIXME: are all variants allowed?
return (type == `${compactedType}`
Expand Down Expand Up @@ -64,7 +68,7 @@ function validateThing(parentFieldName, typeFieldValidators, doc) {

var documentType = getDocumentType(doc);

var id = doc["id"] || doc["@id"];
var id = getDocumentId(doc);
if (id !== undefined && !isUrl(id)) {
setError(`"${fieldName}" has an invalid URI as id: ${JSON.stringify(id)}"`);
return false;
Expand Down

0 comments on commit 858cc4e

Please sign in to comment.