Skip to content

Commit

Permalink
Merge pull request #446 from UKHomeOffice/bugfix/FAIR-413
Browse files Browse the repository at this point in the history
Validation added for zero month entries
  • Loading branch information
justinhyltonHO authored Feb 26, 2024
2 parents 8a3fd2a + 028d252 commit a910bc3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 46 deletions.
44 changes: 23 additions & 21 deletions src/common/utils/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,14 @@ function valuetrue(value) {
}

function daysInMonth(m, y) {
switch (m - 1) {
case 1:
return (y % 4 === 0 && y % 100) || y % 400 === 0 ? 29 : 28;
case 8:
case 3:
case 5:
case 10:
return 30;
default:
return 31;
}
const lastDayOfMonth = new Date(y, m, 0);
return lastDayOfMonth.getDate();
}

function isNumeric(input) {
// Essentially jQuery's implementation...
return (input - parseFloat(input) + 1) >= 0;
if (typeof input === "string") {
return (input - parseFloat(input) + 1) >= 0;
}
}

/**
Expand All @@ -118,17 +110,23 @@ function isPrintable(value) {
return !value.includes('\n');
}

// validday
function validDay(d, m, y) {
if (isNumeric(d)) {
return m >= 0 && m <= 12 && d > 0 && d <= daysInMonth(m, y);
return validMonth(m) && (d >= 1 && d <= daysInMonth(m, y));
}
return false;
}

function validMonth(m) {
if (isNumeric(m)) {
return m >= 0 && m <= 12;
return m >= 1 && m <= 12;
}
return false;
}

function validYear(y) {
if (isNumeric(y)) {
return (y.length === 4) && (y >= 1000 && y <= 9999);
}
return false;
}
Expand Down Expand Up @@ -197,7 +195,7 @@ function validGender(value) {
}

/**
* Checks that the supplied date is not a past date
* Checks that a valid supplied date is not a past date
* @param {Object} dObjh Date - can be js Date object or the {d:,m:,y} type object that is used in the UI
* @returns {Boolean} true if not past date, false if past date
*/
Expand All @@ -213,6 +211,14 @@ function currentOrPastDate(dObj) {
return true;
}

/*
Returns true if supplied dates are invalid to avoid duplicate error messages being displayed.
The dateNotTooFarInFuture function will cover this error.
*/
if(validDay(dObj.d, dObj.m, dObj.y) === false || validMonth(dObj.m) === false || validYear(dObj.y) === false){
return true;
}

if (dObj.y < currDate.getFullYear()) {
return false;
}
Expand Down Expand Up @@ -281,10 +287,6 @@ function getDateFromDynamicInput(input) {
return providedDate;
}

function validYear(y) {
return y.length === 4;
}

const numericDateElements = dObj => isNumeric(dObj.d)
&& isNumeric(dObj.m)
&& isNumeric(dObj.y);
Expand Down
34 changes: 9 additions & 25 deletions src/test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ describe('Validator', () => {
});

it('Should return true for a valid year greater than or equal to the current year', () => {
expect(validator.validYear('2025')).to.be.true;
expect(validator.validYear('2030')).to.be.true;
});

it('Should return false for a year not consisting of 4 characters', () => {
Expand All @@ -181,14 +181,16 @@ describe('Validator', () => {

it('Should return true for real dates', () => {
expect(validator.realDate(genDateObj('22', '12', '2050'))).to.be.true;
expect(validator.realDate(genDateObj('01', '02', '2025'))).to.be.true;
expect(validator.realDate(genDateObj('01', '02', '2030'))).to.be.true;
});

it('Should return false for invalid dates', () => {
expect(validator.realDate(null)).to.be.false;
expect(validator.realDate(undefined)).to.be.false;
expect(validator.realDate(genDateObj('aa', 'bb', 'cccc'))).to.be.false;
expect(validator.realDate(genDateObj('1f', 'd2', '20S5'))).to.be.false;
expect(validator.realDate(genDateObj('22', '0', '2025'))).to.be.false;
expect(validator.realDate(genDateObj('22', '2', '0000'))).to.be.false;
});

it('Should return true for a date greater than today', () => {
Expand All @@ -210,30 +212,12 @@ describe('Validator', () => {
clock.restore();
});

it('Should return true for a date after, edge cases', () => {
const clock = sinon.useFakeTimers(new Date(2011, 11, 31).getTime());

expect(validator.currentOrPastDate(genDateObj(1, 1, 2011))).to.be.false;
expect(validator.currentOrPastDate(genDateObj(31, 1, 2011))).to.be.false;
expect(validator.currentOrPastDate(genDateObj(1, 1, 2012))).to.be.true;
expect(validator.currentOrPastDate(genDateObj(31, 12, 2011))).to.be.true;

clock.restore();
});

it('Should return false for a date before today', () => {
const clock = sinon.useFakeTimers(new Date(2011, 12, 31).getTime());

const currDate = new Date();
const day = currDate.getDate() - 1;
const month = currDate.getMonth() + 1;
const previousMonth = currDate.getMonth() - 1;
const year = currDate.getFullYear();
expect(validator.currentOrPastDate(genDateObj(day, month, year))).to.be.false;
expect(validator.currentOrPastDate(genDateObj(currDate.getDate(), previousMonth, year))).to.be.false;
expect(validator.currentOrPastDate(genDateObj('22', '12', '1999'))).to.be.false;

clock.restore();
expect(validator.currentOrPastDate(genDateObj('22','12','1999'))).to.be.false;
expect(validator.currentOrPastDate(genDateObj('16','2','2019'))).to.be.false;
expect(validator.currentOrPastDate(genDateObj('16','1','2021'))).to.be.false;
expect(validator.currentOrPastDate(genDateObj('27','7','2023',))).to.be.false;
expect(validator.currentOrPastDate(genDateObj('21','2','2024'))).to.be.false;
});

it('Should return true for valid times', () => {
Expand Down

0 comments on commit a910bc3

Please sign in to comment.