@ukhomeoffice/cop-react-form-renderer 2.0.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.
- package/LICENSE +21 -0
- package/README.md +12 -0
- package/dist/components/CheckYourAnswers/CheckYourAnswers.js +16 -5
- package/dist/components/CheckYourAnswers/CheckYourAnswers.stories.mdx +199 -79
- package/dist/components/CheckYourAnswers/CheckYourAnswers.test.js +44 -0
- package/dist/components/FormComponent/FormComponent.js +6 -1
- package/dist/components/FormComponent/FormComponent.test.js +67 -0
- package/dist/components/FormRenderer/FormRenderer.js +122 -11
- package/dist/components/FormRenderer/FormRenderer.stories.mdx +115 -49
- package/dist/components/FormRenderer/FormRenderer.test.js +275 -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/SummaryList/SummaryList.js +7 -4
- package/dist/components/SummaryList/SummaryList.scss +10 -2
- package/dist/components/SummaryList/SummaryList.stories.mdx +64 -10
- package/dist/components/SummaryList/SummaryList.test.js +54 -0
- 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/useHooks.js +5 -1
- package/dist/index.js +8 -0
- package/dist/json/taskList.json +228 -0
- package/dist/json/userProfile.data.json +2 -1
- package/dist/models/ComponentTypes.js +3 -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/Component/cleanAttributes.js +1 -1
- package/dist/utils/Component/getComponent.js +26 -0
- package/dist/utils/Component/isEditable.js +1 -1
- package/dist/utils/FormPage/getPageActions.js +6 -0
- package/dist/utils/Hub/getFormHub.js +4 -0
- package/dist/utils/Hub/getFormHub.test.js +10 -2
- 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/validateComponent.js +38 -1
- package/dist/utils/Validate/validateComponent.test.js +48 -2
- package/dist/utils/Validate/validateDate.js +144 -0
- package/dist/utils/Validate/validateDate.test.js +112 -0
- package/dist/utils/Validate/validateRequired.js +11 -0
- package/dist/utils/Validate/validateRequired.test.js +12 -0
- package/dist/utils/Validate/validateTime.js +76 -0
- package/dist/utils/Validate/validateTime.test.js +55 -0
- package/package.json +3 -2
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
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 _utils = require("./utils");
|
|
15
|
+
|
|
16
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
|
+
|
|
18
|
+
// Global imports
|
|
19
|
+
//Local imports
|
|
20
|
+
_dayjs.default.extend(_customParseFormat.default);
|
|
21
|
+
|
|
22
|
+
_dayjs.default.extend(_isToday.default);
|
|
23
|
+
/**
|
|
24
|
+
* Function returns true if the date parameter is a date before today, else returns false
|
|
25
|
+
* dayjs counts today as before today so also cover same day inputs
|
|
26
|
+
* @param {string} date - date string e.g. '03-12-2021'
|
|
27
|
+
* @param {boolean} config.todayAllowed - true if today should be a valid date
|
|
28
|
+
* @returns true if date is before current date
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
var mustBeInThePast = function mustBeInThePast(date, config) {
|
|
33
|
+
var beforeToday = (0, _dayjs.default)((0, _utils.formatString)(date), _utils.DATE_FORMAT).isBefore((0, _dayjs.default)());
|
|
34
|
+
var isToday = (0, _dayjs.default)((0, _utils.formatString)(date), _utils.DATE_FORMAT).isToday(); //dayjs classifies the current day as in the past so exclude it here
|
|
35
|
+
|
|
36
|
+
if (beforeToday && !isToday) {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return isToday && !!(config !== null && config !== void 0 && config.todayAllowed);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
var _default = mustBeInThePast;
|
|
44
|
+
exports.default = _default;
|
|
@@ -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,15 +35,46 @@ 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
|
-
error = (0, _validateRequired.default)(value, component.label);
|
|
41
|
+
error = (0, _validateRequired.default)(value, component.label, component.custom_errors);
|
|
35
42
|
}
|
|
36
43
|
|
|
37
44
|
if (!error && component.type === _models.ComponentTypes.EMAIL) {
|
|
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
|
+
});
|
|
@@ -14,6 +14,7 @@ exports.default = void 0;
|
|
|
14
14
|
*/
|
|
15
15
|
var validateRequired = function validateRequired(value) {
|
|
16
16
|
var label = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
|
17
|
+
var customErrors = arguments.length > 2 ? arguments[2] : undefined;
|
|
17
18
|
var hasValue = false;
|
|
18
19
|
|
|
19
20
|
if (!!value || value === false || value === 0) {
|
|
@@ -27,6 +28,16 @@ var validateRequired = function validateRequired(value) {
|
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
if (!hasValue) {
|
|
31
|
+
if (Array.isArray(customErrors)) {
|
|
32
|
+
var result = customErrors.filter(function (error) {
|
|
33
|
+
return error.type === 'required';
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
if (result && result.length > 0 && result[0].message) {
|
|
37
|
+
return result[0].message;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
30
41
|
var name = label ? label : 'Field';
|
|
31
42
|
return "".concat(name, " is required");
|
|
32
43
|
}
|
|
@@ -45,6 +45,18 @@ describe('utils', function () {
|
|
|
45
45
|
it('should use a default label when none is specified', function () {
|
|
46
46
|
expect((0, _validateRequired.default)(undefined, undefined)).toEqual('Field is required');
|
|
47
47
|
});
|
|
48
|
+
it('should use a custom error when one is provided', function () {
|
|
49
|
+
expect((0, _validateRequired.default)(undefined, undefined, [{
|
|
50
|
+
type: 'required',
|
|
51
|
+
message: 'custom error message'
|
|
52
|
+
}])).toEqual('custom error message');
|
|
53
|
+
});
|
|
54
|
+
it('should ignore a custom error when not of type required', function () {
|
|
55
|
+
expect((0, _validateRequired.default)(undefined, undefined, [{
|
|
56
|
+
type: 'genericError',
|
|
57
|
+
message: 'generic error message'
|
|
58
|
+
}])).toEqual('Field is required');
|
|
59
|
+
});
|
|
48
60
|
});
|
|
49
61
|
});
|
|
50
62
|
});
|
|
@@ -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;
|