@ukhomeoffice/cop-react-form-renderer 5.49.1 → 5.49.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -31,7 +31,7 @@ var maxMonthDays = exports.maxMonthDays = function maxMonthDays(month, year) {
31
31
  };
32
32
  /**
33
33
  * Checks if a date passed is a valid date.
34
- * This will validate for 'leap years', missing components, invalid day, monnth or year components.
34
+ * This will validate for 'leap years', missing components, invalid day, month or year components.
35
35
  * EXAMPLE USE : const { message, propsInError } = validateDate('2-11-2020')
36
36
  *
37
37
  * Note that an empty string is not considered invalid. You should use
@@ -56,53 +56,47 @@ var validateDate = function validateDate(date) {
56
56
  year = _formattedDate$split2[2];
57
57
  var intDay = parseInt(day, 10);
58
58
  var intMonth = parseInt(month, 10);
59
+ var badProps = [];
60
+ var messages = [];
59
61
  if (year.length === 0) {
60
- return {
61
- message: 'Date must include a year',
62
- propsInError: {
63
- year: true
64
- }
65
- };
66
- }
67
- if (year.length !== 4) {
68
- return {
69
- message: 'Year must be 4 numbers',
70
- propsInError: {
71
- year: true
72
- }
73
- };
62
+ badProps.push('year');
63
+ messages.push('Date must include a year');
64
+ } else if (year.length !== 4) {
65
+ badProps.push('year');
66
+ messages.push('Year must be 4 numbers');
74
67
  }
75
68
  if (month.length === 0) {
76
- return {
77
- message: 'Date must include a month',
78
- propsInError: {
79
- month: true
80
- }
81
- };
82
- }
83
- if (intMonth > 12 || intMonth < 1) {
84
- return {
85
- message: 'Month must be between 1 and 12',
86
- propsInError: {
87
- month: true
88
- }
89
- };
69
+ badProps.push('month');
70
+ messages.push('Date must include a month');
71
+ } else if (intMonth > 12 || intMonth < 1) {
72
+ badProps.push('month');
73
+ messages.push('Month must be between 1 and 12');
90
74
  }
91
75
  if (day.length === 0) {
92
- return {
93
- message: 'Date must include a day',
94
- propsInError: {
95
- day: true
96
- }
97
- };
76
+ badProps.push('day');
77
+ messages.push('Date must include a day');
78
+ } else {
79
+ var maxDays = maxMonthDays(month, year);
80
+ if (intDay > maxDays || intDay < 1) {
81
+ badProps.push('day');
82
+ messages.push("Day must be between 1 and ".concat(maxDays));
83
+ }
98
84
  }
99
- var maxDays = maxMonthDays(month, year);
100
- if (intDay > maxDays || intDay < 1) {
85
+ if (badProps.length > 0) {
86
+ // return single error or generic error if multiple problem
87
+ var propsInError = {};
88
+ if (badProps.includes('day')) {
89
+ propsInError.day = true;
90
+ }
91
+ if (badProps.includes('month')) {
92
+ propsInError.month = true;
93
+ }
94
+ if (badProps.includes('year')) {
95
+ propsInError.year = true;
96
+ }
101
97
  return {
102
- message: "Day must be between 1 and ".concat(maxDays),
103
- propsInError: {
104
- day: true
105
- }
98
+ message: badProps.length === 1 ? messages[0] : 'Enter a valid date',
99
+ propsInError: propsInError
106
100
  };
107
101
  }
108
102
  if ((0, _dayjs.default)(formattedDate, _utils.DATE_FORMAT).format(_utils.DATE_FORMAT) !== formattedDate) {
@@ -55,7 +55,8 @@ describe('utils', function () {
55
55
  });
56
56
  });
57
57
  test('should return an error if the year contains more than 4 numbers', function () {
58
- var output = (0, _validateDate.default)('29-02-20202', 'DD-MM-YYYY');
58
+ // 20202 is not a leap year,so 29th Feb not valid
59
+ var output = (0, _validateDate.default)('28-02-20202', 'DD-MM-YYYY');
59
60
  expect(output).toEqual({
60
61
  message: 'Year must be 4 numbers',
61
62
  propsInError: {
@@ -99,6 +100,25 @@ describe('utils', function () {
99
100
  }
100
101
  });
101
102
  });
103
+ test('should handle multiple errors', function () {
104
+ var output = (0, _validateDate.default)('45-13-2020', 'DD-MM-YYYY');
105
+ expect(output).toEqual({
106
+ message: 'Enter a valid date',
107
+ propsInError: {
108
+ day: true,
109
+ month: true
110
+ }
111
+ });
112
+ var output2 = (0, _validateDate.default)('45-13-', 'DD-MM-YYYY');
113
+ expect(output2).toEqual({
114
+ message: 'Enter a valid date',
115
+ propsInError: {
116
+ day: true,
117
+ month: true,
118
+ year: true
119
+ }
120
+ });
121
+ });
102
122
  test('should correctly identify the maximum numbers of days in a given month and year', function () {
103
123
  var max = (0, _validateDate.maxMonthDays)('02', '2024');
104
124
  expect(max).toEqual(29);
@@ -37,36 +37,33 @@ var validateTime = function validateTime(time) {
37
37
  minute = _formattedDate$split2[1];
38
38
  var intHour = parseInt(hour, 10);
39
39
  var intMinute = parseInt(minute, 10);
40
+ var badProps = [];
41
+ var messages = [];
40
42
  if (hour.length === 0) {
41
- return {
42
- message: 'Time must include a hour',
43
- propsInError: {
44
- hour: true
45
- }
46
- };
47
- }
48
- if (intHour > 23 || intHour < 0) {
49
- return {
50
- message: 'Hour must be between 0 and 23',
51
- propsInError: {
52
- hour: true
53
- }
54
- };
43
+ badProps.push('hour');
44
+ messages.push('Time must include a hour');
45
+ } else if (intHour > 23 || intHour < 0) {
46
+ badProps.push('hour');
47
+ messages.push('Hour must be between 0 and 23');
55
48
  }
56
49
  if (minute.length === 0) {
57
- return {
58
- message: 'Time must include a minute',
59
- propsInError: {
60
- minute: true
61
- }
62
- };
50
+ badProps.push('minute');
51
+ messages.push('Time must include a minute');
52
+ } else if (intMinute > 59 || intMinute < 0) {
53
+ badProps.push('minute');
54
+ messages.push('Minute must be between 0 and 59');
63
55
  }
64
- if (intMinute > 59 || intMinute < 0) {
56
+ if (badProps.length > 0) {
57
+ var propsInError = {};
58
+ if (badProps.includes('hour')) {
59
+ propsInError.hour = true;
60
+ }
61
+ if (badProps.includes('minute')) {
62
+ propsInError.minute = true;
63
+ }
65
64
  return {
66
- message: 'Minute must be between 0 and 59',
67
- propsInError: {
68
- minute: true
69
- }
65
+ message: badProps.length > 1 ? 'Enter a valid time' : messages[0],
66
+ propsInError: propsInError
70
67
  };
71
68
  }
72
69
  return {
@@ -54,6 +54,16 @@ describe('utils', function () {
54
54
  }
55
55
  });
56
56
  });
57
+ test('should report multiple errors', function () {
58
+ var output = (0, _validateTime.default)('24:75');
59
+ expect(output).toEqual({
60
+ message: 'Enter a valid time',
61
+ propsInError: {
62
+ hour: true,
63
+ minute: true
64
+ }
65
+ });
66
+ });
57
67
  });
58
68
  });
59
69
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "5.49.1",
3
+ "version": "5.49.2",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",