@ukhomeoffice/cop-react-form-renderer 2.2.0 → 2.5.0

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.
Files changed (51) hide show
  1. package/dist/components/FormRenderer/FormRenderer.js +105 -6
  2. package/dist/components/FormRenderer/FormRenderer.test.js +235 -0
  3. package/dist/components/FormRenderer/helpers/getNextPageId.js +3 -0
  4. package/dist/components/FormRenderer/helpers/getNextPageId.test.js +6 -0
  5. package/dist/components/FormRenderer/helpers/getPage.js +1 -1
  6. package/dist/components/FormRenderer/helpers/getSubmissionStatus.js +18 -1
  7. package/dist/components/FormRenderer/helpers/getSubmissionStatus.test.js +44 -6
  8. package/dist/components/FormRenderer/helpers/getUpdatedSectionStates.js +50 -0
  9. package/dist/components/FormRenderer/helpers/getUpdatedSectionStates.test.js +122 -0
  10. package/dist/components/FormRenderer/helpers/index.js +7 -1
  11. package/dist/components/TaskList/Task.js +83 -0
  12. package/dist/components/TaskList/Task.test.js +112 -0
  13. package/dist/components/TaskList/TaskList.js +112 -0
  14. package/dist/components/TaskList/TaskList.scss +70 -0
  15. package/dist/components/TaskList/TaskList.stories.mdx +57 -0
  16. package/dist/components/TaskList/TaskList.test.js +223 -0
  17. package/dist/components/TaskList/TaskState.js +42 -0
  18. package/dist/components/TaskList/TaskState.test.js +99 -0
  19. package/dist/components/TaskList/index.js +13 -0
  20. package/dist/json/taskList.json +228 -0
  21. package/dist/json/userProfile.data.json +2 -1
  22. package/dist/models/ComponentTypes.js +3 -1
  23. package/dist/models/FormTypes.js +2 -0
  24. package/dist/models/HubFormats.js +3 -1
  25. package/dist/models/PageAction.js +5 -0
  26. package/dist/models/TaskStates.js +41 -0
  27. package/dist/models/index.js +8 -0
  28. package/dist/utils/Component/cleanAttributes.js +1 -1
  29. package/dist/utils/Component/getComponent.js +8 -0
  30. package/dist/utils/Component/isEditable.js +1 -1
  31. package/dist/utils/FormPage/getPageActions.js +6 -0
  32. package/dist/utils/Hub/getFormHub.js +4 -0
  33. package/dist/utils/Hub/getFormHub.test.js +10 -2
  34. package/dist/utils/Validate/additional/index.js +55 -0
  35. package/dist/utils/Validate/additional/index.test.js +69 -0
  36. package/dist/utils/Validate/additional/mustBeAfter.js +37 -0
  37. package/dist/utils/Validate/additional/mustBeAfter.test.js +98 -0
  38. package/dist/utils/Validate/additional/mustBeBefore.js +35 -0
  39. package/dist/utils/Validate/additional/mustBeBefore.test.js +66 -0
  40. package/dist/utils/Validate/additional/mustBeInTheFuture.js +42 -0
  41. package/dist/utils/Validate/additional/mustBeInTheFuture.test.js +34 -0
  42. package/dist/utils/Validate/additional/mustBeInThePast.js +44 -0
  43. package/dist/utils/Validate/additional/mustBeInThePast.test.js +34 -0
  44. package/dist/utils/Validate/additional/utils.js +66 -0
  45. package/dist/utils/Validate/validateComponent.js +37 -0
  46. package/dist/utils/Validate/validateComponent.test.js +48 -2
  47. package/dist/utils/Validate/validateDate.js +144 -0
  48. package/dist/utils/Validate/validateDate.test.js +112 -0
  49. package/dist/utils/Validate/validateTime.js +76 -0
  50. package/dist/utils/Validate/validateTime.test.js +55 -0
  51. package/package.json +3 -2
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ var _dayjs = _interopRequireDefault(require("dayjs"));
4
+
5
+ var _mustBeInThePast = _interopRequireDefault(require("./mustBeInThePast"));
6
+
7
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
+
9
+ describe('utils', function () {
10
+ describe('Validate', function () {
11
+ describe('additional', function () {
12
+ describe('mustBeInThePast', function () {
13
+ test('should return true given a date in the past', function () {
14
+ var result = (0, _mustBeInThePast.default)((0, _dayjs.default)().subtract(1, 'day').format('DD-MM-YYYY'));
15
+ expect(result).toEqual(true);
16
+ });
17
+ test('should return false given a date in the future', function () {
18
+ var result = (0, _mustBeInThePast.default)((0, _dayjs.default)().add(1, 'day').format('DD-MM-YYYY'));
19
+ expect(result).toEqual(false);
20
+ });
21
+ test('should return false given todays date', function () {
22
+ var result = (0, _mustBeInThePast.default)((0, _dayjs.default)().format('DD-MM-YYYY'));
23
+ expect(result).toEqual(false);
24
+ });
25
+ test('should return true given todays date if todayAllowed is true', function () {
26
+ var result = (0, _mustBeInThePast.default)((0, _dayjs.default)().format('DD-MM-YYYY'), {
27
+ todayAllowed: true
28
+ });
29
+ expect(result).toEqual(true);
30
+ });
31
+ });
32
+ });
33
+ });
34
+ });
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.formattedTime = exports.formatString = exports.DATE_FORMAT = void 0;
7
+
8
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
9
+
10
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
11
+
12
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
13
+
14
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
15
+
16
+ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
17
+
18
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
19
+
20
+ var isNumeric = function isNumeric(value) {
21
+ return /^-?\d+$/.test(value);
22
+ };
23
+
24
+ var formatString = function formatString(date) {
25
+ var _date$split = date.split('-'),
26
+ _date$split2 = _slicedToArray(_date$split, 3),
27
+ day = _date$split2[0],
28
+ month = _date$split2[1],
29
+ year = _date$split2[2];
30
+
31
+ return "".concat(formatInTwoDigits(day), "-").concat(formatInTwoDigits(month), "-").concat(year);
32
+ };
33
+
34
+ exports.formatString = formatString;
35
+
36
+ var formattedTime = function formattedTime(time) {
37
+ var _time$split = time.split(':'),
38
+ _time$split2 = _slicedToArray(_time$split, 2),
39
+ hour = _time$split2[0],
40
+ minute = _time$split2[1];
41
+
42
+ return "".concat(formatInTwoDigits(hour), ":").concat(formatInTwoDigits(minute));
43
+ };
44
+ /**
45
+
46
+ /**
47
+ * Prepends a leading zero if the 'date component' parameter contains only a single digit.
48
+ * if the parameter contains more than one digit it is returned unchanged.
49
+ * If the parameter contains a non-numeric value it returned as an empty string.
50
+ * @param {string} dateComponent - the value representing a day or month.
51
+ * @returns the value (appended with a leading zero, if the value passed in was a single digit)
52
+ */
53
+
54
+
55
+ exports.formattedTime = formattedTime;
56
+
57
+ var formatInTwoDigits = function formatInTwoDigits(dateComponent) {
58
+ if (isNumeric(dateComponent)) {
59
+ return dateComponent.padStart(2, '0');
60
+ }
61
+
62
+ return '';
63
+ };
64
+
65
+ var DATE_FORMAT = 'DD-MM-YYYY';
66
+ exports.DATE_FORMAT = DATE_FORMAT;
@@ -13,6 +13,12 @@ var _validateEmail = _interopRequireDefault(require("./validateEmail"));
13
13
 
