Skip to content

Commit

Permalink
[open-formulieren/open-forms#3611] Make time bounds validation inclusive
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Nov 21, 2023
1 parent 7aee061 commit e8fa867
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/formio/validators/MinMaxTimeValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ const validateTimeBoundaries = (minBoundary, maxBoundary, timeValue) => {
// Case 1: only one boundary is given
if (!minTime || !maxTime) {
if (minTime) return {isValid: parsedValue >= minTime, errorKeys: ['minTime']};
if (maxTime) return {isValid: parsedValue < maxTime, errorKeys: ['maxTime']};
if (maxTime) return {isValid: parsedValue <= maxTime, errorKeys: ['maxTime']};
} else {
// Case 2: min boundary is smaller than max boundary
if (minTime < maxTime) {
const isTooEarly = parsedValue < minTime;
const isTooLate = parsedValue >= maxTime;
const isTooLate = parsedValue > maxTime;
return {
isValid: !isTooEarly && !isTooLate,
errorKeys: [isTooEarly ? 'minTime' : 'maxTime', 'invalid_time'],
};
} else {
// Case 3: min boundary is bigger than max boundary (it's the next day. For example min = 08:00, max = 01:00)
return {
isValid: !(parsedValue >= maxTime && parsedValue < minTime),
isValid: !(parsedValue > maxTime && parsedValue < minTime), // Basically, the opposite of 01:00 < `parsedValue` < 08:00
errorKeys: ['invalid_time'],
};
}
Expand Down
8 changes: 4 additions & 4 deletions src/jstests/formio/components/time.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ describe('Time Component', () => {
formJSON.components[0].validate.minTime = '09:00:00';
formJSON.components[0].validate.maxTime = '17:00:00';

const validValues = ['09:00:00', '10:30:00', '11:11:11'];
const validValues = ['09:00:00', '10:30:00', '11:11:11', '17:00:00'];

const invalidValues = ['17:00:00', '17:30:00', '08:30:00'];
const invalidValues = ['17:30:00', '08:30:00'];

const testValidity = (values, valid) => {
values.forEach(value => {
Expand Down Expand Up @@ -125,7 +125,7 @@ describe('Time Component', () => {
let formJSON = _.cloneDeep(timeForm);
formJSON.components[0].validate.maxTime = '09:00:00';

const validValues = ['08:00:00'];
const validValues = ['08:00:00', '09:00:00'];

const invalidValues = ['17:00:00'];

Expand Down Expand Up @@ -165,7 +165,7 @@ describe('Time Component', () => {
formJSON.components[0].validate.maxTime = '01:00:00';
formJSON.components[0].validate.minTime = '08:00:00';

const validValues = ['09:00:00', '00:30:00'];
const validValues = ['09:00:00', '00:30:00', '01:00:00', '08:00:00'];

const invalidValues = ['02:00:00'];

Expand Down

0 comments on commit e8fa867

Please sign in to comment.