@ukhomeoffice/cop-react-form-renderer 4.16.3 → 4.17.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.
|
@@ -9,6 +9,8 @@ var _mustBeAfter = _interopRequireDefault(require("./mustBeAfter"));
|
|
|
9
9
|
|
|
10
10
|
var _mustBeBefore = _interopRequireDefault(require("./mustBeBefore"));
|
|
11
11
|
|
|
12
|
+
var _mustBeEarlierDateTime = _interopRequireDefault(require("./mustBeEarlierDateTime"));
|
|
13
|
+
|
|
12
14
|
var _mustBeInThePast = _interopRequireDefault(require("./mustBeInThePast"));
|
|
13
15
|
|
|
14
16
|
var _mustBeInTheFuture = _interopRequireDefault(require("./mustBeInTheFuture"));
|
|
@@ -29,6 +31,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
29
31
|
var functions = {
|
|
30
32
|
mustBeAfter: _mustBeAfter.default,
|
|
31
33
|
mustBeBefore: _mustBeBefore.default,
|
|
34
|
+
mustBeEarlierDateTime: _mustBeEarlierDateTime.default,
|
|
32
35
|
mustBeInThePast: _mustBeInThePast.default,
|
|
33
36
|
mustBeInTheFuture: _mustBeInTheFuture.default,
|
|
34
37
|
mustBeLongerThan: _mustBeLongerThan.default,
|
|
@@ -0,0 +1,67 @@
|
|
|
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 _validateComponent = _interopRequireDefault(require("../validateComponent"));
|
|
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
|
+
* Checks whether the combination of a given date & time input is in the past.
|
|
23
|
+
* NOTE: This additional validation will only work for containers. They get the
|
|
24
|
+
* date & time from their children - if there's an error then it can be displayed
|
|
25
|
+
* by wrapping both components.
|
|
26
|
+
*
|
|
27
|
+
* @param {object} data A page's formData.
|
|
28
|
+
* @param {object} config An additionalValidation config obj.
|
|
29
|
+
* @param {object} container A config obj for a container.
|
|
30
|
+
* @returns True if the date & time are in the past, or there are errors with
|
|
31
|
+
* the individual components. False if the date & time is in the future, or
|
|
32
|
+
* the date/time component can't be found.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
var mustBeEarlierDateTime = function mustBeEarlierDateTime(data, config, container) {
|
|
37
|
+
var _container$components, _container$components2, _data$container$field, _data$container$field2;
|
|
38
|
+
|
|
39
|
+
var date = (_container$components = container.components) === null || _container$components === void 0 ? void 0 : _container$components.find(function (c) {
|
|
40
|
+
return c.fieldId === config.date;
|
|
41
|
+
});
|
|
42
|
+
var time = (_container$components2 = container.components) === null || _container$components2 === void 0 ? void 0 : _container$components2.find(function (c) {
|
|
43
|
+
return c.fieldId === config.time;
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
if (!date || !time || !data) {
|
|
47
|
+
return false;
|
|
48
|
+
} // Here we return true if there are errors with the date & time components
|
|
49
|
+
// individually. This is to allow them to process their errors, and to prevent
|
|
50
|
+
// validation from stopping at the container level.
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
if ((0, _validateComponent.default)(date, data[container.fieldId], data[container.fieldId])) {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if ((0, _validateComponent.default)(time, data[container.fieldId], data[container.fieldId])) {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
var dateStr = (0, _utils.formatString)((_data$container$field = data[container.fieldId]) === null || _data$container$field === void 0 ? void 0 : _data$container$field[config.date]);
|
|
62
|
+
var timeStr = (0, _utils.formattedTime)((_data$container$field2 = data[container.fieldId]) === null || _data$container$field2 === void 0 ? void 0 : _data$container$field2[config.time]);
|
|
63
|
+
return (0, _dayjs.default)("".concat(dateStr, " ").concat(timeStr), "".concat(_utils.DATE_FORMAT, " HH:mm")).isBefore((0, _dayjs.default)());
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
var _default = mustBeEarlierDateTime;
|
|
67
|
+
exports.default = _default;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _mustBeEarlierDateTime = _interopRequireDefault(require("./mustBeEarlierDateTime"));
|
|
4
|
+
|
|
5
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
6
|
+
|
|
7
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
8
|
+
|
|
9
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
10
|
+
|
|
11
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
12
|
+
|
|
13
|
+
describe('utils.Validate.additional.mustBeEarlierDateTime', function () {
|
|
14
|
+
var DATA = {
|
|
15
|
+
testContainer: {
|
|
16
|
+
testDate: '01-01-2000',
|
|
17
|
+
testTime: '00:00'
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
var CONFIG = {
|
|
21
|
+
function: 'mustBeEarlierDateTime',
|
|
22
|
+
date: 'testDate',
|
|
23
|
+
time: 'testTime',
|
|
24
|
+
message: 'Date and time must be in the past'
|
|
25
|
+
};
|
|
26
|
+
var CONTAINER = {
|
|
27
|
+
fieldId: 'testContainer',
|
|
28
|
+
type: 'container',
|
|
29
|
+
components: [{
|
|
30
|
+
fieldId: 'testDate',
|
|
31
|
+
type: 'date',
|
|
32
|
+
required: true
|
|
33
|
+
}, {
|
|
34
|
+
fieldId: 'testTime',
|
|
35
|
+
type: 'time',
|
|
36
|
+
required: true
|
|
37
|
+
}]
|
|
38
|
+
};
|
|
39
|
+
test('should return false if the date component is missing', function () {
|
|
40
|
+
var EDITED_CONFIG = _objectSpread(_objectSpread({}, CONFIG), {}, {
|
|
41
|
+
date: 'notARealComponent'
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
var result = (0, _mustBeEarlierDateTime.default)(DATA, EDITED_CONFIG, CONTAINER);
|
|
45
|
+
expect(result).toEqual(false);
|
|
46
|
+
});
|
|
47
|
+
test('should return false if the time component is missing', function () {
|
|
48
|
+
var EDITED_CONFIG = _objectSpread(_objectSpread({}, CONFIG), {}, {
|
|
49
|
+
time: 'notARealComponent'
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
var result = (0, _mustBeEarlierDateTime.default)(DATA, EDITED_CONFIG, CONTAINER);
|
|
53
|
+
expect(result).toEqual(false);
|
|
54
|
+
});
|
|
55
|
+
test('should return false if data is null', function () {
|
|
56
|
+
var result = (0, _mustBeEarlierDateTime.default)(null, CONFIG, CONTAINER);
|
|
57
|
+
expect(result).toEqual(false);
|
|
58
|
+
});
|
|
59
|
+
test('should return true if date value is missing', function () {
|
|
60
|
+
var EDITED_DATA = {
|
|
61
|
+
testContainer: {
|
|
62
|
+
testTime: '00:00'
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
var result = (0, _mustBeEarlierDateTime.default)(EDITED_DATA, CONFIG, CONTAINER);
|
|
66
|
+
expect(result).toEqual(true);
|
|
67
|
+
});
|
|
68
|
+
test('should return true if time value is missing', function () {
|
|
69
|
+
var EDITED_DATA = {
|
|
70
|
+
testContainer: {
|
|
71
|
+
testDate: '01-01-2000'
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
var result = (0, _mustBeEarlierDateTime.default)(EDITED_DATA, CONFIG, CONTAINER);
|
|
75
|
+
expect(result).toEqual(true);
|
|
76
|
+
});
|
|
77
|
+
test('should return true if date has errors', function () {
|
|
78
|
+
var EDITED_DATA = {
|
|
79
|
+
testContainer: {
|
|
80
|
+
testDate: '011-011-200000',
|
|
81
|
+
testTime: '00:00'
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
var result = (0, _mustBeEarlierDateTime.default)(EDITED_DATA, CONFIG, CONTAINER);
|
|
85
|
+
expect(result).toEqual(true);
|
|
86
|
+
});
|
|
87
|
+
test('should return true if time has errors', function () {
|
|
88
|
+
var EDITED_DATA = {
|
|
89
|
+
testContainer: {
|
|
90
|
+
testDate: '01-01-2000',
|
|
91
|
+
testTime: '45:78'
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
var result = (0, _mustBeEarlierDateTime.default)(EDITED_DATA, CONFIG, CONTAINER);
|
|
95
|
+
expect(result).toEqual(true);
|
|
96
|
+
});
|
|
97
|
+
test('should return true if date & time are in the past', function () {
|
|
98
|
+
var result = (0, _mustBeEarlierDateTime.default)(DATA, CONFIG, CONTAINER);
|
|
99
|
+
expect(result).toEqual(true);
|
|
100
|
+
});
|
|
101
|
+
test('should return false if date & time are not in the past', function () {
|
|
102
|
+
var DATA = {
|
|
103
|
+
testContainer: {
|
|
104
|
+
testDate: '01-01-5000',
|
|
105
|
+
testTime: '00:00'
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
var result = (0, _mustBeEarlierDateTime.default)(DATA, CONFIG, CONTAINER);
|
|
109
|
+
expect(result).toEqual(false);
|
|
110
|
+
});
|
|
111
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ukhomeoffice/cop-react-form-renderer",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.17.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"scripts": {
|
|
6
6
|
"clean": "rimraf dist",
|
|
@@ -16,7 +16,7 @@
|
|
|
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": "2.2.
|
|
19
|
+
"@ukhomeoffice/cop-react-components": "2.2.5",
|
|
20
20
|
"axios": "^0.23.0",
|
|
21
21
|
"dayjs": "^1.11.0",
|
|
22
22
|
"govuk-frontend": "^4.3.1",
|