14
14
  var _validateRequired = _interopRequireDefault(require("./validateRequired"));
15
15
 
16
+ var _validateDate2 = _interopRequireDefault(require("./validateDate"));
17
+
18
+ var _validateTime2 = _interopRequireDefault(require("./validateTime"));
19
+
20
+ var _additional = _interopRequireDefault(require("./additional"));
21
+
16
22
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
23
 
18
24
  function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
@@ -29,6 +35,7 @@ var validateComponent = function validateComponent(component, formData) {
29
35
 
30
36
  if (component && (0, _showComponent.default)(component, formData)) {
31
37
  var value = data[component.fieldId];
38
+ delete component.propsInError;
32
39
 
33
40
  if (component.required) {
34
41
  error = (0, _validateRequired.default)(value, component.label, component.custom_errors);
@@ -38,6 +45,36 @@ var validateComponent = function validateComponent(component, formData) {
38
45
  error = (0, _validateEmail.default)(value, component.label);
39
46
  }
40
47
 
48
+ if (!error && component.type === _models.ComponentTypes.DATE && value) {
49
+ var _validateDate = (0, _validateDate2.default)(value),
50
+ message = _validateDate.message,
51
+ propsInError = _validateDate.propsInError;
52
+
53
+ component.propsInError = propsInError;
54
+ error = message;
55
+ }
56
+
57
+ if (!error && component.type === _models.ComponentTypes.TIME && value) {
58
+ var _validateTime = (0, _validateTime2.default)(value),
59
+ _message = _validateTime.message,
60
+ _propsInError = _validateTime.propsInError;
61
+
62
+ component.propsInError = _propsInError;
63
+ error = _message;
64
+ }
65
+
66
+ if (!error && component.additionalValidation) {
67
+ error = (0, _additional.default)(component, value);
68
+
69
+ if (component.type === _models.ComponentTypes.DATE && error) {
70
+ component.propsInError = {
71
+ day: true,
72
+ month: true,
73
+ year: true
74
+ };
75
+ }
76
+ }
77
+
41
78
  component.error = error;
42
79
  }
43
80
 
@@ -11,13 +11,14 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
11
11
  describe('utils', function () {
12
12
  describe('Validate', function () {
13
13
  describe('component', function () {
14
- var setup = function setup(id, type, label, required) {
14
+ var setup = function setup(id, type, label, required, additionalValidation) {
15
15
  return {
16
16
  id: id,
17
17
  fieldId: id,
18
18
  type: type,
19
19
  label: label,
20
- required: required
20
+ required: required,
21
+ additionalValidation: additionalValidation
21
22
  };
22
23
  };
23
24
 
@@ -114,6 +115,51 @@ describe('utils', function () {
114
115
  });
115
116
  });
116
117
  });
118
+ describe('when the component is a Date Input', function () {
119
+ var ID = 'field';
120
+ it('should always reject invalid dates', function () {
121
+ var LABEL = 'Field';
122
+ var COMPONENT = setup(ID, _models.ComponentTypes.DATE, LABEL, false);
123
+
124
+ var DATA = _defineProperty({}, ID, '25-45-2033');
125
+
126
+ expect((0, _validateComponent.default)(COMPONENT, DATA)).toEqual({
127
+ error: 'Month must be between 1 and 12',
128
+ id: ID
129
+ });
130
+ });
131
+ it('should apply optional validators when specified', function () {
132
+ var LABEL = 'Field';
133
+
134
+ var DATA = _defineProperty({}, ID, '25-3-3033');
135
+
136
+ var ADDITIONAL_VALIDATION = [{
137
+ function: 'mustBeBefore',
138
+ value: 3,
139
+ unit: 'day',
140
+ message: 'Date must be less than 3 days in the future'
141
+ }];
142
+ var COMPONENT = setup(ID, _models.ComponentTypes.DATE, LABEL, false, ADDITIONAL_VALIDATION);
143
+ expect((0, _validateComponent.default)(COMPONENT, DATA)).toEqual({
144
+ error: 'Date must be less than 3 days in the future',
145
+ id: ID
146
+ });
147
+ });
148
+ });
149
+ describe('when the component is a Time Input', function () {
150
+ var ID = 'field';
151
+ it('should always reject invalid time', function () {
152
+ var LABEL = 'Field';
153
+ var COMPONENT = setup(ID, _models.ComponentTypes.TIME, LABEL, false);
154
+
155
+ var DATA = _defineProperty({}, ID, '25:45');
156
+
157
+ expect((0, _validateComponent.default)(COMPONENT, DATA)).toEqual({
158
+ error: 'Hour must be between 0 and 23',
159
+ id: ID
160
+ });
161
+ });
162
+ });
117
163
  });
118
164
  });
119
165
  });
