@ukhomeoffice/cop-react-form-renderer 2.2.0 → 2.6.1-alpha
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.
- package/dist/components/FormComponent/Container.test.js +250 -0
- package/dist/components/FormComponent/FormComponent.js +101 -22
- package/dist/components/FormPage/FormPage.js +2 -1
- package/dist/components/FormRenderer/FormRenderer.js +105 -6
- package/dist/components/FormRenderer/FormRenderer.test.js +235 -0
- package/dist/components/FormRenderer/helpers/getNextPageId.js +3 -0
- package/dist/components/FormRenderer/helpers/getNextPageId.test.js +6 -0
- package/dist/components/FormRenderer/helpers/getPage.js +1 -1
- package/dist/components/FormRenderer/helpers/getSubmissionStatus.js +18 -1
- package/dist/components/FormRenderer/helpers/getSubmissionStatus.test.js +44 -6
- package/dist/components/FormRenderer/helpers/getUpdatedSectionStates.js +50 -0
- package/dist/components/FormRenderer/helpers/getUpdatedSectionStates.test.js +122 -0
- package/dist/components/FormRenderer/helpers/index.js +7 -1
- package/dist/components/TaskList/Task.js +83 -0
- package/dist/components/TaskList/Task.test.js +112 -0
- package/dist/components/TaskList/TaskList.js +112 -0
- package/dist/components/TaskList/TaskList.scss +70 -0
- package/dist/components/TaskList/TaskList.stories.mdx +57 -0
- package/dist/components/TaskList/TaskList.test.js +223 -0
- package/dist/components/TaskList/TaskState.js +42 -0
- package/dist/components/TaskList/TaskState.test.js +99 -0
- package/dist/components/TaskList/index.js +13 -0
- package/dist/hooks/index.js +19 -1
- package/dist/json/taskList.json +228 -0
- package/dist/json/userProfile.data.json +2 -1
- package/dist/models/ComponentTypes.js +5 -1
- package/dist/models/FormTypes.js +2 -0
- package/dist/models/HubFormats.js +3 -1
- package/dist/models/PageAction.js +5 -0
- package/dist/models/TaskStates.js +41 -0
- package/dist/models/index.js +8 -0
- package/dist/utils/CheckYourAnswers/getCYARowsForContainer.js +40 -0
- package/dist/utils/CheckYourAnswers/getCYARowsForContainer.test.js +257 -0
- package/dist/utils/CheckYourAnswers/getCYARowsForPage.js +9 -1
- package/dist/utils/CheckYourAnswers/getCYARowsForPage.test.js +44 -0
- package/dist/utils/CheckYourAnswers/showComponentCYA.js +6 -0
- package/dist/utils/Component/cleanAttributes.js +1 -1
- package/dist/utils/Component/getComponent.js +8 -0
- package/dist/utils/Component/isEditable.js +1 -1
- package/dist/utils/Component/showComponent.js +2 -20
- package/dist/utils/Condition/index.js +19 -0
- package/dist/utils/Condition/meetsAllConditions.js +40 -0
- package/dist/utils/Condition/meetsAllConditions.test.js +62 -0
- package/dist/utils/{meetsCondition.js → Condition/meetsCondition.js} +0 -0
- package/dist/utils/Condition/meetsCondition.test.js +302 -0
- package/dist/utils/Condition/setupConditions.js +47 -0
- package/dist/utils/Condition/setupConditions.test.js +35 -0
- package/dist/utils/Container/getEditableComponents.js +38 -0
- package/dist/utils/Container/getEditableComponents.test.js +157 -0
- package/dist/utils/Container/index.js +22 -0
- package/dist/utils/Container/setupNesting.js +45 -0
- package/dist/utils/Container/setupNesting.test.js +92 -0
- package/dist/utils/Container/showContainer.js +61 -0
- package/dist/utils/Container/showContainer.test.js +128 -0
- package/dist/utils/Data/getDataPath.js +90 -0
- package/dist/utils/Data/getDataPath.test.js +52 -0
- package/dist/utils/Data/index.js +3 -0
- package/dist/utils/FormPage/getFormPage.js +4 -2
- package/dist/utils/FormPage/getFormPage.test.js +18 -7
- package/dist/utils/FormPage/getFormPages.test.js +5 -2
- package/dist/utils/FormPage/getPageActions.js +6 -0
- package/dist/utils/FormPage/index.js +0 -3
- package/dist/utils/FormPage/showFormPage.js +7 -27
- package/dist/utils/Hub/getFormHub.js +4 -0
- package/dist/utils/Hub/getFormHub.test.js +15 -4
- package/dist/utils/Validate/additional/index.js +55 -0
- package/dist/utils/Validate/additional/index.test.js +69 -0
- package/dist/utils/Validate/additional/mustBeAfter.js +37 -0
- package/dist/utils/Validate/additional/mustBeAfter.test.js +98 -0
- package/dist/utils/Validate/additional/mustBeBefore.js +35 -0
- package/dist/utils/Validate/additional/mustBeBefore.test.js +66 -0
- package/dist/utils/Validate/additional/mustBeInTheFuture.js +42 -0
- package/dist/utils/Validate/additional/mustBeInTheFuture.test.js +34 -0
- package/dist/utils/Validate/additional/mustBeInThePast.js +44 -0
- package/dist/utils/Validate/additional/mustBeInThePast.test.js +34 -0
- package/dist/utils/Validate/additional/utils.js +66 -0
- package/dist/utils/Validate/index.js +7 -1
- package/dist/utils/Validate/validateComponent.js +57 -0
- package/dist/utils/Validate/validateComponent.test.js +92 -2
- package/dist/utils/Validate/validateDate.js +155 -0
- package/dist/utils/Validate/validateDate.test.js +118 -0
- package/dist/utils/Validate/validatePage.js +6 -8
- package/dist/utils/Validate/validateTime.js +95 -0
- package/dist/utils/Validate/validateTime.test.js +61 -0
- package/dist/utils/index.js +6 -3
- package/package.json +5 -4
- package/dist/utils/FormPage/getEditableComponents.js +0 -28
- package/dist/utils/FormPage/getEditableComponents.test.js +0 -75
|
@@ -9,26 +9,53 @@ var _models = require("../../models");
|
|
|
9
9
|
|
|
10
10
|
var _showComponent = _interopRequireDefault(require("../Component/showComponent"));
|
|
11
11
|
|
|
12
|
+
var _additional = _interopRequireDefault(require("./additional"));
|
|
13
|
+
|
|
14
|
+
var _validateDate2 = _interopRequireDefault(require("./validateDate"));
|
|
15
|
+
|
|
12
16
|
var _validateEmail = _interopRequireDefault(require("./validateEmail"));
|
|
13
17
|
|
|
14
18
|
var _validateRequired = _interopRequireDefault(require("./validateRequired"));
|
|
15
19
|
|
|
20
|
+
var _validateTime2 = _interopRequireDefault(require("./validateTime"));
|
|
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); }
|
|
19
25
|
|
|
26
|
+
var validateContainer = function validateContainer(container, formData) {
|
|
27
|
+
var errors = [];
|
|
28
|
+
|
|
29
|
+
if (Array.isArray(container.components)) {
|
|
30
|
+
var fd = formData && container.fieldId ? formData[container.fieldId] : formData;
|
|
31
|
+
container.components.forEach(function (component) {
|
|
32
|
+
errors.push(validateComponent(component, fd));
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return errors.filter(function (e) {
|
|
37
|
+
return !!e;
|
|
38
|
+
});
|
|
39
|
+
};
|
|
20
40
|
/**
|
|
21
41
|
* Validates a single component.
|
|
22
42
|
* @param {object} component The component to validate.
|
|
23
43
|
* @param {object} formData The data to use that holds this component's value.
|
|
24
44
|
* @returns The first encountered error with the component.
|
|
25
45
|
*/
|
|
46
|
+
|
|
47
|
+
|
|
26
48
|
var validateComponent = function validateComponent(component, formData) {
|
|
27
49
|
var error = undefined;
|
|
28
50
|
var data = formData && _typeof(formData) === 'object' ? formData : {};
|
|
29
51
|
|
|
30
52
|
if (component && (0, _showComponent.default)(component, formData)) {
|
|
53
|
+
if (component.type === _models.ComponentTypes.CONTAINER) {
|
|
54
|
+
return validateContainer(component, formData);
|
|
55
|
+
}
|
|
56
|
+
|
|
31
57
|
var value = data[component.fieldId];
|
|
58
|
+
delete component.propsInError;
|
|
32
59
|
|
|
33
60
|
if (component.required) {
|
|
34
61
|
error = (0, _validateRequired.default)(value, component.label, component.custom_errors);
|
|
@@ -38,6 +65,36 @@ var validateComponent = function validateComponent(component, formData) {
|
|
|
38
65
|
error = (0, _validateEmail.default)(value, component.label);
|
|
39
66
|
}
|
|
40
67
|
|
|
68
|
+
if (!error && component.type === _models.ComponentTypes.DATE) {
|
|
69
|
+
var _validateDate = (0, _validateDate2.default)(value),
|
|
70
|
+
message = _validateDate.message,
|
|
71
|
+
propsInError = _validateDate.propsInError;
|
|
72
|
+
|
|
73
|
+
component.propsInError = propsInError;
|
|
74
|
+
error = message;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (!error && component.type === _models.ComponentTypes.TIME) {
|
|
78
|
+
var _validateTime = (0, _validateTime2.default)(value),
|
|
79
|
+
_message = _validateTime.message,
|
|
80
|
+
_propsInError = _validateTime.propsInError;
|
|
81
|
+
|
|
82
|
+
component.propsInError = _propsInError;
|
|
83
|
+
error = _message;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (!error && component.additionalValidation) {
|
|
87
|
+
error = (0, _additional.default)(component, value);
|
|
88
|
+
|
|
89
|
+
if (component.type === _models.ComponentTypes.DATE && error) {
|
|
90
|
+
component.propsInError = {
|
|
91
|
+
day: true,
|
|
92
|
+
month: true,
|
|
93
|
+
year: true
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
41
98
|
component.error = error;
|
|
42
99
|
}
|
|
43
100
|
|
|
@@ -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,95 @@ 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
|
+
});
|
|
163
|
+
describe('when the component is a container', function () {
|
|
164
|
+
it('should return an empty array when the container has an undefined components array', function () {
|
|
165
|
+
var ID = 'container';
|
|
166
|
+
var LABEL = 'field';
|
|
167
|
+
var CONTAINER = setup(ID, _models.ComponentTypes.CONTAINER, LABEL, false);
|
|
168
|
+
expect((0, _validateComponent.default)(CONTAINER, null)).toEqual([]);
|
|
169
|
+
});
|
|
170
|
+
it('should return an empty array when the container has no components', function () {
|
|
171
|
+
var ID = 'container';
|
|
172
|
+
var LABEL = 'field';
|
|
173
|
+
var CONTAINER = setup(ID, _models.ComponentTypes.CONTAINER, LABEL, false);
|
|
174
|
+
CONTAINER.components = [];
|
|
175
|
+
expect((0, _validateComponent.default)(CONTAINER, null)).toEqual([]);
|
|
176
|
+
});
|
|
177
|
+
it('should return an empty array when the container has only valid components', function () {
|
|
178
|
+
var EMAIL_ID = 'email';
|
|
179
|
+
var EMAIL_LABEL = 'Email';
|
|
180
|
+
var EMAIL = setup(EMAIL_ID, _models.ComponentTypes.EMAIL, EMAIL_LABEL, false);
|
|
181
|
+
var ID = 'container';
|
|
182
|
+
var LABEL = 'field';
|
|
183
|
+
var CONTAINER = setup(ID, _models.ComponentTypes.CONTAINER, LABEL, false);
|
|
184
|
+
CONTAINER.components = [EMAIL];
|
|
185
|
+
|
|
186
|
+
var DATA = _defineProperty({}, ID, _defineProperty({}, EMAIL_ID, 'alpha.bravo@digital.homeoffice.gov.uk'));
|
|
187
|
+
|
|
188
|
+
expect((0, _validateComponent.default)(CONTAINER, DATA)).toEqual([]);
|
|
189
|
+
});
|
|
190
|
+
it('should return an array containing an error when the container has an invalid component', function () {
|
|
191
|
+
var EMAIL_ID = 'email';
|
|
192
|
+
var EMAIL_LABEL = 'Email';
|
|
193
|
+
var EMAIL = setup(EMAIL_ID, _models.ComponentTypes.EMAIL, EMAIL_LABEL, false);
|
|
194
|
+
var ID = 'container';
|
|
195
|
+
var LABEL = 'field';
|
|
196
|
+
var CONTAINER = setup(ID, _models.ComponentTypes.CONTAINER, LABEL, false);
|
|
197
|
+
CONTAINER.components = [EMAIL];
|
|
198
|
+
|
|
199
|
+
var DATA = _defineProperty({}, ID, _defineProperty({}, EMAIL_ID, 'alpha.bravo@digital.homeoffice.com'));
|
|
200
|
+
|
|
201
|
+
expect((0, _validateComponent.default)(CONTAINER, DATA)).toEqual([{
|
|
202
|
+
id: EMAIL_ID,
|
|
203
|
+
error: "Enter ".concat(EMAIL_LABEL.toLowerCase(), " in the correct format, like jane.doe@homeoffice.gov.uk")
|
|
204
|
+
}]);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
117
207
|
});
|
|
118
208
|
});
|
|
119
209
|
});
|
|
@@ -0,0 +1,155 @@
|
|
|
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
|
+
*
|
|
55
|
+
* Note that an empty string is not considered invalid. You should use
|
|
56
|
+
* validateRequired (Validate.required) for that sort of validation.
|
|
57
|
+
*
|
|
58
|
+
* @param {string} date date as a string
|
|
59
|
+
* @returns an object with an error message and instructions for which parts of the date are in error
|
|
60
|
+
* or undefined for both if the date is valid
|
|
61
|
+
*/
|
|
62
|
+
var validateDate = function validateDate(date) {
|
|
63
|
+
if (!date) {
|
|
64
|
+
return {
|
|
65
|
+
message: undefined,
|
|
66
|
+
propsInError: undefined
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
var formattedDate = (0, _utils.formatString)(date);
|
|
71
|
+
|
|
72
|
+
var _formattedDate$split = formattedDate.split('-'),
|
|
73
|
+
_formattedDate$split2 = _slicedToArray(_formattedDate$split, 3),
|
|
74
|
+
day = _formattedDate$split2[0],
|
|
75
|
+
month = _formattedDate$split2[1],
|
|
76
|
+
year = _formattedDate$split2[2];
|
|
77
|
+
|
|
78
|
+
var intDay = parseInt(day, 10);
|
|
79
|
+
var intMonth = parseInt(month, 10);
|
|
80
|
+
|
|
81
|
+
if (year.length === 0) {
|
|
82
|
+
return {
|
|
83
|
+
message: 'Date must include a year',
|
|
84
|
+
propsInError: {
|
|
85
|
+
year: true
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (year.length !== 4) {
|
|
91
|
+
return {
|
|
92
|
+
message: 'Year must be 4 numbers',
|
|
93
|
+
propsInError: {
|
|
94
|
+
year: true
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (month.length === 0) {
|
|
100
|
+
return {
|
|
101
|
+
message: 'Date must include a month',
|
|
102
|
+
propsInError: {
|
|
103
|
+
month: true
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (intMonth > 12 || intMonth < 1) {
|
|
109
|
+
return {
|
|
110
|
+
message: 'Month must be between 1 and 12',
|
|
111
|
+
propsInError: {
|
|
112
|
+
month: true
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (day.length === 0) {
|
|
118
|
+
return {
|
|
119
|
+
message: 'Date must include a day',
|
|
120
|
+
propsInError: {
|
|
121
|
+
day: true
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
var maxDays = maxMonthDays(month, year);
|
|
127
|
+
|
|
128
|
+
if (intDay > maxDays || intDay < 1) {
|
|
129
|
+
return {
|
|
130
|
+
message: "Day must be between 1 and ".concat(maxDays),
|
|
131
|
+
propsInError: {
|
|
132
|
+
day: true
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if ((0, _dayjs.default)(formattedDate, _utils.DATE_FORMAT).format(_utils.DATE_FORMAT) !== formattedDate) {
|
|
138
|
+
return {
|
|
139
|
+
message: 'Enter a valid date',
|
|
140
|
+
propsInError: {
|
|
141
|
+
day: true,
|
|
142
|
+
month: true,
|
|
143
|
+
year: true
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
message: undefined,
|
|
150
|
+
propsInError: undefined
|
|
151
|
+
};
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
var _default = validateDate;
|
|
155
|
+
exports.default = _default;
|
|
@@ -0,0 +1,118 @@
|
|
|
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 no error when the value is an empty string', function () {
|
|
15
|
+
expect((0, _validateDate.default)('', 'DD-MM-YYYY')).toEqual({
|
|
16
|
+
message: undefined,
|
|
17
|
+
propsInError: undefined
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
test('should return undefined if a date string is valid', function () {
|
|
21
|
+
var output = (0, _validateDate.default)('28-02-2024', 'DD-MM-YYYY');
|
|
22
|
+
expect(output).toEqual({
|
|
23
|
+
message: undefined,
|
|
24
|
+
propsInError: undefined
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
test('should return undefined to a correct leap year date', function () {
|
|
28
|
+
var output = (0, _validateDate.default)('29-02-2024', 'DD-MM-YYYY');
|
|
29
|
+
expect(output).toEqual({
|
|
30
|
+
message: undefined,
|
|
31
|
+
propsInError: undefined
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
test('should validate false if NOT a leap year & 29 Feb is entered', function () {
|
|
35
|
+
var output = (0, _validateDate.default)('29-02-2023', 'DD-MM-YYYY');
|
|
36
|
+
expect(output).toEqual({
|
|
37
|
+
message: 'Day must be between 1 and 28',
|
|
38
|
+
propsInError: {
|
|
39
|
+
day: true
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
test('should return an error if no year is given', function () {
|
|
44
|
+
var output = (0, _validateDate.default)('29-02-', 'DD-MM-YYYY');
|
|
45
|
+
expect(output).toEqual({
|
|
46
|
+
message: 'Date must include a year',
|
|
47
|
+
propsInError: {
|
|
48
|
+
year: true
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
test('should return an error if the year contains less than 4 numbers', function () {
|
|
53
|
+
var output = (0, _validateDate.default)('29-02-20', 'DD-MM-YYYY');
|
|
54
|
+
expect(output).toEqual({
|
|
55
|
+
message: 'Year must be 4 numbers',
|
|
56
|
+
propsInError: {
|
|
57
|
+
year: true
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
test('should return an error if the year contains more than 4 numbers', function () {
|
|
62
|
+
var output = (0, _validateDate.default)('29-02-20202', 'DD-MM-YYYY');
|
|
63
|
+
expect(output).toEqual({
|
|
64
|
+
message: 'Year must be 4 numbers',
|
|
65
|
+
propsInError: {
|
|
66
|
+
year: true
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
test('should return an error if no month is given', function () {
|
|
71
|
+
var output = (0, _validateDate.default)('29--2020', 'DD-MM-YYYY');
|
|
72
|
+
expect(output).toEqual({
|
|
73
|
+
message: 'Date must include a month',
|
|
74
|
+
propsInError: {
|
|
75
|
+
month: true
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
test('should return an error if the month is not between 1 and 12', function () {
|
|
80
|
+
var output = (0, _validateDate.default)('29-14-2020', 'DD-MM-YYYY');
|
|
81
|
+
expect(output).toEqual({
|
|
82
|
+
message: 'Month must be between 1 and 12',
|
|
83
|
+
propsInError: {
|
|
84
|
+
month: true
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
test('should return an error if no day is given', function () {
|
|
89
|
+
var output = (0, _validateDate.default)('-03-2020', 'DD-MM-YYYY');
|
|
90
|
+
expect(output).toEqual({
|
|
91
|
+
message: 'Date must include a day',
|
|
92
|
+
propsInError: {
|
|
93
|
+
day: true
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
test('should return an error if the day is not between 1 and 31', function () {
|
|
98
|
+
var output = (0, _validateDate.default)('45-12-2020', 'DD-MM-YYYY');
|
|
99
|
+
expect(output).toEqual({
|
|
100
|
+
message: 'Day must be between 1 and 31',
|
|
101
|
+
propsInError: {
|
|
102
|
+
day: true
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
test('should correctly identify the maximum numbers of days in a given month and year', function () {
|
|
107
|
+
var max = (0, _validateDate.maxMonthDays)('02', '2024');
|
|
108
|
+
expect(max).toEqual(29);
|
|
109
|
+
max = (0, _validateDate.maxMonthDays)('02', '2023');
|
|
110
|
+
expect(max).toEqual(28);
|
|
111
|
+
max = (0, _validateDate.maxMonthDays)('06', '2023');
|
|
112
|
+
expect(max).toEqual(30);
|
|
113
|
+
max = (0, _validateDate.maxMonthDays)('07', '2023');
|
|
114
|
+
expect(max).toEqual(31);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
});
|
|
@@ -18,17 +18,15 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
18
18
|
* @returns An array containing all of the errors.
|
|
19
19
|
*/
|
|
20
20
|
var validatePage = function validatePage(components, formData) {
|
|
21
|
-
var errors = [];
|
|
22
|
-
|
|
23
21
|
if (Array.isArray(components)) {
|
|
24
|
-
components.
|
|
25
|
-
errors.
|
|
26
|
-
})
|
|
22
|
+
return components.reduce(function (errors, component) {
|
|
23
|
+
return errors.concat((0, _validateComponent.default)(component, formData));
|
|
24
|
+
}, []).filter(function (e) {
|
|
25
|
+
return !!e;
|
|
26
|
+
}).flat();
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
return
|
|
30
|
-
return !!e;
|
|
31
|
-
});
|
|
29
|
+
return [];
|
|
32
30
|
};
|
|
33
31
|
|
|
34
32
|
var _default = validatePage;
|
|
@@ -0,0 +1,95 @@
|
|
|
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
|
+
/**
|
|
23
|
+
* Checks if a time passed is a valid time.
|
|
24
|
+
* This will validate for missing components, invalid hour or minute components.
|
|
25
|
+
* EXAMPLE USE : const { message, propsInError } = validateTime('15:30')
|
|
26
|
+
*
|
|
27
|
+
* Note that an empty string is not considered invalid. You should use
|
|
28
|
+
* validateRequired (Validate.required) for that sort of validation.
|
|
29
|
+
*
|
|
30
|
+
* @param {string} time time as a string
|
|
31
|
+
* @returns an object with an error message and instructions for which parts of the time are in error
|
|
32
|
+
* or undefined for both if the time is valid
|
|
33
|
+
*/
|
|
34
|
+
var validateTime = function validateTime(time) {
|
|
35
|
+
if (!time) {
|
|
36
|
+
return {
|
|
37
|
+
message: undefined,
|
|
38
|
+
propsInError: undefined
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
var formattedDate = (0, _utils.formattedTime)(time);
|
|
43
|
+
|
|
44
|
+
var _formattedDate$split = formattedDate.split(':'),
|
|
45
|
+
_formattedDate$split2 = _slicedToArray(_formattedDate$split, 2),
|
|
46
|
+
hour = _formattedDate$split2[0],
|
|
47
|
+
minute = _formattedDate$split2[1];
|
|
48
|
+
|
|
49
|
+
var intHour = parseInt(hour, 10);
|
|
50
|
+
var intMinute = parseInt(minute, 10);
|
|
51
|
+
|
|
52
|
+
if (hour.length === 0) {
|
|
53
|
+
return {
|
|
54
|
+
message: 'Time must include a hour',
|
|
55
|
+
propsInError: {
|
|
56
|
+
hour: true
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (intHour > 23 || intHour < 0) {
|
|
62
|
+
return {
|
|
63
|
+
message: 'Hour must be between 0 and 23',
|
|
64
|
+
propsInError: {
|
|
65
|
+
hour: true
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (minute.length === 0) {
|
|
71
|
+
return {
|
|
72
|
+
message: 'Time must include a minute',
|
|
73
|
+
propsInError: {
|
|
74
|
+
minute: true
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (intMinute > 59 || intMinute < 0) {
|
|
80
|
+
return {
|
|
81
|
+
message: 'Minute must be between 0 and 59',
|
|
82
|
+
propsInError: {
|
|
83
|
+
minute: true
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
message: undefined,
|
|
90
|
+
propsInError: undefined
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
var _default = validateTime;
|
|
95
|
+
exports.default = _default;
|
|
@@ -0,0 +1,61 @@
|
|
|
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 no error when the value is an empty string', function () {
|
|
11
|
+
expect((0, _validateTime.default)('')).toEqual({
|
|
12
|
+
message: undefined,
|
|
13
|
+
propsInError: undefined
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
test('should return undefined if a time string is valid', function () {
|
|
17
|
+
var output = (0, _validateTime.default)('14:30');
|
|
18
|
+
expect(output).toEqual({
|
|
19
|
+
message: undefined,
|
|
20
|
+
propsInError: undefined
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
test('should return an error if no hour is given', function () {
|
|
24
|
+
var output = (0, _validateTime.default)(':30');
|
|
25
|
+
expect(output).toEqual({
|
|
26
|
+
message: 'Time must include a hour',
|
|
27
|
+
propsInError: {
|
|
28
|
+
hour: true
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
test('should return an error if the hour is not between 0 and 23', function () {
|
|
33
|
+
var output = (0, _validateTime.default)('35:30');
|
|
34
|
+
expect(output).toEqual({
|
|
35
|
+
message: 'Hour must be between 0 and 23',
|
|
36
|
+
propsInError: {
|
|
37
|
+
hour: true
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
test('should return an error if no minute is given', function () {
|
|
42
|
+
var output = (0, _validateTime.default)('14:');
|
|
43
|
+
expect(output).toEqual({
|
|
44
|
+
message: 'Time must include a minute',
|
|
45
|
+
propsInError: {
|
|
46
|
+
minute: true
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
test('should return an error if the minute is not between 0 and 59', function () {
|
|
51
|
+
var output = (0, _validateTime.default)('14:75');
|
|
52
|
+
expect(output).toEqual({
|
|
53
|
+
message: 'Minute must be between 0 and 59',
|
|
54
|
+
propsInError: {
|
|
55
|
+
minute: true
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|