@ukhomeoffice/cop-react-form-renderer 6.16.3-alpha → 6.16.4
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.
|
@@ -94,7 +94,7 @@ const validateComponent = (component, outerData, formData) => {
|
|
|
94
94
|
error = (0, _validateMultifile.default)(value, component.custom_errors);
|
|
95
95
|
break;
|
|
96
96
|
case _models.ComponentTypes.TEXT_AREA:
|
|
97
|
-
error = (0, _validateTextArea.default)(component.label, value, component.showCharacterCount, component.custom_errors, component.maxLength);
|
|
97
|
+
error = (0, _validateTextArea.default)(component.label, value, component.showCharacterCount, component.custom_errors, component.required, component.maxLength);
|
|
98
98
|
break;
|
|
99
99
|
default:
|
|
100
100
|
break;
|
|
@@ -11,15 +11,16 @@ exports.default = void 0;
|
|
|
11
11
|
* @param {*} value The value to validate.
|
|
12
12
|
* @param {string} showCharacterCount The flag to enable character count validation.
|
|
13
13
|
* @param {array} customErrors An array of custom errors for the component.
|
|
14
|
+
* @param {boolean} required If the component is required.
|
|
14
15
|
* @param {number} maxLength The maximum allowable number of characters (by default, this is 1000 characters).
|
|
15
16
|
* @returns An error if the value exceeds the maximum allowable number of characters.
|
|
16
17
|
*/
|
|
17
|
-
const validateTextArea = function (label, value, showCharacterCount, customErrors) {
|
|
18
|
-
let maxLength = arguments.length >
|
|
19
|
-
if (!showCharacterCount) {
|
|
18
|
+
const validateTextArea = function (label, value, showCharacterCount, customErrors, required) {
|
|
19
|
+
let maxLength = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 1000;
|
|
20
|
+
if (!showCharacterCount || !required && !value) {
|
|
20
21
|
return undefined;
|
|
21
22
|
}
|
|
22
|
-
const hasError = !value || value.length > maxLength;
|
|
23
|
+
const hasError = required && !value || value.length > maxLength;
|
|
23
24
|
if (hasError) {
|
|
24
25
|
if (Array.isArray(customErrors)) {
|
|
25
26
|
const result = customErrors.filter(error => error.type === 'length');
|
|
@@ -15,31 +15,53 @@ describe('utils', () => {
|
|
|
15
15
|
}];
|
|
16
16
|
it('should return no error when the character count flag is disabled', () => {
|
|
17
17
|
const VALUE = 'ALPHA BRAVO';
|
|
18
|
-
expect((0, _validateTextArea.default)(LABEL, VALUE, false, CUSTOM_ERRORS, MAX_LENGTH)).toBeUndefined();
|
|
18
|
+
expect((0, _validateTextArea.default)(LABEL, VALUE, false, CUSTOM_ERRORS, true, MAX_LENGTH)).toBeUndefined();
|
|
19
19
|
});
|
|
20
20
|
it('should return no error when value length has not exceeded the maximum allowable character length', () => {
|
|
21
21
|
const VALUE = 'ALPHA BRA';
|
|
22
|
-
expect((0, _validateTextArea.default)(LABEL, VALUE, true, CUSTOM_ERRORS, MAX_LENGTH)).toBeUndefined();
|
|
22
|
+
expect((0, _validateTextArea.default)(LABEL, VALUE, true, CUSTOM_ERRORS, true, MAX_LENGTH)).toBeUndefined();
|
|
23
23
|
});
|
|
24
24
|
it('should return no error when value length has not exceeded the default maximum allowable character length', () => {
|
|
25
25
|
const VALUE = 'ALPHA BRAVO';
|
|
26
|
-
expect((0, _validateTextArea.default)(LABEL, VALUE, true, undefined)).toBeUndefined();
|
|
26
|
+
expect((0, _validateTextArea.default)(LABEL, VALUE, true, undefined, true)).toBeUndefined();
|
|
27
27
|
});
|
|
28
28
|
it('should return an error when value length has exceeded the maximum allowable character length', () => {
|
|
29
29
|
const VALUE = 'ALPHA BRAVO';
|
|
30
|
-
const error = (0, _validateTextArea.default)(LABEL, VALUE, true, undefined, MAX_LENGTH);
|
|
30
|
+
const error = (0, _validateTextArea.default)(LABEL, VALUE, true, undefined, true, MAX_LENGTH);
|
|
31
31
|
expect(error).toBeDefined();
|
|
32
32
|
expect(error).toEqual(ERROR_MSG);
|
|
33
33
|
});
|
|
34
34
|
it('should return a custom error when one is provided', () => {
|
|
35
35
|
const VALUE = 'ALPHA BRAVO CHARLIE';
|
|
36
|
-
const error = (0, _validateTextArea.default)(LABEL, VALUE, true, CUSTOM_ERRORS, MAX_LENGTH);
|
|
36
|
+
const error = (0, _validateTextArea.default)(LABEL, VALUE, true, CUSTOM_ERRORS, true, MAX_LENGTH);
|
|
37
37
|
expect(error).toBeDefined();
|
|
38
38
|
expect(error).toEqual(CUSTOM_ERROR);
|
|
39
39
|
});
|
|
40
40
|
it('Should return a custom error when one is provided and the value is undefined', () => {
|
|
41
41
|
const VALUE = undefined;
|
|
42
|
-
const error = (0, _validateTextArea.default)(LABEL, VALUE, true, CUSTOM_ERRORS, MAX_LENGTH);
|
|
42
|
+
const error = (0, _validateTextArea.default)(LABEL, VALUE, true, CUSTOM_ERRORS, true, MAX_LENGTH);
|
|
43
|
+
expect(error).toBeDefined();
|
|
44
|
+
expect(error).toEqual(CUSTOM_ERROR);
|
|
45
|
+
});
|
|
46
|
+
it('Should return undefined when the component is not required and value is undefined', () => {
|
|
47
|
+
const VALUE = undefined;
|
|
48
|
+
const error = (0, _validateTextArea.default)(LABEL, VALUE, true, CUSTOM_ERRORS, false, MAX_LENGTH);
|
|
49
|
+
expect(error).toBeUndefined();
|
|
50
|
+
});
|
|
51
|
+
it('Should return undefined when component required is undefined and value is undefined', () => {
|
|
52
|
+
const VALUE = undefined;
|
|
53
|
+
const error = (0, _validateTextArea.default)(LABEL, VALUE, true, CUSTOM_ERRORS, undefined, MAX_LENGTH);
|
|
54
|
+
expect(error).toBeUndefined();
|
|
55
|
+
});
|
|
56
|
+
it('Should return error when component required is false and value is over max length', () => {
|
|
57
|
+
const VALUE = 'ALPHA BRAVO CHARLIE';
|
|
58
|
+
const error = (0, _validateTextArea.default)(LABEL, VALUE, true, CUSTOM_ERRORS, false, MAX_LENGTH);
|
|
59
|
+
expect(error).toBeDefined();
|
|
60
|
+
expect(error).toEqual(CUSTOM_ERROR);
|
|
61
|
+
});
|
|
62
|
+
it('Should return error when component is required and value is undefined', () => {
|
|
63
|
+
const VALUE = undefined;
|
|
64
|
+
const error = (0, _validateTextArea.default)(LABEL, VALUE, true, CUSTOM_ERRORS, true, MAX_LENGTH);
|
|
43
65
|
expect(error).toBeDefined();
|
|
44
66
|
expect(error).toEqual(CUSTOM_ERROR);
|
|
45
67
|
});
|