@@ -0,0 +1,144 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.maxMonthDays = exports.default = void 0;
7
+
8
+ var _dayjs = _interopRequireDefault(require("dayjs"));
9
+
10
+ var _customParseFormat = _interopRequireDefault(require("dayjs/plugin/customParseFormat"));
11
+
12
+ var _isToday = _interopRequireDefault(require("dayjs/plugin/isToday"));
13
+
14
+ var _isLeapYear = _interopRequireDefault(require("dayjs/plugin/isLeapYear"));
15
+
16
+ var _utils = require("./additional/utils");
17
+
18
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
+
20
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
21
+
22
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
23
+
24
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
25
+
26
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
27
+
28
+ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
29
+
30
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
31
+
32
+ _dayjs.default.extend(_customParseFormat.default);
33
+
34
+ _dayjs.default.extend(_isToday.default);
35
+
36
+ _dayjs.default.extend(_isLeapYear.default);
37
+
38
+ var maxMonthDays = function maxMonthDays(month, year) {
39
+ if (month === '02') {
40
+ return (0, _dayjs.default)().year(year).isLeapYear() ? 29 : 28;
41
+ } else if (['04', '06', '09', '11'].includes(month)) {
42
+ return 30;
43
+ }
44
+
45
+ return 31;
46
+ };
47
+
48
+ exports.maxMonthDays = maxMonthDays;
49
+
50
+ /**
51
+ * Checks if a date passed is a valid date.
52
+ * This will validate for 'leap years', missing components, invalid day, monnth or year components.
53
+ * EXAMPLE USE : const { message, propsInError } = validateDate('2-11-2020')
54
+ * @param {string} date date as a string
55
+ * @returns an object with an error message and instructions for which parts of the date are in error
56
+ * or undefined for both if the date is valid
57
+ */
58
+ var validateDate = function validateDate(date) {
59
+ var formattedDate = (0, _utils.formatString)(date);
60
+
61
+ var _formattedDate$split = formattedDate.split('-'),
62
+ _formattedDate$split2 = _slicedToArray(_formattedDate$split, 3),
63
+ day = _formattedDate$split2[0],
64
+ month = _formattedDate$split2[1],
65
+ year = _formattedDate$split2[2];
66
+
67
+ var intDay = parseInt(day, 10);
68
+ var intMonth = parseInt(month, 10);
69
+
70
+ if (year.length === 0) {
71
+ return {
72
+ message: 'Date must include a year',
73
+ propsInError: {
74
+ year: true
75
+ }
76
+ };
77
+ }
78
+
79
+ if (year.length !== 4) {
80
+ return {
81
+ message: 'Year must be 4 numbers',
82
+ propsInError: {
83
+ year: true
84
+ }
85
+ };
86
+ }
87
+
88
+ if (month.length === 0) {
89
+ return {
90
+ message: 'Date must include a month',
91
+ propsInError: {
92
+ month: true
93
+ }
94
+ };
95
+ }
96
+
97
+ if (intMonth > 12 || intMonth < 1) {
98
+ return {
99
+ message: 'Month must be between 1 and 12',
100
+ propsInError: {
101
+ month: true
102
+ }
103
+ };
104
+ }
105
+
106
+ if (day.length === 0) {
107
+ return {
108
+ message: 'Date must include a day',
109
+ propsInError: {
110
+ day: true
111
+ }
112
+ };
113
+ }
114
+
115
+ var maxDays = maxMonthDays(month, year);
116
+
117
+ if (intDay > maxDays || intDay < 1) {
118
+ return {
119
+ message: "Day must be between 1 and ".concat(maxDays),
120
+ propsInError: {
121
+ day: true
122
+ }
123
+ };
124
+ }
125
+
126
+ if ((0, _dayjs.default)(formattedDate, _utils.DATE_FORMAT).format(_utils.DATE_FORMAT) !== formattedDate) {
127
+ return {
128
+ message: 'Enter a valid date',
129
+ propsInError: {
130
+ day: true,
131
+ month: true,
132
+ year: true
133
+ }
134
+ };
135
+ }
136
+
137
+ return {
138
+ message: undefined,
139
+ propsInError: undefined
140
+ };
141
+ };
142
+
143
+ var _default = validateDate;
144
+ exports.default = _default;
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+
3
+ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
4
+
5
+ var _validateDate = _interopRequireWildcard(require("./validateDate"));
6
+
7
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
8
+
9
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
10
+
11
+ describe('utils', function () {
12
+ describe('Validate', function () {
13
+ describe('date', function () {
14
+ test('should return undefined if a date string is valid', function () {
15
+ var output = (0, _validateDate.default)('28-02-2024', 'DD-MM-YYYY');
16
+ expect(output).toEqual({
17
+ message: undefined,
18
+ propsInError: undefined
19
+ });
20
+ });
21
+ test('should return undefined to a correct leap year date', function () {
22
+ var output = (0, _validateDate.default)('29-02-2024', 'DD-MM-YYYY');
23
+ expect(output).toEqual({
24
+ message: undefined,
25
+ propsInError: undefined
26
+ });
27
+ });
28
+ test('should validate false if NOT a leap year & 29 Feb is entered', function () {
29
+ var output = (0, _validateDate.default)('29-02-2023', 'DD-MM-YYYY');
30
+ expect(output).toEqual({
31
+ message: 'Day must be between 1 and 28',
32
+ propsInError: {
33
+ day: true
34
+ }
35
+ });
36
+ });
37
+ test('should return an error if no year is given', function () {
38
+ var output = (0, _validateDate.default)('29-02-', 'DD-MM-YYYY');
39
+ expect(output).toEqual({
40
+ message: 'Date must include a year',
41
+ propsInError: {
42
+ year: true
43
+ }
44
+ });
45
+ });
46
+ test('should return an error if the year contains less than 4 numbers', function () {
47
+ var output = (0, _validateDate.default)('29-02-20', 'DD-MM-YYYY');
48
+ expect(output).toEqual({
49
+ message: 'Year must be 4 numbers',
50
+ propsInError: {
51
+ year: true
52
+ }
53
+ });
54
+ });
55
+ test('should return an error if the year contains more than 4 numbers', function () {
56
+ var output = (0, _validateDate.default)('29-02-20202', 'DD-MM-YYYY');
57
+ expect(output).toEqual({
58
+ message: 'Year must be 4 numbers',
59
+ propsInError: {
60
+ year: true
61
+ }
62
+ });
63
+ });
64
+ test('should return an error if no month is given', function () {
65
+ var output = (0, _validateDate.default)('29--2020', 'DD-MM-YYYY');
66
+ expect(output).toEqual({
67
+ message: 'Date must include a month',
68
+ propsInError: {
69
+ month: true
70
+ }
71
+ });
72
+ });
73
+ test('should return an error if the month is not between 1 and 12', function () {
74
+ var output = (0, _validateDate.default)('29-14-2020', 'DD-MM-YYYY');
75
+ expect(output).toEqual({
76
+ message: 'Month must be between 1 and 12',
77
+ propsInError: {
78
+ month: true
79
+ }
80
+ });
81
+ });
82
+ test('should return an error if no day is given', function () {
83
+ var output = (0, _validateDate.default)('-03-2020', 'DD-MM-YYYY');
84
+ expect(output).toEqual({
85
+ message: 'Date must include a day',
86
+ propsInError: {
87
+ day: true
88
+ }
89
+ });
90
+ });
91
+ test('should return an error if the day is not between 1 and 31', function () {
92
+ var output = (0, _validateDate.default)('45-12-2020', 'DD-MM-YYYY');
93
+ expect(output).toEqual({
94
+ message: 'Day must be between 1 and 31',
95
+ propsInError: {
96
+ day: true
97
+ }
98
+ });
99
+ });
100
+ test('should correctly identify the maximum numbers of days in a given month and year', function () {
101
+ var max = (0, _validateDate.maxMonthDays)('02', '2024');
102
+ expect(max).toEqual(29);
103
+ max = (0, _validateDate.maxMonthDays)('02', '2023');
104
+ expect(max).toEqual(28);
105
+ max = (0, _validateDate.maxMonthDays)('06', '2023');
106
+ expect(max).toEqual(30);
107
+ max = (0, _validateDate.maxMonthDays)('07', '2023');
108
+ expect(max).toEqual(31);
109
+ });
110
+ });
111
+ });
112
+ });
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _utils = require("./additional/utils");
9
+
10
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
11
+
12
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
13
+
14
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
15
+
16
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
17
+
18
+ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
19
+
20
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
21
+
22
+ var validateTime = function validateTime(time) {
23
+ var formattedDate = (0, _utils.formattedTime)(time);
24
+
25
+ var _formattedDate$split = formattedDate.split(':'),
26
+ _formattedDate$split2 = _slicedToArray(_formattedDate$split, 2),
27
+ hour = _formattedDate$split2[0],
28
+ minute = _formattedDate$split2[1];
29
+
30
+ var intHour = parseInt(hour, 10);
31
+ var intMinute = parseInt(minute, 10);
32
+
33
+ if (hour.length === 0) {
34
+ return {
35
+ message: 'Time must include a hour',
36
+ propsInError: {
37
+ hour: true
38
+ }
39
+ };
40
+ }
41
+
42
+ if (intHour > 23 || intHour < 0) {
43
+ return {
44
+ message: 'Hour must be between 0 and 23',
45
+ propsInError: {
46
+ hour: true
47
+ }
48
+ };
49
+ }
50
+
51
+ if (minute.length === 0) {
52
+ return {
53
+ message: 'Time must include a minute',
54
+ propsInError: {
55
+ minute: true
56
+ }
57
+ };
58
+ }
59
+
60
+ if (intMinute > 59 || intMinute < 0) {
61
+ return {
62
+ message: 'Minute must be between 0 and 59',
63
+ propsInError: {
64
+ minute: true
65
+ }
66
+ };
67
+ }
68
+
69
+ return {
70
+ message: undefined,
71
+ propsInError: undefined
72
+ };
73
+ };
74
+
75
+ var _default = validateTime;
76
+ exports.default = _default;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+
3
+ var _validateTime = _interopRequireDefault(require("./validateTime"));
4
+
5
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
6
+
7
+ describe('utils', function () {
8
+ describe('Validate', function () {
9
+ describe('time', function () {
10
+ test('should return undefined if a time string is valid', function () {
11
+ var output = (0, _validateTime.default)('14:30');
12
+ expect(output).toEqual({
13
+ message: undefined,
14
+ propsInError: undefined
15
+ });
16
+ });
17
+ test('should return an error if no hour is given', function () {
18
+ var output = (0, _validateTime.default)(':30');
19
+ expect(output).toEqual({
20
+ message: 'Time must include a hour',
21
+ propsInError: {
22
+ hour: true
23
+ }
24
+ });
25
+ });
26
+ test('should return an error if the hour is not between 0 and 23', function () {
27
+ var output = (0, _validateTime.default)('35:30');
28
+ expect(output).toEqual({
29
+ message: 'Hour must be between 0 and 23',
30
+ propsInError: {
31
+ hour: true
32
+ }
33
+ });
34
+ });
35
+ test('should return an error if no minute is given', function () {
36
+ var output = (0, _validateTime.default)('14:');
37
+ expect(output).toEqual({
38
+ message: 'Time must include a minute',
39
+ propsInError: {
40
+ minute: true
41
+ }
42
+ });
43
+ });
44
+ test('should return an error if the minute is not between 0 and 59', function () {
45
+ var output = (0, _validateTime.default)('14:75');
46
+ expect(output).toEqual({
47
+ message: 'Minute must be between 0 and 59',
48
+ propsInError: {
49
+ minute: true
50
+ }
51
+ });
52
+ });
53
+ });
54
+ });
55
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ukhomeoffice/cop-react-form-renderer",
3
- "version": "2.2.0",
3
+ "version": "2.5.0",
4
4
  "private": false,
5
5
  "scripts": {
6
6
  "clean": "rimraf dist",
@@ -16,8 +16,9 @@
16
16
  "post-compile": "rimraf dist/*.test.* dist/**/*.test.* dist/**/*.stories.* dist/docs dist/assets"
17
17
  },
18
18
  "dependencies": {
19
- "@ukhomeoffice/cop-react-components": "1.1.2",
19
+ "@ukhomeoffice/cop-react-components": "1.2.0",
20
20
  "axios": "^0.21.1",
21
+ "dayjs": "^1.11.0",
21
22
  "govuk-frontend": "^3.13.0",
22
23
  "web-vitals": "^1.0.1"
23
24
  },