diff --git a/src/classes/field.js b/src/classes/field.js index af3f6eb..f748518 100644 --- a/src/classes/field.js +++ b/src/classes/field.js @@ -55,6 +55,10 @@ const Field = class { return this.data.maxDecimalPlaces; } + get minValueInclusive() { + return this.data.minValueInclusive; + } + get standard() { return this.data.standard; } diff --git a/src/rules/format/repeatcount-format-rule-spec.js b/src/rules/format/repeatcount-format-rule-spec.js index 1d5afb1..718c862 100644 --- a/src/rules/format/repeatcount-format-rule-spec.js +++ b/src/rules/format/repeatcount-format-rule-spec.js @@ -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); }); diff --git a/src/rules/format/repeatcount-format-rule.js b/src/rules/format/repeatcount-format-rule.js index b32f575..4574e8f 100644 --- a/src/rules/format/repeatcount-format-rule.js +++ b/src/rules/format/repeatcount-format-rule.js @@ -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', @@ -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), }, ), );