Skip to content

Commit 442d9b2

Browse files
authored
fix: Allow relative URLs (#26)
1 parent 7a21f63 commit 442d9b2

5 files changed

Lines changed: 56 additions & 2 deletions

File tree

gallery/ImageObject/dataUrl.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"@context": "https://schema.org/",
3+
"@type": "ImageObject",
4+
"url": "data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7",
5+
"acquireLicensePage": "https://example.com/how-to-use-my-images",
6+
"creator": {
7+
"@type": "Person",
8+
"name": "Brixton Brownstone"
9+
},
10+
"copyrightNotice": "Clara Kent"
11+
}

gallery/ImageObject/valid3.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"@context": "https://schema.org/",
3+
"@type": "ImageObject",
4+
"url": "/photos/1x1/black-labrador-puppy.jpg",
5+
"acquireLicensePage": "https://example.com/how-to-use-my-images",
6+
"creator": {
7+
"@type": "Person",
8+
"name": "Brixton Brownstone"
9+
},
10+
"copyrightNotice": "Clara Kent"
11+
}

src/types/ImageObject.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default class ImageObjectValidator extends BaseValidator {
2020
// Only require these additional fields for root image objects
2121
if (this.path.length === 1) {
2222
conditions.push(
23+
// Be aware, this rule is not shown as an error in the validator. Instead the entity is completely ignored if not at least one of these fields is present.
2324
this.or(
2425
this.required('creator'),
2526
this.required('creditText'),

src/types/__tests__/ImageObject.test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,40 @@ describe('ImageObjectValidator', () => {
3232
it('should validate a correct image object structure in valid2.json', async () => {
3333
const data = await loadTestData('ImageObject/valid2.json', 'jsonld');
3434
const issues = await validator.validate(data);
35-
expect(issues).to.have.lengthOf(2);
35+
const errors = issues.filter((issue) => issue.severity === 'ERROR');
36+
expect(errors).to.have.lengthOf(0);
3637
});
3738

3839
it('should ignore additional fields on nested image objects', async () => {
3940
const data = await loadTestData('ImageObject/nested.json', 'jsonld');
4041
const issues = await validator.validate(data);
4142
expect(issues).to.have.lengthOf(0);
4243
});
44+
45+
it('should allow relative URLs', async () => {
46+
const data = await loadTestData('ImageObject/valid3.json', 'jsonld');
47+
const issues = await validator.validate(data);
48+
const errors = issues.filter((issue) => issue.severity === 'ERROR');
49+
expect(errors).to.have.lengthOf(0);
50+
});
51+
52+
it('should not allow data: URLs', async () => {
53+
const data = await loadTestData('ImageObject/dataUrl.json', 'jsonld');
54+
const issues = await validator.validate(data);
55+
const errors = issues.filter((issue) => issue.severity === 'ERROR');
56+
expect(errors).to.have.lengthOf(1);
57+
expect(errors[0]).to.deep.include({
58+
rootType: 'ImageObject',
59+
issueMessage:
60+
'One of the following conditions needs to be met: Required attribute "contentUrl" is missing or Invalid type for attribute "url"',
61+
severity: 'ERROR',
62+
path: [
63+
{
64+
type: 'ImageObject',
65+
index: 0,
66+
},
67+
],
68+
});
69+
});
4370
});
4471
});

src/types/base.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,12 @@ export default class BaseValidator {
140140
const date = new Date(data);
141141
return !isNaN(date.getTime());
142142
} else if (type === 'url') {
143+
// Absolute or relative URL, but no data: URLs
144+
if (data.startsWith('data:')) {
145+
return false;
146+
}
143147
try {
144-
new URL(data);
148+
new URL(data, 'https://example.com');
145149
} catch (e) {
146150
return false;
147151
}

0 commit comments

Comments
 (0)