Skip to content

Commit

Permalink
rules: Refactor repeatCount rule to use minValueInclusive
Browse files Browse the repository at this point in the history
  • Loading branch information
Jared Parnell committed Mar 19, 2021
1 parent ed6cd2d commit 00faf5f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/classes/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ const Field = class {
return this.data.maxDecimalPlaces;
}

get minValueInclusive() {
return this.data.minValueInclusive;
}

get standard() {
return this.data.standard;
}
Expand Down
4 changes: 3 additions & 1 deletion src/rules/format/repeatcount-format-rule-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ const ValidationErrorSeverity = require('../../errors/validation-error-severity'

describe('RepeatCountIsPositiveInteger', () => {
const rule = new RepeatCountIsPositiveInteger();

const model = new Model({
type: 'Schedule',
fields: {
repeatCount: {
fieldName: 'repeatCount',
minValueInclusive: 1,
requiredType: 'https://schema.org/Integer',
},
},
}, 'latest');

it('should target Schedule model', () => {
const isTargeted = rule.isModelTargeted(model);
const isTargeted = rule.isFieldTargeted(model, 'repeatCount');
expect(isTargeted).toBe(true);
});

Expand Down
19 changes: 10 additions & 9 deletions src/rules/format/repeatcount-format-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const ValidationErrorSeverity = require('../../errors/validation-error-severity'
module.exports = class RepeatCountIsPositiveInteger extends Rule {
constructor(options) {
super(options);
this.targetModels = ['Schedule', 'PartialSchedule'];
this.targetFields = { Schedule: 'repeatCount', PartialSchedule: 'repeatCount' };
this.meta = {
name: 'RepeatCountIsPositiveInteger',
description: 'Validates that repeatCount is a positive integer',
Expand All @@ -25,23 +25,24 @@ module.exports = class RepeatCountIsPositiveInteger extends Rule {
};
}

validateModel(node) {
const repeatCount = node.getValue('repeatCount');
validateField(node, field) {
const fieldObj = node.model.getField(field);
const fieldValue = node.getValue(field);

if (typeof repeatCount === 'undefined'
) {
if (typeof fieldValue !== 'number') {
return [];
}

const errors = [];

if (repeatCount < 1
|| repeatCount % 1 !== 0) {
if (typeof fieldObj.minValueInclusive !== 'undefined'
&& (fieldValue < fieldObj.minValueInclusive || fieldValue % 1 !== 0)) {
errors.push(
this.createError(
'default',
{
repeatCount,
path: node.getPath('repeatCount'),
fieldValue,
path: node.getPath(field),
},
),
);
Expand Down

0 comments on commit 00faf5f

Please sign in to comment.