diff --git a/cypress/integration/validation.js b/cypress/integration/validation.js index 3eddfef..e796fcb 100644 --- a/cypress/integration/validation.js +++ b/cypress/integration/validation.js @@ -39,7 +39,6 @@ describe('Document validation', function() { ); cy.get('#validateCodemeta').click(); - cy.get('#name').should('have.value', ''); cy.get('#errorMessage').should('have.text', ''); }); @@ -105,6 +104,20 @@ describe('Document validation', function() { cy.get('#errorMessage').should('have.text', 'Unknown field "foobar".'); }); + + it('errors when both "type" and "@type" are present', function() { + cy.get('#codemetaText').then((elem) => + elem.text(JSON.stringify({ + "@context": "https://doi.org/10.5063/schema/codemeta-2.0", + "type": "SoftwareSourceCode", + "@type": "SoftwareSourceCode", + "name": "Conflicting types example", + })) + ); + cy.get('#validateCodemeta').click(); + + cy.get('#errorMessage').should('have.text', 'Document must use either "type" or "@type", not both.'); + }); }); describe('URLs validation', function() { diff --git a/js/validation/index.js b/js/validation/index.js index 4d396a3..d9a284a 100644 --- a/js/validation/index.js +++ b/js/validation/index.js @@ -20,7 +20,13 @@ function validateDocument(doc) { } // TODO: validate id/@id - // TODO: check there is either type or @type but not both + // Ensure either "type" or "@type" is used, but not both + const typeKeys = ['type', '@type']; + if (typeKeys.filter(k => Object.prototype.hasOwnProperty.call(doc, k)).length > 1) { + setError(`Document must use either "type" or "@type", not both.`); + return false; + } + var type = getDocumentType(doc); if (type === undefined) { setError("Missing type (must be SoftwareSourceCode or SoftwareApplication).")