-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
repeatFrequency must be accompanied by appropriate byDay/byMonth/byMonthDay values #362
Open
thill-odi
wants to merge
2
commits into
master
Choose a base branch
from
consistent-schedule-reps
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
src/rules/data-quality/consistent-schedule-repetition-frequency-rule-spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
const ConsistentScheduleRepetitionFrequencyRule = require('./consistent-schedule-repetition-frequency-rule'); | ||
const Model = require('../../classes/model'); | ||
const ModelNode = require('../../classes/model-node'); | ||
const ValidationErrorType = require('../../errors/validation-error-type'); | ||
const ValidationErrorSeverity = require('../../errors/validation-error-severity'); | ||
|
||
|
||
/* | ||
Ensures that the value given in repeatFrequency is matched by an appropriate byDay, byWeek, or byMonth attribute. Possible values for repeatFrequency are in the first instance P[\d]D, P[\d]W, P[\d]M. In the first instance the repeatFrequency should be paired with a byDay attribute, in the second, byWeek, etc. Note that this test checks for the bare presence of appropriate attributes; there is no semantic checking | ||
*/ | ||
|
||
describe('ConsistentScheduleRepetitionFrequencyRule', () => { | ||
const rule = new ConsistentScheduleRepetitionFrequencyRule(); | ||
|
||
const model = new Model({ | ||
type: 'Schedule', | ||
fields: { | ||
repeatFrequency: { | ||
fieldName: 'repeatFrequency', | ||
requiredType: 'https://schema.org/Text', | ||
}, | ||
byDay: { | ||
fieldName: 'byDay', | ||
requiredType: 'ArrayOf#https://schema.org/DayOfWeek', | ||
}, | ||
byMonth: { | ||
fieldName: 'byMonth', | ||
requiredType: 'ArrayOf#https://schema.org/Integer', | ||
}, | ||
byMonthDay: { | ||
fieldName: 'byMonthDay', | ||
requiredType: 'ArrayOf#https://schema.org/Integer', | ||
}, | ||
}, | ||
}, 'latest'); | ||
|
||
it('should target Schedule models', () => { | ||
const isTargeted = rule.isModelTargeted(model); | ||
expect(isTargeted).toBe(true); | ||
}); | ||
|
||
it('should return an error when a repeatFrequency is malformed', async () => { | ||
const data = { | ||
type: 'Schedule', | ||
repeatFrequency: 'P1Day', | ||
}; | ||
const nodeToTest = new ModelNode( | ||
'$', | ||
data, | ||
null, | ||
model, | ||
); | ||
|
||
const errors = await rule.validate(nodeToTest); | ||
expect(errors.length).toBe(1); | ||
}); | ||
it('should return no error when a repeatFrequency is daily and no further specification is given', async () => { | ||
const data = { | ||
type: 'Schedule', | ||
repeatFrequency: 'P1D', | ||
}; | ||
const nodeToTest = new ModelNode( | ||
'$', | ||
data, | ||
null, | ||
model, | ||
); | ||
|
||
const errors = await rule.validate(nodeToTest); | ||
expect(errors.length).toBe(0); | ||
}); | ||
it('should return no error when a weekly repeatFrequency has a byDay attribute', async () => { | ||
const data = { | ||
type: 'Schedule', | ||
repeatFrequency: 'P1W', | ||
startDate: '2020-11-03T13:00:00', | ||
byDay: ['Monday', 'Thursday'], | ||
}; | ||
|
||
const nodeToTest = new ModelNode( | ||
'$', | ||
data, | ||
null, | ||
model, | ||
); | ||
const errors = await rule.validate(nodeToTest); | ||
expect(errors.length).toBe(0); | ||
}); | ||
it('should return no error when a monthly repeatFrequency has a byMonthDay attribute', async () => { | ||
const data = { | ||
type: 'Schedule', | ||
repeatFrequency: 'P1M', | ||
byMonthDay: [1, 14], | ||
}; | ||
|
||
const nodeToTest = new ModelNode( | ||
'$', | ||
data, | ||
null, | ||
model, | ||
); | ||
const errors = await rule.validate(nodeToTest); | ||
expect(errors.length).toBe(0); | ||
}); | ||
it('should return an error when a daily repeatFrequency has a byMonth attribute', async () => { | ||
const data = { | ||
type: 'Schedule', | ||
repeatFrequency: 'P1D', | ||
byMonth: [1, 3, 5, 7, 9, 11], | ||
}; | ||
|
||
const nodeToTest = new ModelNode( | ||
'$', | ||
data, | ||
null, | ||
model, | ||
); | ||
const errors = await rule.validate(nodeToTest); | ||
expect(errors.length).toBe(1); | ||
}); | ||
|
||
it('should return an error when a weekly repeatFrequency has a byMonthDay attribute', async () => { | ||
const data = { | ||
type: 'Schedule', | ||
repeatFrequency: 'P1W', | ||
startDate: '2020-11-03T13:00:00', | ||
byMonthDay: [1, 3], | ||
}; | ||
|
||
const nodeToTest = new ModelNode( | ||
'$', | ||
data, | ||
null, | ||
model, | ||
); | ||
const errors = await rule.validate(nodeToTest); | ||
expect(errors.length).toBe(1); | ||
}); | ||
it('should return an error when more than one repetition frequency period attribute is specified', async () => { | ||
const data = { | ||
type: 'Schedule', | ||
repeatFrequency: 'P1W', | ||
byMonth: [1, 3, 5, 7, 9, 11], | ||
byDay: ['Monday', 'Thursday'], | ||
}; | ||
|
||
const nodeToTest = new ModelNode( | ||
'$', | ||
data, | ||
null, | ||
model, | ||
); | ||
const errors = await rule.validate(nodeToTest); | ||
expect(errors.length).toBe(1); | ||
expect(errors[0].type).toBe(ValidationErrorType.REPEATFREQUENCY_MISALIGNED); | ||
expect(errors[0].severity).toBe(ValidationErrorSeverity.FAILURE); | ||
}); | ||
}); |
105 changes: 105 additions & 0 deletions
105
src/rules/data-quality/consistent-schedule-repetition-frequency-rule.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
const Rule = require('../rule'); | ||
const ValidationErrorType = require('../../errors/validation-error-type'); | ||
const ValidationErrorCategory = require('../../errors/validation-error-category'); | ||
const ValidationErrorSeverity = require('../../errors/validation-error-severity'); | ||
|
||
module.exports = class ConsistentScheduleRepetitionFrequencyRule extends Rule { | ||
constructor(options) { | ||
super(options); | ||
this.targetModels = ['Event', 'CourseInstance', 'EventSeries', 'HeadlineEvent', 'ScheduledSession', 'SessionSeries', 'Schedule', 'Slot']; | ||
this.meta = { | ||
name: 'ConsistentScheduleRepetitionFrequencyRule', | ||
description: 'Ensures that the repeatFrequency of a Schedule is aligned with the correct frequency specifier: e.g., weekly repetition with a day of the week, monthly repetition with a week specified.', | ||
tests: { | ||
default: { | ||
message: 'repeatFrequency must align with byDay/byWeek/byMonthWeek values of Schedule.', | ||
category: ValidationErrorCategory.DATA_QUALITY, | ||
severity: ValidationErrorSeverity.FAILURE, | ||
type: ValidationErrorType.REPEATFREQUENCY_MISALIGNED, | ||
}, | ||
norepfreq: { | ||
message: 'Schedules must contain a repeatFrequency', | ||
category: ValidationErrorCategory.DATA_QUALITY, | ||
severity: ValidationErrorSeverity.FAILURE, | ||
type: ValidationErrorType.REPEATFREQUENCY_MISALIGNED, | ||
}, | ||
badrepfreq: { | ||
message: 'repeatFrequency must conform to ISO 8601 duration values (e.g. "P1W", "P4M", etc.).', | ||
category: ValidationErrorCategory.DATA_QUALITY, | ||
severity: ValidationErrorSeverity.FAILURE, | ||
type: ValidationErrorType.REPEATFREQUENCY_MISALIGNED, | ||
}, | ||
dayerr: { | ||
message: 'Daily repeat frequencies should not have any additional "byDay", "byMonth", or "byMonthDay" attributes.', | ||
category: ValidationErrorCategory.DATA_QUALITY, | ||
severity: ValidationErrorSeverity.FAILURE, | ||
type: ValidationErrorType.REPEATFREQUENCY_MISALIGNED, | ||
}, | ||
weekerr: { | ||
message: 'Weekly repeat frequencies need a "byDay" attribute, and no others.', | ||
category: ValidationErrorCategory.DATA_QUALITY, | ||
severity: ValidationErrorSeverity.FAILURE, | ||
type: ValidationErrorType.REPEATFREQUENCY_MISALIGNED, | ||
}, | ||
montherr: { | ||
message: 'Monthly repeat frequencies need a "byMonthDay" attribute, and no others.', | ||
category: ValidationErrorCategory.DATA_QUALITY, | ||
severity: ValidationErrorSeverity.FAILURE, | ||
type: ValidationErrorType.REPEATFREQUENCY_MISALIGNED, | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
validateModel(node) { | ||
let repeatFrequency = node.getValue('repeatFrequency'); | ||
const byDay = node.getValue('byDay'); | ||
const byMonth = node.getValue('byMonth'); | ||
const byMonthDay = node.getValue('byMonthDay'); | ||
const errors = []; | ||
|
||
if (typeof repeatFrequency === 'undefined') { | ||
errors.push(this.createError('norepfreq', {}, { model: node.model.type })); | ||
return errors; | ||
} | ||
// check frequency is valud | ||
repeatFrequency = repeatFrequency.toLowerCase(); | ||
const regexp = /^p\d(d|w|m)$/; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this allow annual repeats? |
||
if (!regexp.test(repeatFrequency)) { | ||
errors.push(this.createError('badrepfreq', {}, { model: node.model.type })); | ||
} | ||
|
||
// if frequency is valid, simple parsing will work | ||
const period = repeatFrequency.slice(-1); | ||
|
||
switch (period) { | ||
case 'd': | ||
if (byDay !== undefined || byMonth !== undefined || byMonthDay !== undefined) { | ||
errors.push(this.createError('dayerr', {}, { model: node.model.type })); | ||
} | ||
break; | ||
|
||
case 'w': | ||
if (byDay === undefined) { | ||
errors.push(this.createError('weekerr', {}, { model: node.model.type })); | ||
} else if (byMonth !== undefined) { | ||
errors.push(this.createError('weekerr', {}, { model: node.model.type })); | ||
} else if (byMonthDay !== undefined) { | ||
errors.push(this.createError('weekerr', {}, { model: node.model.type })); | ||
} | ||
break; | ||
case 'm': | ||
if (byDay !== undefined) { | ||
errors.push(this.createError('montherr', {}, { model: node.model.type })); | ||
} else if (byMonth !== undefined) { | ||
errors.push(this.createError('montherr', {}, { model: node.model.type })); | ||
} else if (byMonthDay === undefined) { | ||
errors.push(this.createError('montherr', {}, { model: node.model.type })); | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
return errors; | ||
} | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
P[\d]Y
also possible?