@ukhomeoffice/cop-react-form-renderer 3.0.0 → 3.0.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/utils/Component/getComponentTests/getComponent.date.test.js +6 -6
- package/dist/utils/Validate/index.js +3 -0
- package/dist/utils/Validate/validateComponent.js +6 -0
- package/dist/utils/Validate/validateRegex.js +56 -0
- package/dist/utils/Validate/validateRegex.test.js +41 -0
- package/package.json +2 -2
|
@@ -94,8 +94,8 @@ describe('utils.Component.get', function () {
|
|
|
94
94
|
}
|
|
95
95
|
});
|
|
96
96
|
|
|
97
|
-
expect(ON_CHANGE_CALLS.length).toEqual(
|
|
98
|
-
expect(ON_CHANGE_CALLS[
|
|
97
|
+
expect(ON_CHANGE_CALLS.length).toEqual(1);
|
|
98
|
+
expect(ON_CHANGE_CALLS[0]).toMatchObject({
|
|
99
99
|
name: FIELD_ID,
|
|
100
100
|
value: '5--'
|
|
101
101
|
}); // And now the month...
|
|
@@ -107,8 +107,8 @@ describe('utils.Component.get', function () {
|
|
|
107
107
|
}
|
|
108
108
|
});
|
|
109
109
|
|
|
110
|
-
expect(ON_CHANGE_CALLS.length).toEqual(
|
|
111
|
-
expect(ON_CHANGE_CALLS[
|
|
110
|
+
expect(ON_CHANGE_CALLS.length).toEqual(2);
|
|
111
|
+
expect(ON_CHANGE_CALLS[1]).toMatchObject({
|
|
112
112
|
name: FIELD_ID,
|
|
113
113
|
value: '5-11-'
|
|
114
114
|
}); // And finally the year.
|
|
@@ -120,8 +120,8 @@ describe('utils.Component.get', function () {
|
|
|
120
120
|
}
|
|
121
121
|
});
|
|
122
122
|
|
|
123
|
-
expect(ON_CHANGE_CALLS.length).toEqual(
|
|
124
|
-
expect(ON_CHANGE_CALLS[
|
|
123
|
+
expect(ON_CHANGE_CALLS.length).toEqual(3);
|
|
124
|
+
expect(ON_CHANGE_CALLS[2]).toMatchObject({
|
|
125
125
|
name: FIELD_ID,
|
|
126
126
|
value: '5-11-2022'
|
|
127
127
|
});
|
|
@@ -13,6 +13,8 @@ var _validateEmail = _interopRequireDefault(require("./validateEmail"));
|
|
|
13
13
|
|
|
14
14
|
var _validatePage = _interopRequireDefault(require("./validatePage"));
|
|
15
15
|
|
|
16
|
+
var _validateRegex = _interopRequireDefault(require("./validateRegex"));
|
|
17
|
+
|
|
16
18
|
var _validateRequired = _interopRequireDefault(require("./validateRequired"));
|
|
17
19
|
|
|
18
20
|
var _validateTime = _interopRequireDefault(require("./validateTime"));
|
|
@@ -25,6 +27,7 @@ var Validate = {
|
|
|
25
27
|
email: _validateEmail.default,
|
|
26
28
|
date: _validateDate.default,
|
|
27
29
|
page: _validatePage.default,
|
|
30
|
+
regex: _validateRegex.default,
|
|
28
31
|
required: _validateRequired.default,
|
|
29
32
|
time: _validateTime.default
|
|
30
33
|
};
|
|
@@ -19,6 +19,8 @@ var _validateDate = _interopRequireDefault(require("./validateDate"));
|
|
|
19
19
|
|
|
20
20
|
var _validateEmail = _interopRequireDefault(require("./validateEmail"));
|
|
21
21
|
|
|
22
|
+
var _validateRegex = _interopRequireDefault(require("./validateRegex"));
|
|
23
|
+
|
|
22
24
|
var _validateRequired = _interopRequireDefault(require("./validateRequired"));
|
|
23
25
|
|
|
24
26
|
var _validateTime = _interopRequireDefault(require("./validateTime"));
|
|
@@ -99,6 +101,10 @@ var validateComponent = function validateComponent(component, formData) {
|
|
|
99
101
|
break;
|
|
100
102
|
}
|
|
101
103
|
|
|
104
|
+
if (!error && component.pattern) {
|
|
105
|
+
error = (0, _validateRegex.default)(value, component.label, component.pattern, component.custom_errors);
|
|
106
|
+
}
|
|
107
|
+
|
|
102
108
|
if (!error && component.additionalValidation) {
|
|
103
109
|
error = (0, _additional.default)(component, value);
|
|
104
110
|
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Validates an components value against a given regex pattern.
|
|
10
|
+
*
|
|
11
|
+
* An empty string for value is considered valid as not all components
|
|
12
|
+
* with pattern validation have to be required.
|
|
13
|
+
* @param {*} value The value to validate.
|
|
14
|
+
* @param {string} label The label to use in the default error message.
|
|
15
|
+
* @param {string} pattern The regex pattern to validate against.
|
|
16
|
+
* @param {array} customErrors The array of custom errors for the component.
|
|
17
|
+
* @returns An error if the value doesn't match the regex pattern.
|
|
18
|
+
*/
|
|
19
|
+
var validateRegex = function validateRegex(value) {
|
|
20
|
+
var label = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
|
21
|
+
var pattern = arguments.length > 2 ? arguments[2] : undefined;
|
|
22
|
+
var customErrors = arguments.length > 3 ? arguments[3] : undefined;
|
|
23
|
+
|
|
24
|
+
if (!value) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (typeof value === 'string') {
|
|
29
|
+
var regex = new RegExp(pattern);
|
|
30
|
+
|
|
31
|
+
if (regex.test(value)) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (Array.isArray(customErrors)) {
|
|
36
|
+
var _result$;
|
|
37
|
+
|
|
38
|
+
var result = customErrors.filter(function (error) {
|
|
39
|
+
return error.type === 'pattern';
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
if (result !== null && result !== void 0 && (_result$ = result[0]) !== null && _result$ !== void 0 && _result$.message) {
|
|
43
|
+
return result[0].message;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (label === '') {
|
|
49
|
+
return 'Component failed regex validation';
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return "".concat(label, " failed regex validation");
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
var _default = validateRegex;
|
|
56
|
+
exports.default = _default;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _validateRegex = _interopRequireDefault(require("./validateRegex"));
|
|
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('regex', function () {
|
|
11
|
+
var GOOD_VALUE = 'hello';
|
|
12
|
+
var BAD_VALUE = 'h3llo';
|
|
13
|
+
var LABEL = 'Component';
|
|
14
|
+
var PATTERN = '^[a-z]*$';
|
|
15
|
+
var CUSTOM_ERRORS = [{
|
|
16
|
+
"type": "pattern",
|
|
17
|
+
"message": "Regex validation failed for ".concat(LABEL)
|
|
18
|
+
}];
|
|
19
|
+
var DEFAULT_ERROR = [{
|
|
20
|
+
"type": "pattern"
|
|
21
|
+
}]; // Valid values
|
|
22
|
+
|
|
23
|
+
it('should return no error when the value matches the regex pattern', function () {
|
|
24
|
+
expect((0, _validateRegex.default)(GOOD_VALUE, LABEL, PATTERN, CUSTOM_ERRORS)).toBeUndefined();
|
|
25
|
+
});
|
|
26
|
+
it('should return no error when the value is an empty string', function () {
|
|
27
|
+
expect((0, _validateRegex.default)('', LABEL, PATTERN, CUSTOM_ERRORS)).toBeUndefined();
|
|
28
|
+
}); // Invalid values
|
|
29
|
+
|
|
30
|
+
it('should return a custom error when the value does not match the regex pattern and one is specified', function () {
|
|
31
|
+
expect((0, _validateRegex.default)(BAD_VALUE, LABEL, PATTERN, CUSTOM_ERRORS)).toEqual(CUSTOM_ERRORS[0].message);
|
|
32
|
+
});
|
|
33
|
+
it('should return an error using label when the value does not match the regex pattern and a custom error is not specified', function () {
|
|
34
|
+
expect((0, _validateRegex.default)(BAD_VALUE, LABEL, PATTERN, DEFAULT_ERROR)).toEqual("".concat(LABEL, " failed regex validation"));
|
|
35
|
+
});
|
|
36
|
+
it('should return a default error when the value does not match the regex pattern and both a custom error and label are not specified', function () {
|
|
37
|
+
expect((0, _validateRegex.default)(BAD_VALUE, '', PATTERN, DEFAULT_ERROR)).toEqual('Component failed regex validation');
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ukhomeoffice/cop-react-form-renderer",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.1",
|
|
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": "1.
|
|
19
|
+
"@ukhomeoffice/cop-react-components": "1.7.1",
|
|
20
20
|
"axios": "^0.21.1",
|
|
21
21
|
"dayjs": "^1.11.0",
|
|
22
22
|
"govuk-frontend": "^3.13.0",
|