Skip to content

Commit

Permalink
Make isUrl stricter
Browse files Browse the repository at this point in the history
  • Loading branch information
progval committed Oct 25, 2024
1 parent c9f6aba commit ba617e4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cypress/integration/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,19 @@ describe('URLs validation', function() {
cy.get('#errorMessage').should('have.text', 'Invalid URL in field "codeRepository": "foo"');
});

it('errors on invalid URL that Javascript considers to be valid', function() {
cy.get('#codemetaText').then((elem) =>
elem.text(JSON.stringify({
"@context": "https://doi.org/10.5063/schema/codemeta-2.0",
"@type": "SoftwareSourceCode",
"codeRepository": "foo: bar",
}))
);
cy.get('#validateCodemeta').click();

cy.get('#errorMessage').should('have.text', 'Invalid URL in field "codeRepository": "foo: bar"');
});

it('errors on non-string instead of URL', function() {
cy.get('#codemetaText').then((elem) =>
elem.text(JSON.stringify({
Expand Down
4 changes: 4 additions & 0 deletions js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ function trimSpaces(s) {
function isUrl(s) {
try {
const url = new URL(s);
if (url.origin == "null") {
// forbids "foo: bar" as a URL, for example
return false;
}
return true;
} catch (e) {
return false;
Expand Down

0 comments on commit ba617e4

Please sign in to comment.