@ukhomeoffice/cop-react-form-renderer 4.24.0 → 4.25.0
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.
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Additional validator for the Container component type.
|
|
10
|
+
* @param {object} data the form data
|
|
11
|
+
* @param {object} config the config for the validation, must contain a property 'base' with the id of the conditionally required component and
|
|
12
|
+
* a property 'check' which if the corresponding component has a value will turn off the required check for 'base'
|
|
13
|
+
* @param {object} component the container component
|
|
14
|
+
* @returns true if the user has entered data into the 'base' field or entered a value into the 'check' field to turn off the validation
|
|
15
|
+
* otherwise returns false
|
|
16
|
+
*/
|
|
17
|
+
var conditionallyRequired = function conditionallyRequired(data, config, component) {
|
|
18
|
+
var _data$component$id, _data$component$id2;
|
|
19
|
+
|
|
20
|
+
var conditional = (_data$component$id = data[component.id]) === null || _data$component$id === void 0 ? void 0 : _data$component$id[config.base];
|
|
21
|
+
var checkFieldvalue = (_data$component$id2 = data[component.id]) === null || _data$component$id2 === void 0 ? void 0 : _data$component$id2[config.check];
|
|
22
|
+
var checkHasValue = checkFieldvalue && (checkFieldvalue === null || checkFieldvalue === void 0 ? void 0 : checkFieldvalue.length) > 0;
|
|
23
|
+
return !!(conditional || checkHasValue);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
var _default = conditionallyRequired;
|
|
27
|
+
exports.default = _default;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _conditionallyRequired = _interopRequireDefault(require("./conditionallyRequired"));
|
|
4
|
+
|
|
5
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
6
|
+
|
|
7
|
+
describe('utils', function () {
|
|
8
|
+
describe('Validate', function () {
|
|
9
|
+
describe('additional', function () {
|
|
10
|
+
describe('conditionallyRequired', function () {
|
|
11
|
+
var CONFIG = {
|
|
12
|
+
base: 'componentOne',
|
|
13
|
+
check: 'componentTwo'
|
|
14
|
+
};
|
|
15
|
+
var COMPONENT = {
|
|
16
|
+
id: 'containerComponent',
|
|
17
|
+
fieldId: 'containerComponent',
|
|
18
|
+
type: 'container',
|
|
19
|
+
components: [{
|
|
20
|
+
id: 'componentOne',
|
|
21
|
+
type: 'text',
|
|
22
|
+
fieldId: 'componentOne'
|
|
23
|
+
}, {
|
|
24
|
+
id: 'componentTwo',
|
|
25
|
+
fieldId: 'componentTwo',
|
|
26
|
+
type: 'checkboxes'
|
|
27
|
+
}]
|
|
28
|
+
};
|
|
29
|
+
test('should pass if only the base field has a value', function () {
|
|
30
|
+
var DATA = {
|
|
31
|
+
containerComponent: {
|
|
32
|
+
componentOne: 'value'
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
expect((0, _conditionallyRequired.default)(DATA, CONFIG, COMPONENT)).toEqual(true);
|
|
36
|
+
});
|
|
37
|
+
test('should pass if the base and check fields have values', function () {
|
|
38
|
+
var DATA = {
|
|
39
|
+
containerComponent: {
|
|
40
|
+
componentOne: 'value',
|
|
41
|
+
componentTwo: 'value'
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
expect((0, _conditionallyRequired.default)(DATA, CONFIG, COMPONENT)).toEqual(true);
|
|
45
|
+
});
|
|
46
|
+
test('should pass if only the check field has a value', function () {
|
|
47
|
+
var DATA = {
|
|
48
|
+
containerComponent: {
|
|
49
|
+
componentTwo: 'value'
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
expect((0, _conditionallyRequired.default)(DATA, CONFIG, COMPONENT)).toEqual(true);
|
|
53
|
+
});
|
|
54
|
+
test('should pass if only the check field has a value and that value is an array', function () {
|
|
55
|
+
var DATA = {
|
|
56
|
+
containerComponent: {
|
|
57
|
+
componentTwo: ['value']
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
expect((0, _conditionallyRequired.default)(DATA, CONFIG, COMPONENT)).toEqual(true);
|
|
61
|
+
});
|
|
62
|
+
test('should fail if neither field has a value', function () {
|
|
63
|
+
var DATA = {
|
|
64
|
+
containerComponent: {}
|
|
65
|
+
};
|
|
66
|
+
expect((0, _conditionallyRequired.default)(DATA, CONFIG, COMPONENT)).toEqual(false);
|
|
67
|
+
});
|
|
68
|
+
test('should fail if there is no data', function () {
|
|
69
|
+
var DATA = {};
|
|
70
|
+
expect((0, _conditionallyRequired.default)(DATA, CONFIG, COMPONENT)).toEqual(false);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
});
|
|
@@ -25,6 +25,8 @@ var _mustBeNumbersOnly = _interopRequireDefault(require("./mustBeNumbersOnly"));
|
|
|
25
25
|
|
|
26
26
|
var _mustBeLessThan = _interopRequireDefault(require("./mustBeLessThan"));
|
|
27
27
|
|
|
28
|
+
var _conditionallyRequired = _interopRequireDefault(require("./conditionallyRequired"));
|
|
29
|
+
|
|
28
30
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
29
31
|
|
|
30
32
|
// Local imports
|
|
@@ -36,6 +38,7 @@ var functions = {
|
|
|
36
38
|
mustBeInTheFuture: _mustBeInTheFuture.default,
|
|
37
39
|
mustBeLongerThan: _mustBeLongerThan.default,
|
|
38
40
|
mustBeShorterThan: _mustBeShorterThan.default,
|
|
41
|
+
conditionallyRequired: _conditionallyRequired.default,
|
|
39
42
|
mustEnterAtLeastOne: _mustEnterAtLeastOne.default,
|
|
40
43
|
mustBeNumbersOnly: _mustBeNumbersOnly.default,
|
|
41
44
|
mustBeLessThan: _mustBeLessThan.default
|