Skip to content

Commit

Permalink
validate date in depth
Browse files Browse the repository at this point in the history
  • Loading branch information
ge72mum authored and thamara committed Jan 8, 2024
1 parent e331759 commit 238cf22
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
7 changes: 3 additions & 4 deletions __tests__/__main__/validate-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
});
});

Expand Down
35 changes: 33 additions & 2 deletions js/validate-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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',
Expand All @@ -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)
{
Expand Down

0 comments on commit 238cf22

Please sign in to comment.