diff --git a/__tests__/__main__/validate-json.js b/__tests__/__main__/validate-json.js index 9e0356a4..d7351dcb 100644 --- a/__tests__/__main__/validate-json.js +++ b/__tests__/__main__/validate-json.js @@ -32,10 +32,9 @@ describe('Validate json', function() const goodFlexibleEntry = [{ 'type': 'flexible', 'date': '2020-06-03', 'values': ['08:00', '12:00', '13:00', '14:00'] }]; const goodWaivedEntry = [{ 'type': 'waived', 'date': '2020-06-03', 'data': 'waived', 'hours': '08:00' }]; const invalidDateFormat = [{ 'type': 'flexible', 'date': '03-06-2020', 'values': ['08:00', '12:00', '13:00', '14:00'] }]; - const invalidDateType = [{ 'type': 'flexible', 'date': ['2020-06-03'], 'values': ['08:00', '12:00', '13:00', '14:00'] }]; + const invalidDateType = [{ 'type': 'flexible', 'date': ['2020-06-13'], 'values': ['08:00', '12:00', '13:00', '14:00'] }]; const invalidDateValue = [{ 'type': 'flexible', 'date': '2020-26-03', 'values': ['08:00', '12:00', '13:00', '14:00'] }]; - // TODO - //const invalidDayInMonth = [{ 'type': 'flexible', 'date': '2020-02-31', 'values': ['08:00', '12:00', '13:00', '14:00'] }]; + const invalidDayInMonth = [{ 'type': 'flexible', 'date': '2020-04-31', 'values': ['08:00', '12:00', '13:00', '14:00'] }]; test('should be valid JSON', () => { expect(validateJSON(goodWaivedEntry)).toBeTruthy(); @@ -46,7 +45,7 @@ describe('Validate json', function() expect(validateJSON(invalidDateFormat)).not.toBeTruthy(); expect(validateJSON(invalidDateType)).not.toBeTruthy(); expect(validateJSON(invalidDateValue)).not.toBeTruthy(); - //expect(validateJSON(invalidDayInMonth)).not.toBeTruthy(); + expect(validateJSON(invalidDayInMonth)).not.toBeTruthy(); }); }); diff --git a/js/validate-json.js b/js/validate-json.js index 018afd57..65ba8469 100644 --- a/js/validate-json.js +++ b/js/validate-json.js @@ -22,7 +22,8 @@ const schemaWaivedEntry = { , 'date': { 'type': 'string', - 'pattern': /(1|2)[0-9]{3}-(0[1-9]{1}|1[0-1]{1})-(0[0-9]{1}|1[0-9]{1}|2[0-9]{1}|3[0-1]{1})$/ + 'pattern': /(1|2)[0-9]{3}-(0[1-9]{1}|1[0-1]{1})-(0[0-9]{1}|1[0-9]{1}|2[0-9]{1}|3[0-1]{1})$/, + 'format': 'dateFormat' }, 'data': { 'type': 'string' @@ -49,7 +50,8 @@ const schemaFlexibleEntry = { , 'date': { 'type': 'string', - 'pattern': /(1|2)[0-9]{3}-(0[1-9]{1}|1[0-1]{1})-(0[0-9]{1}|1[0-9]{1}|2[0-9]{1}|3[0-1]{1})$/ + 'pattern': /(1|2)[0-9]{3}-(0[1-9]{1}|1[0-1]{1})-(0[0-9]{1}|1[0-9]{1}|2[0-9]{1}|3[0-1]{1})$/, + 'format': 'dateFormat' }, 'values': { 'type': 'array', @@ -66,6 +68,35 @@ const schemaFlexibleEntry = { ] }; +function daysInMonth(m, y) +{ + switch (m) + { + 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; + } +} + +function isValid(y, m, d) +{ + return m >= 0 && m < 12 && d > 0 && d <= daysInMonth(m, y); +} + +Validator.prototype.customFormats.dateFormat = function(dateStr) +{ + if (!typeof(dateStr) === 'String' || !dateStr.includes('-')) + { + return false; + } + const dateArray = dateStr.split('-'); + const date = new Date(dateArray[0],dateArray[1], dateArray[2]); + const validDate = (date instanceof(Date) && isFinite(date.getTime()) && isValid(dateArray[0],dateArray[1]-1, dateArray[2])); + return validDate; +}; function validateJSON(instance) {