@ukhomeoffice/cop-react-form-renderer 2.8.3 → 2.9.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.
Files changed (32) hide show
  1. package/dist/components/CheckYourAnswers/CheckYourAnswers.test.js +1 -1
  2. package/dist/components/FormComponent/FormComponent.js +2 -1
  3. package/dist/components/FormRenderer/helpers/getSubmissionStatus.js +7 -0
  4. package/dist/components/FormRenderer/helpers/getSubmissionStatus.test.js +2 -2
  5. package/dist/components/SummaryList/GroupAction.js +5 -1
  6. package/dist/components/SummaryList/SummaryList.js +1 -1
  7. package/dist/json/group.json +1 -17
  8. package/dist/json/port.json +346 -0
  9. package/dist/json/sublocation.json +859 -0
  10. package/dist/json/terminal.json +81 -0
  11. package/dist/models/ComponentTypes.js +2 -0
  12. package/dist/utils/CheckYourAnswers/getCYARow.js +12 -0
  13. package/dist/utils/CheckYourAnswers/getCYARow.test.js +28 -1
  14. package/dist/utils/CheckYourAnswers/getCYARowForGroup.js +1 -0
  15. package/dist/utils/Component/cleanAttributes.js +1 -1
  16. package/dist/utils/Component/getComponent.js +36 -3
  17. package/dist/utils/Component/getComponentTests/getComponent.nested.test.js +92 -0
  18. package/dist/utils/Meta/constants.js +10 -0
  19. package/dist/utils/Meta/documents/getDocuments.js +18 -0
  20. package/dist/utils/Meta/documents/getDocuments.test.js +43 -0
  21. package/dist/utils/Meta/documents/index.js +23 -0
  22. package/dist/utils/Meta/documents/setDocumentForField.js +36 -0
  23. package/dist/utils/Meta/documents/setDocumentForField.test.js +49 -0
  24. package/dist/utils/Meta/index.js +21 -0
  25. package/dist/utils/Validate/validateCollection.js +42 -0
  26. package/dist/utils/Validate/validateCollection.test.js +74 -0
  27. package/dist/utils/Validate/validateComponent.js +67 -48
  28. package/dist/utils/Validate/validateComponent.test.js +247 -188
  29. package/dist/utils/Validate/validateContainer.js +29 -0
  30. package/dist/utils/Validate/validateContainer.test.js +68 -0
  31. package/dist/utils/index.js +4 -1
  32. package/package.json +2 -2
@@ -11,97 +11,116 @@ var _showComponent = _interopRequireDefault(require("../Component/showComponent"
11
11
 
12
12
  var _additional = _interopRequireDefault(require("./additional"));
13
13
 
14
- var _validateDate2 = _interopRequireDefault(require("./validateDate"));
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 _validateTime2 = _interopRequireDefault(require("./validateTime"));
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 && (0, _showComponent.default)(component, formData)) {
53
- if (component.type === _models.ComponentTypes.CONTAINER) {
54
- return validateContainer(component, formData);
55
- }
48
+ if (component.type === _models.ComponentTypes.CONTAINER) {
49
+ return (0, _validateContainer.default)(component, formData);
50
+ }
56
51
 
57
- var value = data[component.fieldId];
58
- delete component.propsInError;
52
+ var value = data[component.fieldId];
59
53
 
60
- if (component.required) {
61
- error = (0, _validateRequired.default)(value, component.label, component.custom_errors);
62
- }
54
+ if (component.required) {
55
+ error = (0, _validateRequired.default)(value, component.label, component.custom_errors);
56
+ }
63
57
 
64
- if (!error && component.type === _models.ComponentTypes.EMAIL) {
65
- error = (0, _validateEmail.default)(value, component.label);
66
- }
58
+ if (!error) {
59
+ switch (component.type) {
60
+ case _models.ComponentTypes.COLLECTION:
61
+ return (0, _validateCollection.default)(component, value);
67
62
 
68
- if (!error && component.type === _models.ComponentTypes.DATE) {
69
- var _validateDate = (0, _validateDate2.default)(value),
70
- message = _validateDate.message,
71
- propsInError = _validateDate.propsInError;
63
+ case _models.ComponentTypes.EMAIL:
64
+ error = (0, _validateEmail.default)(value, component.label);
65
+ break;
72
66
 
73
- component.propsInError = propsInError;
74
- error = message;
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
- if (!error && component.type === _models.ComponentTypes.TIME) {
78
- var _validateTime = (0, _validateTime2.default)(value),
79
- _message = _validateTime.message,
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
- component.propsInError = _propsInError;
83
- error = _message;
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
- component.propsInError = {
106
+ propertiesInError = {
91
107
  day: true,
92
108
  month: true,
93
109
  year: true
94
110
  };
95
111
  }
96
112
  }
113
+ }
97
114
 
98
- component.error = error;
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 error ? {
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
- describe('Validate', function () {
13
- describe('component', function () {
14
- var setup = function setup(id, type, label, required, additionalValidation) {
15
- return {
16
- id: id,
17
- fieldId: id,
18
- type: type,
19
- label: label,
20
- required: required,
21
- additionalValidation: additionalValidation
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
- it('should return no error when the component is null', function () {
26
- expect((0, _validateComponent.default)(null, {})).toBeUndefined();
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
- describe('when there is no form data', function () {
29
- it('should return no error when the component is not required and not an email type', function () {
30
- var ID = 'field';
31
- var LABEL = 'Field';
32
- var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, false);
33
- expect((0, _validateComponent.default)(COMPONENT, null)).toBeUndefined();
34
- });
35
- it('should return a required error when the component is required and not an email type', function () {
36
- var ID = 'field';
37
- var LABEL = 'Field';
38
- var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, true);
39
- expect((0, _validateComponent.default)(COMPONENT, null)).toEqual({
40
- id: ID,
41
- error: "".concat(LABEL, " is required")
42
- });
43
- });
44
- it('should return a required error when the component is required and of type email', function () {
45
- var ID = 'field';
46
- var LABEL = 'Field';
47
- var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, true);
48
- expect((0, _validateComponent.default)(COMPONENT, null)).toEqual({
49
- id: ID,
50
- error: "".concat(LABEL, " is required")
51
- });
52
- });
53
- it('should return no error when the component is of type email but not required', function () {
54
- var ID = 'field';
55
- var LABEL = 'Field';
56
- var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, false);
57
- expect((0, _validateComponent.default)(COMPONENT, null)).toBeUndefined();
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
- describe('when the value is fully valid', function () {
61
- var ID = 'field';
62
-
63
- var DATA = _defineProperty({}, ID, 'alpha.bravo@digital.homeoffice.gov.uk');
64
-
65
- it('should return no error when the component is not required and not an email type', function () {
66
- var LABEL = 'Field';
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
- describe('when the value is an invalid email', function () {
87
- var ID = 'field';
88
-
89
- var DATA = _defineProperty({}, ID, 'alpha.bravo@hotmail.com');
90
-
91
- it('should return no error when the component is not required and not an email type', function () {
92
- var LABEL = 'Field';
93
- var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, false);
94
- expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
95
- });
96
- it('should return no error when the component is required and not an email type', function () {
97
- var LABEL = 'Field';
98
- var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, true);
99
- expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
100
- });
101
- it('should return no error when the component is required and of type email', function () {
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
- 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
- });
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
- 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
- });
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
- 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
- });
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;