@ukhomeoffice/cop-react-form-renderer 0.1.0-beta

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 (82) hide show
  1. package/README.md +38 -0
  2. package/dist/components/CheckYourAnswers/CheckYourAnswers.js +77 -0
  3. package/dist/components/CheckYourAnswers/CheckYourAnswers.scss +9 -0
  4. package/dist/components/CheckYourAnswers/index.js +13 -0
  5. package/dist/components/FormComponent/FormComponent.js +95 -0
  6. package/dist/components/FormComponent/index.js +13 -0
  7. package/dist/components/FormPage/FormPage.js +121 -0
  8. package/dist/components/FormPage/FormPage.scss +7 -0
  9. package/dist/components/FormPage/index.js +13 -0
  10. package/dist/components/FormRenderer/FormRenderer.js +203 -0
  11. package/dist/components/FormRenderer/FormRenderer.scss +5 -0
  12. package/dist/components/FormRenderer/helpers.js +66 -0
  13. package/dist/components/FormRenderer/helpers.test.js +158 -0
  14. package/dist/components/FormRenderer/index.js +13 -0
  15. package/dist/components/PageActions/PageActions.js +49 -0
  16. package/dist/components/PageActions/index.js +13 -0
  17. package/dist/components/SummaryList/RowAction.js +75 -0
  18. package/dist/components/SummaryList/SummaryList.js +93 -0
  19. package/dist/components/SummaryList/SummaryList.scss +2 -0
  20. package/dist/components/SummaryList/index.js +13 -0
  21. package/dist/components/index.js +31 -0
  22. package/dist/hooks/index.js +39 -0
  23. package/dist/hooks/useAxios.js +80 -0
  24. package/dist/hooks/useGetRequest.js +149 -0
  25. package/dist/hooks/useHooks.js +51 -0
  26. package/dist/hooks/useRefData.js +91 -0
  27. package/dist/index.js +48 -0
  28. package/dist/models/ComponentTypes.js +26 -0
  29. package/dist/models/FormTypes.js +18 -0
  30. package/dist/models/HubFormats.js +12 -0
  31. package/dist/models/index.js +31 -0
  32. package/dist/utils/CheckYourAnswers/getCYAAction.js +22 -0
  33. package/dist/utils/CheckYourAnswers/getCYAAction.test.js +92 -0
  34. package/dist/utils/CheckYourAnswers/getCYARow.js +27 -0
  35. package/dist/utils/CheckYourAnswers/getCYARow.test.js +151 -0
  36. package/dist/utils/CheckYourAnswers/getCYARowsForPage.js +24 -0
  37. package/dist/utils/CheckYourAnswers/getCYARowsForPage.test.js +134 -0
  38. package/dist/utils/CheckYourAnswers/index.js +17 -0
  39. package/dist/utils/CheckYourAnswers/showComponentCYA.js +37 -0
  40. package/dist/utils/CheckYourAnswers/showComponentCYA.test.js +54 -0
  41. package/dist/utils/Component/cleanAttributes.js +55 -0
  42. package/dist/utils/Component/cleanAttributes.test.js +69 -0
  43. package/dist/utils/Component/getComponent.js +117 -0
  44. package/dist/utils/Component/getComponent.test.js +329 -0
  45. package/dist/utils/Component/index.js +28 -0
  46. package/dist/utils/Component/isEditable.js +19 -0
  47. package/dist/utils/Component/isEditable.test.js +42 -0
  48. package/dist/utils/Component/showComponent.js +44 -0
  49. package/dist/utils/Component/showComponent.test.js +94 -0
  50. package/dist/utils/Component/wrapInFormGroup.js +23 -0
  51. package/dist/utils/Data/getAutocompleteSource.js +28 -0
  52. package/dist/utils/Data/getAutocompleteSource.test.js +146 -0
  53. package/dist/utils/Data/getOptions.js +21 -0
  54. package/dist/utils/Data/getOptions.test.js +71 -0
  55. package/dist/utils/Data/getSourceData.js +34 -0
  56. package/dist/utils/Data/getSourceData.test.js +125 -0
  57. package/dist/utils/Data/index.js +34 -0
  58. package/dist/utils/Data/refDataToOptions.js +33 -0
  59. package/dist/utils/Data/refDataToOptions.test.js +99 -0
  60. package/dist/utils/Data/setupFormData.js +63 -0
  61. package/dist/utils/Data/setupFormData.test.js +216 -0
  62. package/dist/utils/Data/setupRefDataUrlForComponent.js +29 -0
  63. package/dist/utils/Data/setupRefDataUrlForComponent.test.js +83 -0
  64. package/dist/utils/FormPage/getFormPage.js +45 -0
  65. package/dist/utils/FormPage/getFormPages.js +33 -0
  66. package/dist/utils/FormPage/getParagraphFromText.js +20 -0
  67. package/dist/utils/FormPage/index.js +19 -0
  68. package/dist/utils/FormPage/useComponent.js +31 -0
  69. package/dist/utils/Hub/getFormHub.js +30 -0
  70. package/dist/utils/Hub/index.js +20 -0
  71. package/dist/utils/Validate/index.js +26 -0
  72. package/dist/utils/Validate/validateComponent.js +49 -0
  73. package/dist/utils/Validate/validateComponent.test.js +119 -0
  74. package/dist/utils/Validate/validateEmail.js +39 -0
  75. package/dist/utils/Validate/validateEmail.test.js +57 -0
  76. package/dist/utils/Validate/validatePage.js +35 -0
  77. package/dist/utils/Validate/validatePage.test.js +122 -0
  78. package/dist/utils/Validate/validateRequired.js +38 -0
  79. package/dist/utils/Validate/validateRequired.test.js +50 -0
  80. package/dist/utils/index.js +43 -0
  81. package/dist/utils/meetsCondition.js +79 -0
  82. package/package.json +91 -0
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+
3
+ var _models = require("../../models");
4
+
5
+ var _validateComponent = _interopRequireDefault(require("./validateComponent"));
6
+
7
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
+
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
+
11
+ describe('utils', function () {
12
+ describe('Validate', function () {
13
+ describe('component', function () {
14
+ var setup = function setup(id, type, label, required) {
15
+ return {
16
+ id: id,
17
+ fieldId: id,
18
+ type: type,
19
+ label: label,
20
+ required: required
21
+ };
22
+ };
23
+
24
+ it('should return no error when the component is null', function () {
25
+ expect((0, _validateComponent.default)(null, {})).toBeUndefined();
26
+ });
27
+ describe('when there is no form data', function () {
28
+ it('should return no error when the component is not required and not an email type', function () {
29
+ var ID = 'field';
30
+ var LABEL = 'Field';
31
+ var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, false);
32
+ expect((0, _validateComponent.default)(COMPONENT, null)).toBeUndefined();
33
+ });
34
+ it('should return a required error when the component is required and not an email type', function () {
35
+ var ID = 'field';
36
+ var LABEL = 'Field';
37
+ var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, true);
38
+ expect((0, _validateComponent.default)(COMPONENT, null)).toEqual({
39
+ id: ID,
40
+ error: "".concat(LABEL, " is required")
41
+ });
42
+ });
43
+ it('should return a required error when the component is required and of type email', function () {
44
+ var ID = 'field';
45
+ var LABEL = 'Field';
46
+ var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, true);
47
+ expect((0, _validateComponent.default)(COMPONENT, null)).toEqual({
48
+ id: ID,
49
+ error: "".concat(LABEL, " is required")
50
+ });
51
+ });
52
+ it('should return no error when the component is of type email but not required', function () {
53
+ var ID = 'field';
54
+ var LABEL = 'Field';
55
+ var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, false);
56
+ expect((0, _validateComponent.default)(COMPONENT, null)).toBeUndefined();
57
+ });
58
+ });
59
+ describe('when the value is fully valid', function () {
60
+ var ID = 'field';
61
+
62
+ var DATA = _defineProperty({}, ID, 'alpha.bravo@digital.homeoffice.gov.uk');
63
+
64
+ it('should return no error when the component is not required and not an email type', function () {
65
+ var LABEL = 'Field';
66
+ var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, false);
67
+ expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
68
+ });
69
+ it('should return no error when the component is required and not an email type', function () {
70
+ var LABEL = 'Field';
71
+ var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, true);
72
+ expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
73
+ });
74
+ it('should return no error when the component is required and of type email', function () {
75
+ var LABEL = 'Field';
76
+ var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, true);
77
+ expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
78
+ });
79
+ it('should return no error when the component is of type email but not required', function () {
80
+ var LABEL = 'Field';
81
+ var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, false);
82
+ expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
83
+ });
84
+ });
85
+ describe('when the value is an invalid email', function () {
86
+ var ID = 'field';
87
+
88
+ var DATA = _defineProperty({}, ID, 'alpha.bravo@hotmail.com');
89
+
90
+ it('should return no error when the component is not required and not an email type', function () {
91
+ var LABEL = 'Field';
92
+ var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, false);
93
+ expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
94
+ });
95
+ it('should return no error when the component is required and not an email type', function () {
96
+ var LABEL = 'Field';
97
+ var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, true);
98
+ expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
99
+ });
100
+ it('should return no error when the component is required and of type email', function () {
101
+ var LABEL = 'Field';
102
+ var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, true);
103
+ expect((0, _validateComponent.default)(COMPONENT, DATA)).toEqual({
104
+ id: ID,
105
+ error: "Enter ".concat(LABEL.toLowerCase(), " in the correct format, like jane.doe@homeoffice.gov.uk")
106
+ });
107
+ });
108
+ it('should return no error when the component is of type email but not required', function () {
109
+ var LABEL = 'Field';
110
+ var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, false);
111
+ expect((0, _validateComponent.default)(COMPONENT, DATA)).toEqual({
112
+ id: ID,
113
+ error: "Enter ".concat(LABEL.toLowerCase(), " in the correct format, like jane.doe@homeoffice.gov.uk")
114
+ });
115
+ });
116
+ });
117
+ });
118
+ });
119
+ });
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ // eslint-disable-next-line no-control-regex
8
+ // const EMAIL_REGEX = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/i;
9
+ var HODS_EMAIL_REGEX = /^[a-z0-9._-]+@(digital\.)?homeoffice.gov.uk$/i;
10
+ /**
11
+ * Validates an email address, ensuring it is in the correct domain and
12
+ * complies with the Home Office email address standards.
13
+ *
14
+ * Note that an empty string is not considered invalid. You should use
15
+ * validateRequired (Validate.required) for that sort of validation.
16
+ * @param {*} value The value to validate.
17
+ * @param {string} label The label to use in any error message.
18
+ * @returns An error if the email address is invalid.
19
+ */
20
+
21
+ var validateEmail = function validateEmail(value) {
22
+ var label = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
23
+
24
+ if (!!value) {
25
+ if (typeof value === 'string') {
26
+ if (HODS_EMAIL_REGEX.test(value)) {
27
+ return undefined;
28
+ }
29
+ }
30
+
31
+ var name = label ? label.toLowerCase() : 'email address';
32
+ return "Enter ".concat(name, " in the correct format, like jane.doe@homeoffice.gov.uk");
33
+ }
34
+
35
+ return undefined;
36
+ };
37
+
38
+ var _default = validateEmail;
39
+ exports.default = _default;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+
3
+ var _validateEmail = _interopRequireDefault(require("./validateEmail"));
4
+
5
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
6
+
7
+ // Local imports
8
+ describe('utils', function () {
9
+ describe('Validate', function () {
10
+ describe('email', function () {
11
+ var LABEL = 'Component';
12
+ var ERROR = "Enter ".concat(LABEL.toLowerCase(), " in the correct format, like jane.doe@homeoffice.gov.uk"); // Valid values
13
+
14
+ it('should return no error when the value is a valid .gov.uk address', function () {
15
+ expect((0, _validateEmail.default)('alpha@homeoffice.gov.uk', LABEL)).toBeUndefined();
16
+ });
17
+ it('should return no error when the value is a capitalised digital.homeoffice.gov.uk address', function () {
18
+ expect((0, _validateEmail.default)('ALPHA.BRAVO@DIGITAL.HOMEOFFICE.GOV.UK', LABEL)).toBeUndefined();
19
+ });
20
+ it('should return no error when the value is an empty string', function () {
21
+ expect((0, _validateEmail.default)('', LABEL)).toBeUndefined();
22
+ }); // Invalid values
23
+
24
+ it('should return an error when the value is an empty object', function () {
25
+ expect((0, _validateEmail.default)({}, LABEL)).toEqual(ERROR);
26
+ });
27
+ it('should return an error when the value is an array', function () {
28
+ expect((0, _validateEmail.default)(['bob'], LABEL)).toEqual(ERROR);
29
+ });
30
+ it('should return an error when the value is numeric', function () {
31
+ expect((0, _validateEmail.default)(24, LABEL)).toEqual(ERROR);
32
+ });
33
+ it('should return an error when the value is in the wrong domain', function () {
34
+ expect((0, _validateEmail.default)('alpha@domain.com', LABEL)).toEqual(ERROR);
35
+ });
36
+ it('should return an error when the domain contains spaces', function () {
37
+ expect((0, _validateEmail.default)('alpha.bravo@digital homeoffice.gov.uk', LABEL)).toEqual(ERROR);
38
+ });
39
+ it('should return an error when the value has no TLD', function () {
40
+ expect((0, _validateEmail.default)('alpha.bravo@homeoffice', LABEL)).toEqual(ERROR);
41
+ });
42
+ it('should return an error when there is no name', function () {
43
+ expect((0, _validateEmail.default)('@homeoffice.gov.uk', LABEL)).toEqual(ERROR);
44
+ });
45
+ it('should return an error when there is no @ symbol', function () {
46
+ expect((0, _validateEmail.default)('alpha.bravo.homeoffice.gov.uk', LABEL)).toEqual(ERROR);
47
+ });
48
+ it('should return an error when the name contains spaces', function () {
49
+ expect((0, _validateEmail.default)('alpha bravo@digital.homeoffice.gov.uk', LABEL)).toEqual(ERROR);
50
+ });
51
+ it('should use a default label when none is specified', function () {
52
+ var DEFAULT_ERROR = 'Enter email address in the correct format, like jane.doe@homeoffice.gov.uk';
53
+ expect((0, _validateEmail.default)(['bob'], undefined)).toEqual(DEFAULT_ERROR);
54
+ });
55
+ });
56
+ });
57
+ });
@@ -0,0 +1,35 @@
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
+
14
+ /**
15
+ * Validate all of the components on a page.
16
+ * @param {Array} components The components to validate.
17
+ * @param {object} formData The data to use that holds this components' values.
18
+ * @returns An array containing all of the errors.
19
+ */
20
+ var validatePage = function validatePage(components, formData) {
21
+ var errors = [];
22
+
23
+ if (Array.isArray(components)) {
24
+ components.forEach(function (component) {
25
+ errors.push((0, _validateComponent.default)(component, formData));
26
+ });
27
+ }
28
+
29
+ return errors.filter(function (e) {
30
+ return !!e;
31
+ });
32
+ };
33
+
34
+ var _default = validatePage;
35
+ exports.default = _default;
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+
3
+ var _models = require("../../models");
4
+
5
+ var _validatePage = _interopRequireDefault(require("./validatePage"));
6
+
7
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
+
9
+ // Local imports
10
+ describe('utils', function () {
11
+ describe('Validate', function () {
12
+ describe('page', function () {
13
+ var setup = function setup(id, type, label, required) {
14
+ return {
15
+ id: id,
16
+ fieldId: id,
17
+ type: type,
18
+ label: label,
19
+ required: required
20
+ };
21
+ };
22
+
23
+ it('should return no error when the components array is null', function () {
24
+ expect((0, _validatePage.default)(null, {}).length).toEqual(0);
25
+ });
26
+ it('should return no error when the components array is empty', function () {
27
+ expect((0, _validatePage.default)([], {}).length).toEqual(0);
28
+ });
29
+ describe('when there is no form data', function () {
30
+ it('should return a single error when no components are required', function () {
31
+ var COMPONENTS = [setup('a', _models.ComponentTypes.TEXT, 'Alpha', false), setup('b', _models.ComponentTypes.TEXT, 'Bravo', false)];
32
+ expect((0, _validatePage.default)(COMPONENTS, null).length).toEqual(0);
33
+ });
34
+ it('should return an error for each required component', function () {
35
+ var COMPONENTS = [setup('a', _models.ComponentTypes.TEXT, 'Alpha', true), setup('b', _models.ComponentTypes.TEXT, 'Bravo', true), setup('c', _models.ComponentTypes.TEXT, 'Charlie', false), // The only unrequired one
36
+ setup('d', _models.ComponentTypes.TEXT, 'Delta', true), setup('e', _models.ComponentTypes.TEXT, 'Echo', true)];
37
+ var RESULT = (0, _validatePage.default)(COMPONENTS, null);
38
+ expect(RESULT.length).toEqual(4);
39
+ expect(RESULT[0]).toEqual({
40
+ id: 'a',
41
+ error: 'Alpha is required'
42
+ });
43
+ expect(RESULT[1]).toEqual({
44
+ id: 'b',
45
+ error: 'Bravo is required'
46
+ });
47
+ expect(RESULT[2]).toEqual({
48
+ id: 'd',
49
+ error: 'Delta is required'
50
+ });
51
+ expect(RESULT[3]).toEqual({
52
+ id: 'e',
53
+ error: 'Echo is required'
54
+ });
55
+ });
56
+ });
57
+ describe('when the form data is fully valid', function () {
58
+ var DATA = {
59
+ alpha: 'alpha.smith@digital.homeoffice.gov.uk',
60
+ bravo: 'bravo.jones@digital.homeoffice.gov.uk'
61
+ };
62
+ it('should return no errors when none of the components are required or email types', function () {
63
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.TEXT, 'Alpha', false), setup('bravo', _models.ComponentTypes.TEXT, 'Bravo', false)];
64
+ expect((0, _validatePage.default)(COMPONENTS, DATA).length).toEqual(0);
65
+ });
66
+ it('should return no errors when all of the components are required but not email types', function () {
67
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.TEXT, 'Alpha', true), setup('bravo', _models.ComponentTypes.TEXT, 'Bravo', true)];
68
+ expect((0, _validatePage.default)(COMPONENTS, DATA).length).toEqual(0);
69
+ });
70
+ it('should return no errors when none of the components are required but are all email types', function () {
71
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.EMAIL, 'Alpha', false), setup('bravo', _models.ComponentTypes.EMAIL, 'Bravo', false)];
72
+ expect((0, _validatePage.default)(COMPONENTS, DATA).length).toEqual(0);
73
+ });
74
+ it('should return no errors when all of the components are required and email types', function () {
75
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.EMAIL, 'Alpha', true), setup('bravo', _models.ComponentTypes.EMAIL, 'Bravo', true)];
76
+ expect((0, _validatePage.default)(COMPONENTS, DATA).length).toEqual(0);
77
+ });
78
+ });
79
+ describe('when the form data has one field missing and includes an invalid email', function () {
80
+ var DATA = {
81
+ alpha: 'alpha.smith@digital.homeoffice.gov.uk',
82
+ bravo: 'bravo.jones@hotmail.com'
83
+ };
84
+ it('should return no errors when none of the components are required or email types', function () {
85
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.TEXT, 'Alpha', false), setup('bravo', _models.ComponentTypes.TEXT, 'Bravo', false), setup('charlie', _models.ComponentTypes.TEXT, 'Charlie', false)];
86
+ expect((0, _validatePage.default)(COMPONENTS, DATA).length).toEqual(0);
87
+ });
88
+ it('should return an error for the missing field when all are required but not email types', function () {
89
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.TEXT, 'Alpha', true), setup('bravo', _models.ComponentTypes.TEXT, 'Bravo', true), setup('charlie', _models.ComponentTypes.TEXT, 'Charlie', true)];
90
+ var RESULT = (0, _validatePage.default)(COMPONENTS, DATA);
91
+ expect(RESULT.length).toEqual(1);
92
+ expect(RESULT[0]).toEqual({
93
+ id: 'charlie',
94
+ error: 'Charlie is required'
95
+ });
96
+ });
97
+ it('should return an error for the invalid email field when none are required but all email types', function () {
98
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.EMAIL, 'Alpha', false), setup('bravo', _models.ComponentTypes.EMAIL, 'Bravo', false), setup('charlie', _models.ComponentTypes.EMAIL, 'Charlie', false)];
99
+ var RESULT = (0, _validatePage.default)(COMPONENTS, DATA);
100
+ expect(RESULT.length).toEqual(1);
101
+ expect(RESULT[0]).toEqual({
102
+ id: 'bravo',
103
+ error: 'Enter bravo in the correct format, like jane.doe@homeoffice.gov.uk'
104
+ });
105
+ });
106
+ it('should return an error for both invalid fields when all are required and email types', function () {
107
+ var COMPONENTS = [setup('alpha', _models.ComponentTypes.EMAIL, 'Alpha', true), setup('bravo', _models.ComponentTypes.EMAIL, 'Bravo', true), setup('charlie', _models.ComponentTypes.EMAIL, 'Charlie', true)];
108
+ var RESULT = (0, _validatePage.default)(COMPONENTS, DATA);
109
+ expect(RESULT.length).toEqual(2);
110
+ expect(RESULT[0]).toEqual({
111
+ id: 'bravo',
112
+ error: 'Enter bravo in the correct format, like jane.doe@homeoffice.gov.uk'
113
+ });
114
+ expect(RESULT[1]).toEqual({
115
+ id: 'charlie',
116
+ error: 'Charlie is required'
117
+ });
118
+ });
119
+ });
120
+ });
121
+ });
122
+ });
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ /**
9
+ * Validates that a value is not nullish, which includes ensuring
10
+ * a string isn't just whitespace, an array is not empty, etc.
11
+ * @param {any} value The value to validate.
12
+ * @param {string} label The label to use in any error message.
13
+ * @returns An error if the value is nullish.
14
+ */
15
+ var validateRequired = function validateRequired(value) {
16
+ var label = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
17
+ var hasValue = false;
18
+
19
+ if (!!value || value === false || value === 0) {
20
+ hasValue = true;
21
+
22
+ if (typeof value === 'string') {
23
+ hasValue = value.trim().length > 0;
24
+ } else if (Array.isArray(value)) {
25
+ hasValue = value.length > 0;
26
+ }
27
+ }
28
+
29
+ if (!hasValue) {
30
+ var name = label ? label : 'Field';
31
+ return "".concat(name, " is required");
32
+ }
33
+
34
+ return undefined;
35
+ };
36
+
37
+ var _default = validateRequired;
38
+ exports.default = _default;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+
3
+ var _validateRequired = _interopRequireDefault(require("./validateRequired"));
4
+
5
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
6
+
7
+ // Local imports
8
+ describe('utils', function () {
9
+ describe('Validate', function () {
10
+ describe('required', function () {
11
+ var LABEL = 'Component';
12
+ var ERROR = "".concat(LABEL, " is required"); // Valid values
13
+
14
+ it('should return no error when the value is set', function () {
15
+ expect((0, _validateRequired.default)('value', LABEL)).toBeUndefined();
16
+ });
17
+ it('should return no error when the value is an empty object', function () {
18
+ expect((0, _validateRequired.default)({}, LABEL)).toBeUndefined();
19
+ });
20
+ it('should return no error when the value is a non-empty array', function () {
21
+ expect((0, _validateRequired.default)(['bob'], LABEL)).toBeUndefined();
22
+ });
23
+ it('should return no error when the value is a boolean false', function () {
24
+ expect((0, _validateRequired.default)(false, LABEL)).toBeUndefined();
25
+ });
26
+ it('should return no error when the value is the number zero', function () {
27
+ expect((0, _validateRequired.default)(0, LABEL)).toBeUndefined();
28
+ }); // Invalid values
29
+
30
+ it('should return an error when the value is undefined', function () {
31
+ expect((0, _validateRequired.default)(undefined, LABEL)).toEqual(ERROR);
32
+ });
33
+ it('should return an error when the value is null', function () {
34
+ expect((0, _validateRequired.default)(null, LABEL)).toEqual(ERROR);
35
+ });
36
+ it('should return an error when the value is an empty string', function () {
37
+ expect((0, _validateRequired.default)('', LABEL)).toEqual(ERROR);
38
+ });
39
+ it('should return an error when the value is a string containing just spaces', function () {
40
+ expect((0, _validateRequired.default)(' ', LABEL)).toEqual(ERROR);
41
+ });
42
+ it('should return an error when the value is an empty array', function () {
43
+ expect((0, _validateRequired.default)([], LABEL)).toEqual(ERROR);
44
+ });
45
+ it('should use a default label when none is specified', function () {
46
+ expect((0, _validateRequired.default)(undefined, undefined)).toEqual('Field is required');
47
+ });
48
+ });
49
+ });
50
+ });
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _copReactComponents = require("@ukhomeoffice/cop-react-components");
9
+
10
+ var _CheckYourAnswers = _interopRequireDefault(require("./CheckYourAnswers"));
11
+
12
+ var _Component = _interopRequireDefault(require("./Component"));
13
+
14
+ var _Data = _interopRequireDefault(require("./Data"));
15
+
16
+ var _FormPage = _interopRequireDefault(require("./FormPage"));
17
+
18
+ var _Hub = _interopRequireDefault(require("./Hub"));
19
+
20
+ var _meetsCondition = _interopRequireDefault(require("./meetsCondition"));
21
+
22
+ var _Validate = _interopRequireDefault(require("./Validate"));
23
+
24
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
+
26
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
27
+
28
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
29
+
30
+ 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; }
31
+
32
+ var Utils = _objectSpread({
33
+ CheckYourAnswers: _CheckYourAnswers.default,
34
+ Component: _Component.default,
35
+ Data: _Data.default,
36
+ FormPage: _FormPage.default,
37
+ Hub: _Hub.default,
38
+ meetsCondition: _meetsCondition.default,
39
+ Validate: _Validate.default
40
+ }, _copReactComponents.Utils);
41
+
42
+ var _default = Utils;
43
+ exports.default = _default;
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ 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); }
9
+
10
+ var getComparisonValue = function getComparisonValue(condition) {
11
+ if (['in', 'nin'].includes(condition.op)) {
12
+ return condition.values;
13
+ }
14
+
15
+ return condition.value;
16
+ };
17
+ /**
18
+ * Looks at a condition object to see if the supplied value meets it.
19
+ * The condition object contains an `op` property, along with a comparator
20
+ * `value` (or `values`).
21
+ *
22
+ * The evaluation is strict - i.e., it matches the value AND the type.
23
+ *
24
+ * @param {object} condition The condition object.
25
+ * @param {*} value The value to test against the condition.
26
+ *
27
+ * @returns A boolean indicating whether the value meets the condition.
28
+ */
29
+
30
+
31
+ var meetsCondition = function meetsCondition(condition, value) {
32
+ if (condition && _typeof(condition) === 'object') {
33
+ var compare = getComparisonValue(condition);
34
+
35
+ switch (condition.op) {
36
+ case '=':
37
+ case 'eq':
38
+ {
39
+ return compare === value;
40
+ }
41
+
42
+ case '!=':
43
+ case '<>':
44
+ case 'ne':
45
+ case 'neq':
46
+ {
47
+ return compare !== value;
48
+ }
49
+
50
+ case 'in':
51
+ {
52
+ if (Array.isArray(compare)) {
53
+ return compare.includes(value);
54
+ } // If it's not an array, nothing can be IN it, so it must fail the condition.
55
+
56
+
57
+ return false;
58
+ }
59
+
60
+ case 'nin':
61
+ {
62
+ if (Array.isArray(compare)) {
63
+ return compare.includes(value) === false;
64
+ } // If it's not an array, nothing can be IN it, so it must meet the condition.
65
+
66
+
67
+ return true;
68
+ }
69
+
70
+ default:
71
+ return false;
72
+ }
73
+ }
74
+
75
+ return true;
76
+ };
77
+
78
+ var _default = meetsCondition;
79
+ exports.default = _default;