Skip to content

Commit

Permalink
fix: Allow empty RPDE page (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukehesluke authored Feb 26, 2024
1 parent b01ec74 commit 0ffa3a4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"globals": {
"expectAsync": true
},
"parserOptions": {
"ecmaVersion": 2020
},
"rules": {
"max-len": "off",
"no-await-in-loop": 0,
Expand Down
38 changes: 16 additions & 22 deletions src/helpers/raw.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
const _ = require('lodash');

const RawHelper = class {
static isRpdeFeed(data) {
if (
typeof data !== 'object'
|| data === null
|| data instanceof Array
) {
if (!_.isPlainObject(data)) {
return false;
}
const type = data['@type'] ?? data.type;
// This is a JSON-LD object with a @type
if (!_.isNil(type)) {
return false;
}
if (!Array.isArray(data.items)) {
return false;
}
if (
typeof data.type === 'undefined'
&& typeof data['@type'] === 'undefined'
&& typeof data.items !== 'undefined'
&& data.items instanceof Array
) {
for (const item of data.items) {
if (
typeof item.state === 'string'
&& (
item.state === 'updated'
|| item.state === 'deleted'
)
) {
return true;
}
for (const item of data.items) {
if (item.state !== 'updated' && item.state !== 'deleted') {
return false;
}
}
return false;
// If the page has no items (e.g. a last page), it's still considered an RPDE feed.
return true;
}
};

Expand Down
14 changes: 14 additions & 0 deletions src/rules/raw/rpde-feed-rule-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ describe('RpdeFeedRule', () => {
expect(errors[0].severity).toBe(ValidationErrorSeverity.NOTICE);
});

it('should return a notice for an empty RPDE feed', async () => {
const data = {
items: [],
next: 'https://example.org/api/feed/?afterId=ABCDEF09001015&afterTimestamp=1533206202992&limit=500',
license: 'https://creativecommons.org/licenses/by/4.0/',
};

const { errors } = await rule.validate(data);
expect(errors.length).toBe(1);

expect(errors[0].type).toBe(ValidationErrorType.FOUND_RPDE_FEED);
expect(errors[0].severity).toBe(ValidationErrorSeverity.NOTICE);
});

it('should return a notice for an RPDE feed, and modify the data with a limit to the number of items', async () => {
const feed = {
items: [
Expand Down

0 comments on commit 0ffa3a4

Please sign in to comment.