diff --git a/package.json b/package.json index de92b77..4481936 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,6 @@ "dependencies": { "@openactive/data-model-validator": "^2.0.0", "cache-parser": "^1.2.4", - "jsonpath": "^1.1.1", "lodash": "^4.17.21", "luxon": "^1.4.2", "node-fetch": "^2.2.0" diff --git a/src/rules/page/after-change-number-rule.js b/src/rules/page/after-change-number-rule.js index 419a226..42c3cf3 100644 --- a/src/rules/page/after-change-number-rule.js +++ b/src/rules/page/after-change-number-rule.js @@ -2,7 +2,7 @@ const { ValidationErrorCategory, ValidationErrorSeverity, } = require('@openactive/data-model-validator'); -const jp = require('jsonpath'); +const _ = require('lodash'); const RpdeRule = require('../../rpde-rule'); const RpdeErrorType = require('../../errors/rpde-error-type'); const UrlHelper = require('../../helpers/url-helper'); @@ -53,12 +53,9 @@ class AfterChangeNumberRule extends RpdeRule { const lastChangeNumber = UrlHelper.getParam('afterChangeNumber', node.url) || this.lastChangeNumber; const afterChangeNumber = UrlHelper.getParam('afterChangeNumber', node.data.next, node.url); if (afterChangeNumber !== null) { - const modified = jp.query(node.data, '$.items[0].modified'); + const modified = _.get(node.data, 'items[0].modified'); if ( - modified.length === 0 - || ( - typeof modified[0] !== 'number' - && !modified[0].match(/^[1-9][0-9]*$/) + _.isNil(modified) || (typeof modified !== 'number' && !modified.match(/^[1-9][0-9]*$/) ) ) { node.log.addPageError( diff --git a/src/rules/page/after-timestamp-rule.js b/src/rules/page/after-timestamp-rule.js index 1ab8cb8..7f61701 100644 --- a/src/rules/page/after-timestamp-rule.js +++ b/src/rules/page/after-timestamp-rule.js @@ -2,7 +2,7 @@ const { ValidationErrorCategory, ValidationErrorSeverity, } = require('@openactive/data-model-validator'); -const jp = require('jsonpath'); +const _ = require('lodash'); const RpdeRule = require('../../rpde-rule'); const RpdeErrorType = require('../../errors/rpde-error-type'); const UrlHelper = require('../../helpers/url-helper'); @@ -93,15 +93,15 @@ class AfterTimestampRule extends RpdeRule { const afterTimestamp = UrlHelper.getParam('afterTimestamp', node.data.next, node.url); const afterId = UrlHelper.getParam('afterId', node.data.next, node.url); if (afterTimestamp !== null) { - const modified = jp.query(node.data, '$.items[0].modified'); + const modified = _.get(node.data, 'items[0].modified'); if (modified.length !== 0) { if ( - typeof modified[0] === 'number' - || modified[0].match(/^[1-9][0-9]*$/) + typeof modified === 'number' + || modified.match(/^[1-9][0-9]*$/) ) { if ( - typeof modified[0] === 'string' - && modified[0].match(/^[1-9][0-9]*$/) + typeof modified === 'string' + && modified.match(/^[1-9][0-9]*$/) ) { node.log.addPageError( node.url, @@ -187,7 +187,7 @@ class AfterTimestampRule extends RpdeRule { // Do we have a last item that matches afterId and afterTimestamp? if (node.data.items.length > 0) { - const lastItem = jp.query(node.data, `$.items[${node.data.items.length - 1}]`)[0]; + const lastItem = _.get(node.data, `items[${node.data.items.length - 1}]`); const lastItemModified = lastItem.modified; const lastItemId = lastItem.id; const lastItemCompare = { diff --git a/src/rules/page/deleted-items-rule.js b/src/rules/page/deleted-items-rule.js index efae84f..2d8b7f8 100644 --- a/src/rules/page/deleted-items-rule.js +++ b/src/rules/page/deleted-items-rule.js @@ -2,7 +2,7 @@ const { ValidationErrorCategory, ValidationErrorSeverity, } = require('@openactive/data-model-validator'); -const jp = require('jsonpath'); +const _ = require('lodash'); const RpdeRule = require('../../rpde-rule'); const RpdeErrorType = require('../../errors/rpde-error-type'); @@ -30,8 +30,8 @@ const DeletedItemsRule = class extends RpdeRule { return; } if (!this.deletedItemsFound) { - const deleted = jp.query(node.data, '$..items[?(@.state=="deleted")]'); - if (deleted.length > 0) { + const hasDeleted = _.isArray(node.data.items) && node.data.items.some((item) => item.state === 'deleted'); + if (hasDeleted) { this.deletedItemsFound = true; } } diff --git a/src/rules/page/duplicate-items-rule.js b/src/rules/page/duplicate-items-rule.js index 1f39256..3be0dbd 100644 --- a/src/rules/page/duplicate-items-rule.js +++ b/src/rules/page/duplicate-items-rule.js @@ -2,7 +2,7 @@ const { ValidationErrorCategory, ValidationErrorSeverity, } = require('@openactive/data-model-validator'); -const jp = require('jsonpath'); +const _ = require('lodash'); const RpdeRule = require('../../rpde-rule'); const RpdeErrorType = require('../../errors/rpde-error-type'); @@ -34,7 +34,7 @@ const DuplicateItemsRule = class extends RpdeRule { if (typeof node.data !== 'object' || node.isItemDuplicationPermissible) { return; } - const ids = jp.query(node.data, '$.items[*].id'); + const ids = (_.isArray(node.data.items) && node.data.items.map((item) => item.id)) || []; for (const id of ids) { if (this.itemMap.indexOf(id) >= 0) { node.log.addPageError(