Skip to content

Commit 504eca0

Browse files
authored
Ensure either "type" or "@type" is used (#74)
1 parent b7954f1 commit 504eca0

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

cypress/integration/validation.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ describe('Document validation', function() {
3939
);
4040
cy.get('#validateCodemeta').click();
4141

42-
4342
cy.get('#name').should('have.value', '');
4443
cy.get('#errorMessage').should('have.text', '');
4544
});
@@ -105,6 +104,20 @@ describe('Document validation', function() {
105104

106105
cy.get('#errorMessage').should('have.text', 'Unknown field "foobar".');
107106
});
107+
108+
it('errors when both "type" and "@type" are present', function() {
109+
cy.get('#codemetaText').then((elem) =>
110+
elem.text(JSON.stringify({
111+
"@context": "https://doi.org/10.5063/schema/codemeta-2.0",
112+
"type": "SoftwareSourceCode",
113+
"@type": "SoftwareSourceCode",
114+
"name": "Conflicting types example",
115+
}))
116+
);
117+
cy.get('#validateCodemeta').click();
118+
119+
cy.get('#errorMessage').should('have.text', 'Document must use either "type" or "@type", not both.');
120+
});
108121
});
109122

110123
describe('URLs validation', function() {

js/validation/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ function validateDocument(doc) {
2020
}
2121
// TODO: validate id/@id
2222

23-
// TODO: check there is either type or @type but not both
23+
// Ensure either "type" or "@type" is used, but not both
24+
const typeKeys = ['type', '@type'];
25+
if (typeKeys.filter(k => Object.prototype.hasOwnProperty.call(doc, k)).length > 1) {
26+
setError(`Document must use either "type" or "@type", not both.`);
27+
return false;
28+
}
29+
2430
var type = getDocumentType(doc);
2531
if (type === undefined) {
2632
setError("Missing type (must be SoftwareSourceCode or SoftwareApplication).")

0 commit comments

Comments
 (0)