@ukhomeoffice/cop-react-form-renderer 2.8.5 → 2.10.1
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/CheckYourAnswers/CheckYourAnswers.js +8 -12
- package/dist/components/CheckYourAnswers/CheckYourAnswers.test.js +10 -8
- package/dist/components/FormComponent/Collection.js +244 -0
- package/dist/components/FormComponent/Collection.scss +23 -0
- package/dist/components/FormComponent/Container.js +110 -0
- package/dist/components/FormComponent/Container.test.js +26 -26
- package/dist/components/FormComponent/FormComponent.js +65 -128
- package/dist/components/FormComponent/FormComponent.test.js +19 -19
- package/dist/components/FormComponent/helpers/getComponentError.js +31 -0
- package/dist/components/FormComponent/helpers/getComponentError.test.js +52 -0
- package/dist/components/FormComponent/helpers/index.js +15 -0
- package/dist/components/FormPage/FormPage.js +7 -11
- package/dist/components/FormPage/FormPage.test.js +12 -76
- package/dist/components/FormRenderer/FormRenderer.js +103 -44
- package/dist/components/FormRenderer/helpers/canActionProceed.js +3 -12
- package/dist/components/FormRenderer/helpers/canActionProceed.test.js +41 -65
- package/dist/components/FormRenderer/helpers/canCYASubmit.js +3 -24
- package/dist/components/FormRenderer/helpers/canCYASubmit.test.js +72 -145
- package/dist/components/SummaryList/GroupAction.js +6 -2
- package/dist/components/SummaryList/SummaryList.js +33 -18
- package/dist/components/SummaryList/SummaryList.scss +12 -8
- package/dist/components/SummaryList/SummaryList.test.js +94 -27
- package/dist/components/SummaryList/SummaryListRow.js +51 -0
- package/dist/components/SummaryList/SummaryListTitleRow.js +30 -0
- package/dist/context/ValidationContext/ValidationContext.js +122 -0
- package/dist/context/ValidationContext/ValidationContext.test.js +98 -0
- package/dist/context/ValidationContext/index.js +23 -0
- package/dist/context/index.js +15 -0
- package/dist/hooks/index.js +8 -0
- package/dist/hooks/useValidation.js +19 -0
- package/dist/index.js +11 -5
- package/dist/models/CollectionLabels.js +14 -0
- package/dist/models/ComponentTypes.js +2 -0
- package/dist/models/index.js +8 -0
- package/dist/setupTests.js +32 -0
- package/dist/utils/CheckYourAnswers/getCYARow.js +14 -0
- package/dist/utils/CheckYourAnswers/getCYARow.test.js +34 -11
- package/dist/utils/CheckYourAnswers/getCYARowsForCollection.js +89 -0
- package/dist/utils/CheckYourAnswers/getCYARowsForCollection.test.js +194 -0
- package/dist/utils/CheckYourAnswers/getCYARowsForContainer.test.js +9 -13
- package/dist/utils/CheckYourAnswers/getCYARowsForPage.js +11 -4
- package/dist/utils/CheckYourAnswers/getCYARowsForPage.test.js +58 -14
- package/dist/utils/Component/cleanAttributes.js +1 -1
- package/dist/utils/Component/getComponent.js +36 -3
- package/dist/utils/Component/getComponentTests/getComponent.nested.test.js +92 -0
- package/dist/utils/Component/getDefaultValue.js +25 -0
- package/dist/utils/Component/getDefaultValue.test.js +53 -0
- package/dist/utils/Component/index.js +4 -1
- package/dist/utils/Data/getSourceData.js +38 -0
- package/dist/utils/Data/getSourceData.test.js +16 -0
- package/dist/utils/Meta/constants.js +10 -0
- package/dist/utils/Meta/documents/getDocuments.js +18 -0
- package/dist/utils/Meta/documents/getDocuments.test.js +43 -0
- package/dist/utils/Meta/documents/index.js +23 -0
- package/dist/utils/Meta/documents/setDocumentForField.js +36 -0
- package/dist/utils/Meta/documents/setDocumentForField.test.js +49 -0
- package/dist/utils/Meta/index.js +21 -0
- package/dist/utils/Validate/validateCollection.js +42 -0
- package/dist/utils/Validate/validateCollection.test.js +74 -0
- package/dist/utils/Validate/validateComponent.js +67 -48
- package/dist/utils/Validate/validateComponent.test.js +247 -188
- package/dist/utils/Validate/validateContainer.js +29 -0
- package/dist/utils/Validate/validateContainer.test.js +68 -0
- package/dist/utils/index.js +4 -1
- package/package.json +3 -3
|
@@ -11,97 +11,116 @@ var _showComponent = _interopRequireDefault(require("../Component/showComponent"
|
|
|
11
11
|
|
|
12
12
|
var _additional = _interopRequireDefault(require("./additional"));
|
|
13
13
|
|
|
14
|
-
var
|
|
14
|
+
var _validateCollection = _interopRequireDefault(require("./validateCollection"));
|
|
15
|
+
|
|
16
|
+
var _validateContainer = _interopRequireDefault(require("./validateContainer"));
|
|
17
|
+
|
|
18
|
+
var _validateDate = _interopRequireDefault(require("./validateDate"));
|
|
15
19
|
|
|
16
20
|
var _validateEmail = _interopRequireDefault(require("./validateEmail"));
|
|
17
21
|
|
|
18
22
|
var _validateRequired = _interopRequireDefault(require("./validateRequired"));
|
|
19
23
|
|
|
20
|
-
var
|
|
24
|
+
var _validateTime = _interopRequireDefault(require("./validateTime"));
|
|
21
25
|
|
|
22
26
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
23
27
|
|
|
24
28
|
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); }
|
|
25
29
|
|
|
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
|
-
};
|
|
40
30
|
/**
|
|
41
31
|
* Validates a single component.
|
|
42
32
|
* @param {object} component The component to validate.
|
|
43
33
|
* @param {object} formData The data to use that holds this component's value.
|
|
44
34
|
* @returns The first encountered error with the component.
|
|
45
35
|
*/
|
|
36
|
+
var validateComponent = function validateComponent(component, formData) {
|
|
37
|
+
var _component$data$optio;
|
|
46
38
|
|
|
39
|
+
if (!(0, _showComponent.default)(component, formData)) {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
47
42
|
|
|
48
|
-
var validateComponent = function validateComponent(component, formData) {
|
|
49
43
|
var error = undefined;
|
|
44
|
+
var nestedId = undefined;
|
|
45
|
+
var propertiesInError = undefined;
|
|
50
46
|
var data = formData && _typeof(formData) === 'object' ? formData : {};
|
|
51
47
|
|
|
52
|
-
if (component
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
48
|
+
if (component.type === _models.ComponentTypes.CONTAINER) {
|
|
49
|
+
return (0, _validateContainer.default)(component, formData);
|
|
50
|
+
}
|
|
56
51
|
|
|
57
|
-
|
|
58
|
-
delete component.propsInError;
|
|
52
|
+
var value = data[component.fieldId];
|
|
59
53
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
54
|
+
if (component.required) {
|
|
55
|
+
error = (0, _validateRequired.default)(value, component.label, component.custom_errors);
|
|
56
|
+
}
|
|
63
57
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
58
|
+
if (!error) {
|
|
59
|
+
switch (component.type) {
|
|
60
|
+
case _models.ComponentTypes.COLLECTION:
|
|
61
|
+
return (0, _validateCollection.default)(component, value);
|
|
67
62
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
propsInError = _validateDate.propsInError;
|
|
63
|
+
case _models.ComponentTypes.EMAIL:
|
|
64
|
+
error = (0, _validateEmail.default)(value, component.label);
|
|
65
|
+
break;
|
|
72
66
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
67
|
+
case _models.ComponentTypes.DATE:
|
|
68
|
+
case _models.ComponentTypes.TIME:
|
|
69
|
+
var validator = component.type === _models.ComponentTypes.DATE ? _validateDate.default : _validateTime.default;
|
|
70
|
+
|
|
71
|
+
var _validator = validator(value),
|
|
72
|
+
message = _validator.message,
|
|
73
|
+
propsInError = _validator.propsInError;
|
|
74
|
+
|
|
75
|
+
propertiesInError = propsInError;
|
|
76
|
+
error = message;
|
|
77
|
+
break;
|
|
76
78
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
_propsInError = _validateTime.propsInError;
|
|
79
|
+
case _models.ComponentTypes.RADIOS:
|
|
80
|
+
(_component$data$optio = component.data.options) === null || _component$data$optio === void 0 ? void 0 : _component$data$optio.some(function (option) {
|
|
81
|
+
var nestedError;
|
|
81
82
|
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
if (option.nested && option.nested.shown) {
|
|
84
|
+
var _nestedError;
|
|
85
|
+
|
|
86
|
+
nestedError = validateComponent(option.nested, formData);
|
|
87
|
+
error = (_nestedError = nestedError) === null || _nestedError === void 0 ? void 0 : _nestedError.error;
|
|
88
|
+
|
|
89
|
+
if (nestedError) {
|
|
90
|
+
nestedId = nestedError.id;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return nestedError;
|
|
95
|
+
});
|
|
96
|
+
break;
|
|
97
|
+
|
|
98
|
+
default:
|
|
99
|
+
break;
|
|
84
100
|
}
|
|
85
101
|
|
|
86
102
|
if (!error && component.additionalValidation) {
|
|
87
103
|
error = (0, _additional.default)(component, value);
|
|
88
104
|
|
|
89
105
|
if (component.type === _models.ComponentTypes.DATE && error) {
|
|
90
|
-
|
|
106
|
+
propertiesInError = {
|
|
91
107
|
day: true,
|
|
92
108
|
month: true,
|
|
93
109
|
year: true
|
|
94
110
|
};
|
|
95
111
|
}
|
|
96
112
|
}
|
|
113
|
+
}
|
|
97
114
|
|
|
98
|
-
|
|
115
|
+
if (error) {
|
|
116
|
+
return {
|
|
117
|
+
id: nestedId || component.full_path || component.id,
|
|
118
|
+
error: error,
|
|
119
|
+
properties: propertiesInError
|
|
120
|
+
};
|
|
99
121
|
}
|
|
100
122
|
|
|
101
|
-
return
|
|
102
|
-
id: component.id,
|
|
103
|
-
error: error
|
|
104
|
-
} : undefined;
|
|
123
|
+
return undefined;
|
|
105
124
|
};
|
|
106
125
|
|
|
107
126
|
var _default = validateComponent;
|
|
@@ -8,202 +8,261 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
8
8
|
|
|
9
9
|
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; }
|
|
10
10
|
|
|
11
|
-
describe('utils', function () {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
};
|
|
23
|
-
};
|
|
11
|
+
describe('utils.Validate.Component', function () {
|
|
12
|
+
var setup = function setup(id, type, label, required, additionalValidation) {
|
|
13
|
+
return {
|
|
14
|
+
id: id,
|
|
15
|
+
fieldId: id,
|
|
16
|
+
type: type,
|
|
17
|
+
label: label,
|
|
18
|
+
required: required,
|
|
19
|
+
additionalValidation: additionalValidation
|
|
20
|
+
};
|
|
21
|
+
};
|
|
24
22
|
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
it('should return no error when the component is null', function () {
|
|
24
|
+
expect((0, _validateComponent.default)(null, {})).toBeUndefined();
|
|
25
|
+
});
|
|
26
|
+
describe('when there is no form data', function () {
|
|
27
|
+
it('should return no error when the component is not required and not an email type', function () {
|
|
28
|
+
var ID = 'field';
|
|
29
|
+
var LABEL = 'Field';
|
|
30
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, false);
|
|
31
|
+
expect((0, _validateComponent.default)(COMPONENT, null)).toBeUndefined();
|
|
32
|
+
});
|
|
33
|
+
it('should return a required error when the component is required and not an email type', function () {
|
|
34
|
+
var ID = 'field';
|
|
35
|
+
var LABEL = 'Field';
|
|
36
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, true);
|
|
37
|
+
expect((0, _validateComponent.default)(COMPONENT, null)).toEqual({
|
|
38
|
+
id: ID,
|
|
39
|
+
error: "".concat(LABEL, " is required")
|
|
27
40
|
});
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
41
|
+
});
|
|
42
|
+
it('should return a required error when the component is required and of type email', function () {
|
|
43
|
+
var ID = 'field';
|
|
44
|
+
var LABEL = 'Field';
|
|
45
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, true);
|
|
46
|
+
expect((0, _validateComponent.default)(COMPONENT, null)).toEqual({
|
|
47
|
+
id: ID,
|
|
48
|
+
error: "".concat(LABEL, " is required")
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
it('should return no error when the component is of type email but not required', function () {
|
|
52
|
+
var ID = 'field';
|
|
53
|
+
var LABEL = 'Field';
|
|
54
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, false);
|
|
55
|
+
expect((0, _validateComponent.default)(COMPONENT, null)).toBeUndefined();
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
describe('when the value is fully valid', function () {
|
|
59
|
+
var ID = 'field';
|
|
60
|
+
|
|
61
|
+
var DATA = _defineProperty({}, ID, 'alpha.bravo@digital.homeoffice.gov.uk');
|
|
62
|
+
|
|
63
|
+
it('should return no error when the component is not required and not an email type', function () {
|
|
64
|
+
var LABEL = 'Field';
|
|
65
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, false);
|
|
66
|
+
expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
|
|
67
|
+
});
|
|
68
|
+
it('should return no error when the component is required and not an email type', function () {
|
|
69
|
+
var LABEL = 'Field';
|
|
70
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, true);
|
|
71
|
+
expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
|
|
72
|
+
});
|
|
73
|
+
it('should return no error when the component is required and of type email', function () {
|
|
74
|
+
var LABEL = 'Field';
|
|
75
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, true);
|
|
76
|
+
expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
|
|
77
|
+
});
|
|
78
|
+
it('should return no error when the component is of type email but not required', function () {
|
|
79
|
+
var LABEL = 'Field';
|
|
80
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, false);
|
|
81
|
+
expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
describe('when the value is an invalid email', function () {
|
|
85
|
+
var ID = 'field';
|
|
86
|
+
|
|
87
|
+
var DATA = _defineProperty({}, ID, 'alpha.bravo@hotmail.com');
|
|
88
|
+
|
|
89
|
+
it('should return no error when the component is not required and not an email type', function () {
|
|
90
|
+
var LABEL = 'Field';
|
|
91
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, false);
|
|
92
|
+
expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
|
|
93
|
+
});
|
|
94
|
+
it('should return no error when the component is required and not an email type', function () {
|
|
95
|
+
var LABEL = 'Field';
|
|
96
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, true);
|
|
97
|
+
expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
|
|
98
|
+
});
|
|
99
|
+
it('should return no error when the component is required and of type email', function () {
|
|
100
|
+
var LABEL = 'Field';
|
|
101
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, true);
|
|
102
|
+
expect((0, _validateComponent.default)(COMPONENT, DATA)).toEqual({
|
|
103
|
+
id: ID,
|
|
104
|
+
error: "Enter ".concat(LABEL.toLowerCase(), " in the correct format, like jane.doe@homeoffice.gov.uk")
|
|
59
105
|
});
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, false);
|
|
68
|
-
expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
|
|
69
|
-
});
|
|
70
|
-
it('should return no error when the component is required and not an email type', function () {
|
|
71
|
-
var LABEL = 'Field';
|
|
72
|
-
var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, true);
|
|
73
|
-
expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
|
|
74
|
-
});
|
|
75
|
-
it('should return no error when the component is required and of type email', function () {
|
|
76
|
-
var LABEL = 'Field';
|
|
77
|
-
var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, true);
|
|
78
|
-
expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
|
|
79
|
-
});
|
|
80
|
-
it('should return no error when the component is of type email but not required', function () {
|
|
81
|
-
var LABEL = 'Field';
|
|
82
|
-
var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, false);
|
|
83
|
-
expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
|
|
84
|
-
});
|
|
106
|
+
});
|
|
107
|
+
it('should return no error when the component is of type email but not required', function () {
|
|
108
|
+
var LABEL = 'Field';
|
|
109
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, false);
|
|
110
|
+
expect((0, _validateComponent.default)(COMPONENT, DATA)).toEqual({
|
|
111
|
+
id: ID,
|
|
112
|
+
error: "Enter ".concat(LABEL.toLowerCase(), " in the correct format, like jane.doe@homeoffice.gov.uk")
|
|
85
113
|
});
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
var LABEL = 'Field';
|
|
103
|
-
var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, true);
|
|
104
|
-
expect((0, _validateComponent.default)(COMPONENT, DATA)).toEqual({
|
|
105
|
-
id: ID,
|
|
106
|
-
error: "Enter ".concat(LABEL.toLowerCase(), " in the correct format, like jane.doe@homeoffice.gov.uk")
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
it('should return no error when the component is of type email but not required', function () {
|
|
110
|
-
var LABEL = 'Field';
|
|
111
|
-
var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, false);
|
|
112
|
-
expect((0, _validateComponent.default)(COMPONENT, DATA)).toEqual({
|
|
113
|
-
id: ID,
|
|
114
|
-
error: "Enter ".concat(LABEL.toLowerCase(), " in the correct format, like jane.doe@homeoffice.gov.uk")
|
|
115
|
-
});
|
|
116
|
-
});
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
describe('when the component is a Date Input', function () {
|
|
117
|
+
var ID = 'field';
|
|
118
|
+
it('should always reject invalid dates', function () {
|
|
119
|
+
var LABEL = 'Field';
|
|
120
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.DATE, LABEL, false);
|
|
121
|
+
|
|
122
|
+
var DATA = _defineProperty({}, ID, '25-45-2033');
|
|
123
|
+
|
|
124
|
+
expect((0, _validateComponent.default)(COMPONENT, DATA)).toEqual({
|
|
125
|
+
error: 'Month must be between 1 and 12',
|
|
126
|
+
id: ID,
|
|
127
|
+
properties: {
|
|
128
|
+
month: true
|
|
129
|
+
}
|
|
117
130
|
});
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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
|
-
});
|
|
131
|
+
});
|
|
132
|
+
it('should apply optional validators when specified', function () {
|
|
133
|
+
var LABEL = 'Field';
|
|
134
|
+
|
|
135
|
+
var DATA = _defineProperty({}, ID, '25-3-3033');
|
|
136
|
+
|
|
137
|
+
var ADDITIONAL_VALIDATION = [{
|
|
138
|
+
function: 'mustBeBefore',
|
|
139
|
+
value: 3,
|
|
140
|
+
unit: 'day',
|
|
141
|
+
message: 'Date must be less than 3 days in the future'
|
|
142
|
+
}];
|
|
143
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.DATE, LABEL, false, ADDITIONAL_VALIDATION);
|
|
144
|
+
expect((0, _validateComponent.default)(COMPONENT, DATA)).toEqual({
|
|
145
|
+
error: 'Date must be less than 3 days in the future',
|
|
146
|
+
id: ID,
|
|
147
|
+
properties: {
|
|
148
|
+
day: true,
|
|
149
|
+
month: true,
|
|
150
|
+
year: true
|
|
151
|
+
}
|
|
148
152
|
});
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
describe('when the component is a Time Input', function () {
|
|
156
|
+
var ID = 'field';
|
|
157
|
+
it('should always reject invalid time', function () {
|
|
158
|
+
var LABEL = 'Field';
|
|
159
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.TIME, LABEL, false);
|
|
160
|
+
|
|
161
|
+
var DATA = _defineProperty({}, ID, '25:45');
|
|
162
|
+
|
|
163
|
+
expect((0, _validateComponent.default)(COMPONENT, DATA)).toEqual({
|
|
164
|
+
error: 'Hour must be between 0 and 23',
|
|
165
|
+
id: ID,
|
|
166
|
+
properties: {
|
|
167
|
+
hour: true
|
|
168
|
+
}
|
|
162
169
|
});
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
describe('when the component is a container', function () {
|
|
173
|
+
it('should return an empty array when the container has only valid components', function () {
|
|
174
|
+
var EMAIL_ID = 'email';
|
|
175
|
+
var EMAIL_LABEL = 'Email';
|
|
176
|
+
var EMAIL = setup(EMAIL_ID, _models.ComponentTypes.EMAIL, EMAIL_LABEL, false);
|
|
177
|
+
var ID = 'container';
|
|
178
|
+
var LABEL = 'field';
|
|
179
|
+
var CONTAINER = setup(ID, _models.ComponentTypes.CONTAINER, LABEL, false);
|
|
180
|
+
CONTAINER.components = [EMAIL];
|
|
181
|
+
|
|
182
|
+
var DATA = _defineProperty({}, ID, _defineProperty({}, EMAIL_ID, 'alpha.bravo@digital.homeoffice.gov.uk'));
|
|
183
|
+
|
|
184
|
+
expect((0, _validateComponent.default)(CONTAINER, DATA)).toEqual([]);
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
describe('when the component is a collection', function () {
|
|
188
|
+
it('should return an empty array when the collection has only valid items', function () {
|
|
189
|
+
var EMAIL_ID = 'email';
|
|
190
|
+
var EMAIL_LABEL = 'Email';
|
|
191
|
+
var EMAIL = setup(EMAIL_ID, _models.ComponentTypes.EMAIL, EMAIL_LABEL, false);
|
|
192
|
+
var ID = 'collection';
|
|
193
|
+
var LABEL = 'field';
|
|
194
|
+
var COLLECTION = setup(ID, _models.ComponentTypes.COLLECTION, LABEL, false);
|
|
195
|
+
COLLECTION.item = [EMAIL];
|
|
196
|
+
|
|
197
|
+
var DATA = _defineProperty({}, ID, [_defineProperty({}, EMAIL_ID, 'alpha.bravo@homeoffice.gov.uk'), _defineProperty({}, EMAIL_ID, 'charlie.delta@homeoffice.gov.uk')]);
|
|
198
|
+
|
|
199
|
+
expect((0, _validateComponent.default)(COLLECTION, DATA)).toEqual([]);
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
describe('when the component has a nested component', function () {
|
|
203
|
+
it('should return no error when the radio component contains nested components without errors', function () {
|
|
204
|
+
var NESTED_ID = 'nestedId';
|
|
205
|
+
var NESTED_VALUE = 'nestedValue';
|
|
206
|
+
|
|
207
|
+
var FORMDATA = _defineProperty({}, NESTED_ID, NESTED_VALUE);
|
|
208
|
+
|
|
209
|
+
var COMPONENT = {
|
|
210
|
+
type: 'radios',
|
|
211
|
+
id: 'a',
|
|
212
|
+
data: {
|
|
213
|
+
options: [{
|
|
214
|
+
nested: {
|
|
215
|
+
type: 'text',
|
|
216
|
+
fieldId: NESTED_ID,
|
|
217
|
+
id: NESTED_ID,
|
|
218
|
+
required: true,
|
|
219
|
+
shown: true
|
|
220
|
+
}
|
|
221
|
+
}]
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
expect((0, _validateComponent.default)(COMPONENT, FORMDATA)).toBeUndefined();
|
|
225
|
+
});
|
|
226
|
+
it('should return an error when the radio component contains nested components with errors', function () {
|
|
227
|
+
var NESTED_ID = 'nestedId';
|
|
228
|
+
var FORMDATA = {};
|
|
229
|
+
var COMPONENT = {
|
|
230
|
+
type: 'radios',
|
|
231
|
+
id: 'a',
|
|
232
|
+
data: {
|
|
233
|
+
options: [{
|
|
234
|
+
nested: {
|
|
235
|
+
type: 'text',
|
|
236
|
+
fieldId: NESTED_ID,
|
|
237
|
+
id: NESTED_ID,
|
|
238
|
+
required: true,
|
|
239
|
+
shown: true
|
|
240
|
+
}
|
|
241
|
+
}]
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
expect((0, _validateComponent.default)(COMPONENT, FORMDATA)).toEqual({
|
|
245
|
+
id: NESTED_ID,
|
|
246
|
+
error: "Field is required"
|
|
206
247
|
});
|
|
207
248
|
});
|
|
249
|
+
it('should return no error when a non selected radio component contains nested components with errors', function () {
|
|
250
|
+
var NESTED_ID = 'nestedId';
|
|
251
|
+
var COMPONENT = {
|
|
252
|
+
type: 'radios',
|
|
253
|
+
id: 'a',
|
|
254
|
+
data: {
|
|
255
|
+
options: [{
|
|
256
|
+
nested: {
|
|
257
|
+
type: 'text',
|
|
258
|
+
fieldId: NESTED_ID,
|
|
259
|
+
id: NESTED_ID,
|
|
260
|
+
required: true
|
|
261
|
+
}
|
|
262
|
+
}]
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
expect((0, _validateComponent.default)(COMPONENT, {})).toBeUndefined();
|
|
266
|
+
});
|
|
208
267
|
});
|
|
209
268
|
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _validateComponent = _interopRequireDefault(require("./validateComponent"));
|
|
9
|
+
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
|
|
12
|
+
// Local imports
|
|
13
|
+
var validateContainer = function validateContainer(container, formData) {
|
|
14
|
+
var errors = [];
|
|
15
|
+
|
|
16
|
+
if (container && Array.isArray(container.components)) {
|
|
17
|
+
var fd = formData && container.fieldId ? formData[container.fieldId] : formData;
|
|
18
|
+
container.components.forEach(function (component) {
|
|
19
|
+
errors.push((0, _validateComponent.default)(component, fd));
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return errors.filter(function (e) {
|
|
24
|
+
return !!e;
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
var _default = validateContainer;
|
|
29
|
+
exports.default = _default;
|