@ukhomeoffice/cop-react-form-renderer 5.0.0-alpha → 5.0.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.
- package/dist/components/CheckYourAnswers/Answer.js +18 -7
- package/dist/components/CheckYourAnswers/Answer.test.js +137 -0
- package/dist/components/CheckYourAnswers/CheckYourAnswers.js +70 -46
- package/dist/components/CheckYourAnswers/CheckYourAnswers.stories.mdx +410 -0
- package/dist/components/CheckYourAnswers/CheckYourAnswers.test.js +551 -0
- package/dist/components/CollectionPage/CollectionPage.js +15 -10
- package/dist/components/CollectionPage/CollectionPage.test.js +382 -0
- package/dist/components/FormComponent/Collection.js +91 -48
- package/dist/components/FormComponent/Collection.scss +2 -1
- package/dist/components/FormComponent/Collection.test.js +807 -0
- package/dist/components/FormComponent/Container.js +35 -16
- package/dist/components/FormComponent/Container.test.js +370 -0
- package/dist/components/FormComponent/FormComponent.js +39 -13
- package/dist/components/FormComponent/FormComponent.stories.mdx +184 -0
- package/dist/components/FormComponent/FormComponent.test.js +226 -0
- package/dist/components/FormComponent/helpers/addLabel.js +5 -4
- package/dist/components/FormComponent/helpers/getComponentFieldSet.js +16 -0
- package/dist/components/FormComponent/helpers/getComponentFieldSet.test.js +29 -0
- package/dist/components/FormComponent/helpers/index.js +8 -1
- package/dist/components/FormPage/FormPage.js +69 -30
- package/dist/components/FormPage/FormPage.stories.mdx +155 -0
- package/dist/components/FormPage/FormPage.test.js +330 -0
- package/dist/components/FormRenderer/FormRenderer.js +142 -228
- package/dist/components/FormRenderer/FormRenderer.stories.mdx +183 -0
- package/dist/components/FormRenderer/FormRenderer.test.js +968 -0
- package/dist/components/FormRenderer/handlers/index.js +1 -2
- package/dist/components/FormRenderer/handlers/navigate.js +3 -1
- package/dist/components/FormRenderer/helpers/cleanHiddenNestedData.js +35 -14
- package/dist/components/FormRenderer/helpers/cleanHiddenNestedData.test.js +88 -5
- package/dist/components/FormRenderer/helpers/getCYA.js +11 -2
- package/dist/components/FormRenderer/helpers/getNextPageId.js +13 -7
- package/dist/components/FormRenderer/helpers/getRelevantPages.js +10 -2
- package/dist/components/FormRenderer/helpers/getRelevantPages.test.js +1 -1
- package/dist/components/FormRenderer/helpers/getSubmissionStatus.js +2 -2
- package/dist/components/FormRenderer/helpers/getSubmissionStatus.test.js +6 -2
- package/dist/components/FormRenderer/helpers/getUpdatedSectionStates.js +171 -51
- package/dist/components/FormRenderer/helpers/getUpdatedSectionStates.test.js +307 -24
- package/dist/components/FormRenderer/helpers/index.js +1 -2
- package/dist/components/FormRenderer/onCYAAction.js +108 -0
- package/dist/components/FormRenderer/onCYAAction.test.js +583 -0
- package/dist/components/FormRenderer/onPageAction.js +145 -0
- package/dist/components/FormRenderer/onPageAction.test.js +678 -0
- package/dist/components/FormRenderer/onTaskAction.js +48 -0
- package/dist/components/FormRenderer/onTaskAction.test.js +217 -0
- package/dist/components/PageActions/ActionButton.js +0 -3
- package/dist/components/PageActions/ActionButton.test.js +93 -0
- package/dist/components/PageActions/PageActions.js +2 -2
- package/dist/components/PageActions/PageActions.stories.mdx +74 -0
- package/dist/components/PageActions/PageActions.test.js +132 -0
- package/dist/components/SummaryList/GroupAction.js +2 -2
- package/dist/components/SummaryList/GroupAction.test.js +80 -0
- package/dist/components/SummaryList/RowAction.js +2 -2
- package/dist/components/SummaryList/RowAction.test.js +80 -0
- package/dist/components/SummaryList/SummaryList.js +29 -12
- package/dist/components/SummaryList/SummaryList.scss +19 -0
- package/dist/components/SummaryList/SummaryList.stories.mdx +90 -0
- package/dist/components/SummaryList/SummaryList.test.js +400 -0
- package/dist/components/SummaryList/SummaryListHeadingRow.js +30 -0
- package/dist/components/SummaryList/SummaryListRow.js +5 -2
- package/dist/components/SummaryList/helpers/getGroupActionAttributes.js +2 -1
- package/dist/components/SummaryList/helpers/getRowActionAttributes.js +2 -1
- package/dist/components/TaskList/Task.js +3 -3
- package/dist/components/TaskList/Task.test.js +167 -0
- package/dist/components/TaskList/TaskList.js +73 -22
- package/dist/components/TaskList/TaskList.stories.mdx +164 -0
- package/dist/components/TaskList/TaskList.test.js +389 -0
- package/dist/components/TaskList/TaskState.js +2 -2
- package/dist/components/TaskList/TaskState.test.js +86 -0
- package/dist/components/index.js +14 -0
- package/dist/context/HooksContext/HooksContext.js +72 -8
- package/dist/context/HooksContext/HooksContext.test.js +44 -0
- package/dist/context/ValidationContext/ValidationContext.js +38 -28
- package/dist/context/ValidationContext/ValidationContext.test.js +84 -0
- package/dist/hooks/useAxios.js +10 -12
- package/dist/hooks/useGetRequest.js +48 -49
- package/dist/hooks/useRefData.js +7 -4
- package/dist/index.js +34 -1
- package/dist/json/addressDetails.json +149 -0
- package/dist/json/areYouACivilServant.json +7 -0
- package/dist/json/firstForm.json +94 -0
- package/dist/json/grade.json +108 -0
- package/dist/json/group.data.json +21 -0
- package/dist/json/group.json +402 -0
- package/dist/json/groupOfRow.json +137 -0
- package/dist/json/groupOfRowData.json +15 -0
- package/dist/json/port.json +346 -0
- package/dist/json/saveAndContinue.json +98 -0
- package/dist/json/sublocation.json +859 -0
- package/dist/json/taskList.json +265 -0
- package/dist/json/team.json +17351 -0
- package/dist/json/terminal.json +81 -0
- package/dist/json/userProfile.data.json +21 -0
- package/dist/json/userProfile.json +276 -0
- package/dist/models/ComponentTypes.js +8 -0
- package/dist/models/PageAction.js +4 -1
- package/dist/models/TaskStates.js +10 -2
- package/dist/models/index.js +16 -1
- package/dist/setupTests.js +46 -0
- package/dist/utils/CheckYourAnswers/getCYAAction.js +5 -5
- package/dist/utils/CheckYourAnswers/getCYAAction.test.js +121 -0
- package/dist/utils/CheckYourAnswers/getCYACollectionChangeAction.js +6 -2
- package/dist/utils/CheckYourAnswers/getCYACollectionChangeAction.test.js +71 -0
- package/dist/utils/CheckYourAnswers/getCYACollectionDeleteAction.js +6 -2
- package/dist/utils/CheckYourAnswers/getCYACollectionDeleteAction.test.js +55 -0
- package/dist/utils/CheckYourAnswers/getCYARow.js +14 -11
- package/dist/utils/CheckYourAnswers/getCYARow.test.js +291 -0
- package/dist/utils/CheckYourAnswers/getCYARowForGroup.js +4 -1
- package/dist/utils/CheckYourAnswers/getCYARowForGroup.test.js +70 -0
- package/dist/utils/CheckYourAnswers/getCYARowsForCollection.js +15 -10
- package/dist/utils/CheckYourAnswers/getCYARowsForCollection.test.js +288 -0
- package/dist/utils/CheckYourAnswers/getCYARowsForCollectionPage.js +94 -36
- package/dist/utils/CheckYourAnswers/getCYARowsForCollectionPage.test.js +414 -0
- package/dist/utils/CheckYourAnswers/getCYARowsForContainer.js +21 -7
- package/dist/utils/CheckYourAnswers/getCYARowsForContainer.test.js +486 -0
- package/dist/utils/CheckYourAnswers/getCYARowsForPage.js +11 -3
- package/dist/utils/CheckYourAnswers/getCYARowsForPage.test.js +346 -0
- package/dist/utils/CheckYourAnswers/showComponentCYA.js +19 -3
- package/dist/utils/CheckYourAnswers/showComponentCYA.test.js +143 -0
- package/dist/utils/CollectionPage/duplicateCollectionPageActiveEntry.js +5 -1
- package/dist/utils/CollectionPage/duplicateCollectionPageActiveEntry.test.js +40 -0
- package/dist/utils/CollectionPage/getCollectionPageActiveIndex.test.js +53 -0
- package/dist/utils/CollectionPage/mergeCollectionPages.js +25 -17
- package/dist/utils/CollectionPage/mergeCollectionPages.test.js +130 -0
- package/dist/utils/Component/addShowWhen.js +44 -0
- package/dist/utils/Component/addShowWhen.test.js +216 -0
- package/dist/utils/Component/applyToComponentTree.js +71 -0
- package/dist/utils/Component/applyToComponentTree.test.js +127 -0
- package/dist/utils/Component/cleanAttributes.js +8 -2
- package/dist/utils/Component/cleanAttributes.test.js +68 -0
- package/dist/utils/Component/elevateNestedComponents.js +4 -1
- package/dist/utils/Component/elevateNestedComponents.test.js +122 -0
- package/dist/utils/Component/getComponent.js +80 -11
- package/dist/utils/Component/getComponentTests/getComponent.autocomplete.test.js +4 -4
- package/dist/utils/Component/getComponentTests/getComponent.calculation.test.js +25 -24
- package/dist/utils/Component/getComponentTests/getComponent.checkboxes.test.js +66 -5
- package/dist/utils/Component/getComponentTests/getComponent.date.test.js +2 -2
- package/dist/utils/Component/getComponentTests/getComponent.details.test.js +9 -7
- package/dist/utils/Component/getComponentTests/getComponent.email.test.js +3 -3
- package/dist/utils/Component/getComponentTests/getComponent.file.test.js +3 -3
- package/dist/utils/Component/getComponentTests/getComponent.list.test.js +44 -0
- package/dist/utils/Component/getComponentTests/{getComponent.textarea.test.js → getComponent.multifile.test.js} +29 -21
- package/dist/utils/Component/getComponentTests/getComponent.nested.test.js +159 -83
- package/dist/utils/Component/getComponentTests/getComponent.paragraph.test.js +43 -0
- package/dist/utils/Component/getComponentTests/getComponent.phoneNumber.test.js +3 -3
- package/dist/utils/Component/getComponentTests/getComponent.radios.test.js +55 -5
- package/dist/utils/Component/getComponentTests/getComponent.select.test.js +3 -9
- package/dist/utils/Component/getComponentTests/getComponent.text.test.js +3 -3
- package/dist/utils/Component/getComponentTests/getComponent.textArea.test.js +109 -0
- package/dist/utils/Component/getComponentTests/getComponent.time.test.js +7 -7
- package/dist/utils/Component/getDefaultValue.js +4 -1
- package/dist/utils/Component/getDefaultValue.test.js +50 -0
- package/dist/utils/Component/index.js +2 -0
- package/dist/utils/Component/isEditable.js +2 -2
- package/dist/utils/Component/isEditable.test.js +43 -0
- package/dist/utils/Component/optionIsSelected.js +22 -0
- package/dist/utils/Component/optionIsSelected.test.js +42 -0
- package/dist/utils/Component/setupContainerComponentsPath.js +40 -0
- package/dist/utils/Component/setupContainerComponentsPath.test.js +78 -0
- package/dist/utils/Component/showComponent.test.js +156 -0
- package/dist/utils/Component/wrapInFormGroup.js +1 -1
- package/dist/utils/Condition/meetsAllConditions.js +1 -1
- package/dist/utils/Condition/meetsAllConditions.test.js +61 -0
- package/dist/utils/Condition/meetsCondition.js +74 -4
- package/dist/utils/Condition/meetsCondition.test.js +763 -0
- package/dist/utils/Condition/meetsOneCondition.js +1 -1
- package/dist/utils/Condition/meetsOneCondition.test.js +100 -0
- package/dist/utils/Condition/setupConditions.js +6 -3
- package/dist/utils/Condition/setupConditions.test.js +33 -0
- package/dist/utils/Container/getEditableComponents.test.js +135 -0
- package/dist/utils/Container/setupNesting.js +21 -2
- package/dist/utils/Container/setupNesting.test.js +145 -0
- package/dist/utils/Container/showContainer.test.js +178 -0
- package/dist/utils/Data/applyFormula.js +20 -11
- package/dist/utils/Data/applyFormula.test.js +263 -0
- package/dist/utils/Data/getAutocompleteSource.js +6 -2
- package/dist/utils/Data/getAutocompleteSource.test.js +352 -0
- package/dist/utils/Data/getDataPath.test.js +48 -0
- package/dist/utils/Data/getOptions.js +17 -3
- package/dist/utils/Data/getOptions.test.js +140 -0
- package/dist/utils/Data/getSourceData.js +2 -2
- package/dist/utils/Data/getSourceData.test.js +153 -0
- package/dist/utils/Data/refDataToOptions.js +4 -1
- package/dist/utils/Data/refDataToOptions.test.js +196 -0
- package/dist/utils/Data/setDataItem.test.js +110 -0
- package/dist/utils/Data/setupFormData.js +39 -16
- package/dist/utils/Data/setupFormData.test.js +410 -0
- package/dist/utils/Data/setupRefDataUrlForComponent.js +4 -1
- package/dist/utils/Data/setupRefDataUrlForComponent.test.js +131 -0
- package/dist/utils/FormPage/applyConditionalProperties.js +35 -0
- package/dist/utils/FormPage/applyConditionalProperties.test.js +61 -0
- package/dist/utils/FormPage/getFormPage.js +5 -2
- package/dist/utils/FormPage/getFormPage.test.js +206 -0
- package/dist/utils/FormPage/getFormPages.js +4 -1
- package/dist/utils/FormPage/getFormPages.test.js +97 -0
- package/dist/utils/FormPage/getPageActions.js +8 -4
- package/dist/utils/FormPage/getPageActions.test.js +114 -0
- package/dist/utils/FormPage/getParagraphFromText.test.js +27 -0
- package/dist/utils/FormPage/index.js +2 -0
- package/dist/utils/FormPage/showFormPage.js +2 -2
- package/dist/utils/FormPage/showFormPage.test.js +180 -0
- package/dist/utils/FormPage/showFormPageCYA.js +1 -0
- package/dist/utils/FormPage/showFormPageCYA.test.js +28 -0
- package/dist/utils/FormPage/useComponent.js +29 -14
- package/dist/utils/FormPage/useComponent.test.js +167 -0
- package/dist/utils/Format/formatData.test.js +45 -0
- package/dist/utils/Format/formatDataForComponent.js +2 -1
- package/dist/utils/Format/formatDataForComponent.test.js +142 -0
- package/dist/utils/Format/formatDataForForm.js +4 -1
- package/dist/utils/Format/formatDataForForm.test.js +81 -0
- package/dist/utils/Format/formatDataForPage.test.js +99 -0
- package/dist/utils/Hub/getFormHub.test.js +105 -0
- package/dist/utils/Meta/documents/getDocuments.test.js +4 -1
- package/dist/utils/Meta/documents/index.js +2 -2
- package/dist/utils/Meta/documents/setDocumentsForField.js +31 -0
- package/dist/utils/Meta/documents/setDocumentsForField.test.js +59 -0
- package/dist/utils/Operate/checkValueIsTruthy.test.js +42 -0
- package/dist/utils/Operate/getFirstOf.js +31 -0
- package/dist/utils/Operate/getFirstOf.test.js +86 -0
- package/dist/utils/Operate/getIndexOfMatchingValueIn.test.js +163 -0
- package/dist/utils/Operate/persistValueInFormData.js +2 -1
- package/dist/utils/Operate/persistValueInFormData.test.js +115 -0
- package/dist/utils/Operate/runPageOperations.js +3 -1
- package/dist/utils/Operate/runPageOperations.test.js +105 -0
- package/dist/utils/Operate/setValueInFormData.test.js +44 -0
- package/dist/utils/Operate/shouldRun.js +16 -13
- package/dist/utils/Operate/shouldRun.test.js +66 -0
- package/dist/utils/Validate/additional/conditionallyRequired.js +24 -0
- package/dist/utils/Validate/additional/conditionallyRequired.test.js +73 -0
- package/dist/utils/Validate/additional/index.js +13 -4
- package/dist/utils/Validate/additional/mustBeAfter.js +8 -6
- package/dist/utils/Validate/additional/mustBeAfter.test.js +15 -1
- package/dist/utils/Validate/additional/mustBeBefore.js +9 -5
- package/dist/utils/Validate/additional/mustBeBefore.test.js +14 -0
- package/dist/utils/Validate/additional/mustBeEarlierDateTime.js +1 -1
- package/dist/utils/Validate/additional/mustBeEarlierDateTime.test.js +6 -3
- package/dist/utils/Validate/additional/mustBeGreaterThan.js +22 -0
- package/dist/utils/Validate/additional/mustBeGreaterThan.test.js +56 -0
- package/dist/utils/Validate/additional/mustBeInTheFuture.js +1 -1
- package/dist/utils/Validate/additional/mustBeInThePast.js +5 -5
- package/dist/utils/Validate/additional/mustBeLessThan.js +7 -6
- package/dist/utils/Validate/additional/mustBeLessThan.test.js +11 -1
- package/dist/utils/Validate/additional/mustBeNumbersOnly.js +9 -5
- package/dist/utils/Validate/additional/mustBeNumbersOnly.test.js +10 -2
- package/dist/utils/Validate/additional/mustEnterAtLeastOne.js +1 -0
- package/dist/utils/Validate/additional/mustEnterAtLeastOne.test.js +4 -1
- package/dist/utils/Validate/additional/mustHaveLessThanDecimalPlaces.js +19 -0
- package/dist/utils/Validate/additional/mustHaveLessThanDecimalPlaces.test.js +36 -0
- package/dist/utils/Validate/additional/mustSelectOnlyOne.js +29 -0
- package/dist/utils/Validate/additional/mustSelectOnlyOne.test.js +52 -0
- package/dist/utils/Validate/additional/utils.js +18 -18
- package/dist/utils/Validate/index.js +6 -0
- package/dist/utils/Validate/validateCollection.js +20 -5
- package/dist/utils/Validate/validateCollection.test.js +125 -0
- package/dist/utils/Validate/validateComponent.js +36 -20
- package/dist/utils/Validate/validateComponent.test.js +365 -0
- package/dist/utils/Validate/validateContainer.js +9 -3
- package/dist/utils/Validate/validateContainer.test.js +75 -0
- package/dist/utils/Validate/validateDate.js +4 -3
- package/dist/utils/Validate/validateDate.test.js +114 -0
- package/dist/utils/Validate/validateEmail.js +14 -1
- package/dist/utils/Validate/validateEmail.test.js +74 -0
- package/dist/utils/Validate/validateMultifile.js +32 -0
- package/dist/utils/Validate/validateMultifile.test.js +47 -0
- package/dist/utils/Validate/validatePage.js +12 -6
- package/dist/utils/Validate/validatePage.test.js +472 -0
- package/dist/utils/Validate/validateRegex.js +2 -5
- package/dist/utils/Validate/validateRegex.test.js +42 -0
- package/dist/utils/Validate/validateRequired.js +2 -4
- package/dist/utils/Validate/validateRequired.test.js +63 -0
- package/dist/utils/Validate/validateTextArea.js +37 -0
- package/dist/utils/Validate/validateTextArea.test.js +42 -0
- package/dist/utils/Validate/validateTime.js +2 -2
- package/dist/utils/Validate/validateTime.test.js +59 -0
- package/dist/utils/index.js +4 -1
- package/package.json +35 -50
- package/dist/utils/Meta/documents/setDocumentForField.js +0 -29
- package/dist/utils/Meta/documents/setDocumentForField.test.js +0 -49
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _models = require("../../models");
|
|
4
|
+
var _validateComponent = _interopRequireDefault(require("./validateComponent"));
|
|
5
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
6
|
+
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
7
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
8
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
9
|
+
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
10
|
+
describe('utils.Validate.Component', function () {
|
|
11
|
+
var setup = function setup(id, type, label, required, additionalValidation) {
|
|
12
|
+
return {
|
|
13
|
+
id: id,
|
|
14
|
+
fieldId: id,
|
|
15
|
+
type: type,
|
|
16
|
+
label: label,
|
|
17
|
+
required: required,
|
|
18
|
+
additionalValidation: additionalValidation
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
it('should return no error when the component is null', function () {
|
|
22
|
+
expect((0, _validateComponent.default)(null, {})).toBeUndefined();
|
|
23
|
+
});
|
|
24
|
+
describe('when there is no form data', function () {
|
|
25
|
+
it('should return no error when the component is not required and not an email type', function () {
|
|
26
|
+
var ID = 'field';
|
|
27
|
+
var LABEL = 'Field';
|
|
28
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, false);
|
|
29
|
+
expect((0, _validateComponent.default)(COMPONENT, null)).toBeUndefined();
|
|
30
|
+
});
|
|
31
|
+
it('should return a required error when the component is required and not an email type', function () {
|
|
32
|
+
var ID = 'field';
|
|
33
|
+
var LABEL = 'Field';
|
|
34
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, true);
|
|
35
|
+
expect((0, _validateComponent.default)(COMPONENT, null)).toEqual({
|
|
36
|
+
id: ID,
|
|
37
|
+
error: "".concat(LABEL, " is required")
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
it('should return a required error when the component is required and of type email', function () {
|
|
41
|
+
var ID = 'field';
|
|
42
|
+
var LABEL = 'Field';
|
|
43
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, true);
|
|
44
|
+
expect((0, _validateComponent.default)(COMPONENT, null)).toEqual({
|
|
45
|
+
id: ID,
|
|
46
|
+
error: "".concat(LABEL, " is required")
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
it('should return no error when the component is of type email but not required', function () {
|
|
50
|
+
var ID = 'field';
|
|
51
|
+
var LABEL = 'Field';
|
|
52
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, false);
|
|
53
|
+
expect((0, _validateComponent.default)(COMPONENT, null)).toBeUndefined();
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
describe('when the value is fully valid', function () {
|
|
57
|
+
var ID = 'field';
|
|
58
|
+
var DATA = _defineProperty({}, ID, 'alpha.bravo@digital.homeoffice.gov.uk');
|
|
59
|
+
it('should return no error when the component is not required and not an email type', function () {
|
|
60
|
+
var LABEL = 'Field';
|
|
61
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, false);
|
|
62
|
+
expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
|
|
63
|
+
});
|
|
64
|
+
it('should return no error when the component is required and not an email type', function () {
|
|
65
|
+
var LABEL = 'Field';
|
|
66
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, true);
|
|
67
|
+
expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
|
|
68
|
+
});
|
|
69
|
+
it('should return no error when the component is required and of type email', function () {
|
|
70
|
+
var LABEL = 'Field';
|
|
71
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, true);
|
|
72
|
+
expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
|
|
73
|
+
});
|
|
74
|
+
it('should return no error when the component is of type email but not required', function () {
|
|
75
|
+
var LABEL = 'Field';
|
|
76
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, false);
|
|
77
|
+
expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
describe('when the value is an invalid email', function () {
|
|
81
|
+
var ID = 'field';
|
|
82
|
+
var DATA = _defineProperty({}, ID, 'alpha.bravo@hotmail.com');
|
|
83
|
+
it('should return no error when the component is not required and not an email type', function () {
|
|
84
|
+
var LABEL = 'Field';
|
|
85
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, false);
|
|
86
|
+
expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
|
|
87
|
+
});
|
|
88
|
+
it('should return no error when the component is required and not an email type', function () {
|
|
89
|
+
var LABEL = 'Field';
|
|
90
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.TEXT, LABEL, true);
|
|
91
|
+
expect((0, _validateComponent.default)(COMPONENT, DATA)).toBeUndefined();
|
|
92
|
+
});
|
|
93
|
+
it('should return no error when the component is required and of type email', function () {
|
|
94
|
+
var LABEL = 'Field';
|
|
95
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, true);
|
|
96
|
+
expect((0, _validateComponent.default)(COMPONENT, DATA)).toEqual({
|
|
97
|
+
id: ID,
|
|
98
|
+
error: "Enter ".concat(LABEL.toLowerCase(), " in the correct format, like jane.doe@homeoffice.gov.uk")
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
it('should return no error when the component is of type email but not required', function () {
|
|
102
|
+
var LABEL = 'Field';
|
|
103
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.EMAIL, LABEL, false);
|
|
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
|
+
});
|
|
110
|
+
describe('when the component is a Date Input', function () {
|
|
111
|
+
var ID = 'field';
|
|
112
|
+
it('should always reject invalid dates', function () {
|
|
113
|
+
var LABEL = 'Field';
|
|
114
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.DATE, LABEL, false);
|
|
115
|
+
var DATA = _defineProperty({}, ID, '25-45-2033');
|
|
116
|
+
expect((0, _validateComponent.default)(COMPONENT, DATA)).toEqual({
|
|
117
|
+
error: 'Month must be between 1 and 12',
|
|
118
|
+
id: ID,
|
|
119
|
+
properties: {
|
|
120
|
+
month: true
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
it('should apply optional validators when specified', function () {
|
|
125
|
+
var LABEL = 'Field';
|
|
126
|
+
var DATA = _defineProperty({}, ID, '25-3-3033');
|
|
127
|
+
var ADDITIONAL_VALIDATION = [{
|
|
128
|
+
function: 'mustBeBefore',
|
|
129
|
+
value: 3,
|
|
130
|
+
unit: 'day',
|
|
131
|
+
message: 'Date must be less than 3 days in the future'
|
|
132
|
+
}];
|
|
133
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.DATE, LABEL, false, ADDITIONAL_VALIDATION);
|
|
134
|
+
expect((0, _validateComponent.default)(COMPONENT, DATA)).toEqual({
|
|
135
|
+
error: 'Date must be less than 3 days in the future',
|
|
136
|
+
id: ID,
|
|
137
|
+
properties: {
|
|
138
|
+
day: true,
|
|
139
|
+
month: true,
|
|
140
|
+
year: true
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
describe('when the component is a Time Input', function () {
|
|
146
|
+
var ID = 'field';
|
|
147
|
+
it('should always reject invalid time', function () {
|
|
148
|
+
var LABEL = 'Field';
|
|
149
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.TIME, LABEL, false);
|
|
150
|
+
var DATA = _defineProperty({}, ID, '25:45');
|
|
151
|
+
expect((0, _validateComponent.default)(COMPONENT, DATA)).toEqual({
|
|
152
|
+
error: 'Hour must be between 0 and 23',
|
|
153
|
+
id: ID,
|
|
154
|
+
properties: {
|
|
155
|
+
hour: true
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
describe('when the component is a container', function () {
|
|
161
|
+
it('should return an empty array when the container has only valid components', function () {
|
|
162
|
+
var EMAIL_ID = 'email';
|
|
163
|
+
var EMAIL_LABEL = 'Email';
|
|
164
|
+
var EMAIL = setup(EMAIL_ID, _models.ComponentTypes.EMAIL, EMAIL_LABEL, false);
|
|
165
|
+
var ID = 'container';
|
|
166
|
+
var LABEL = 'field';
|
|
167
|
+
var CONTAINER = setup(ID, _models.ComponentTypes.CONTAINER, LABEL, false);
|
|
168
|
+
CONTAINER.components = [EMAIL];
|
|
169
|
+
var DATA = _defineProperty({}, ID, _defineProperty({}, EMAIL_ID, 'alpha.bravo@digital.homeoffice.gov.uk'));
|
|
170
|
+
expect((0, _validateComponent.default)(CONTAINER, DATA)).toEqual([]);
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
describe('when the component is a collection', function () {
|
|
174
|
+
it('should return an empty array when the collection has only valid items', function () {
|
|
175
|
+
var EMAIL_ID = 'email';
|
|
176
|
+
var EMAIL_LABEL = 'Email';
|
|
177
|
+
var EMAIL = setup(EMAIL_ID, _models.ComponentTypes.EMAIL, EMAIL_LABEL, false);
|
|
178
|
+
var ID = 'collection';
|
|
179
|
+
var LABEL = 'field';
|
|
180
|
+
var COLLECTION = setup(ID, _models.ComponentTypes.COLLECTION, LABEL, false);
|
|
181
|
+
COLLECTION.item = [EMAIL];
|
|
182
|
+
var DATA = _defineProperty({}, ID, [_defineProperty({}, EMAIL_ID, 'alpha.bravo@homeoffice.gov.uk'), _defineProperty({}, EMAIL_ID, 'charlie.delta@homeoffice.gov.uk')]);
|
|
183
|
+
expect((0, _validateComponent.default)(COLLECTION, DATA)).toEqual([]);
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
describe('when the component has a nested component', function () {
|
|
187
|
+
it('should return no error when the radio component contains nested components without errors', function () {
|
|
188
|
+
var NESTED_ID = 'nestedId';
|
|
189
|
+
var NESTED_VALUE = 'nestedValue';
|
|
190
|
+
var FORMDATA = _defineProperty({}, NESTED_ID, NESTED_VALUE);
|
|
191
|
+
var COMPONENT = {
|
|
192
|
+
type: 'radios',
|
|
193
|
+
id: 'a',
|
|
194
|
+
data: {
|
|
195
|
+
options: [{
|
|
196
|
+
nested: [{
|
|
197
|
+
type: 'text',
|
|
198
|
+
fieldId: NESTED_ID,
|
|
199
|
+
id: NESTED_ID,
|
|
200
|
+
required: true
|
|
201
|
+
}]
|
|
202
|
+
}]
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
expect((0, _validateComponent.default)(COMPONENT, undefined, FORMDATA)).toBeUndefined();
|
|
206
|
+
});
|
|
207
|
+
it('should return an error when the radio component contains nested components with errors', function () {
|
|
208
|
+
var NESTED_ID = 'nestedId';
|
|
209
|
+
var FORMDATA = {};
|
|
210
|
+
var COMPONENT = {
|
|
211
|
+
type: 'radios',
|
|
212
|
+
id: 'a',
|
|
213
|
+
data: {
|
|
214
|
+
options: [{
|
|
215
|
+
nested: [{
|
|
216
|
+
type: 'text',
|
|
217
|
+
fieldId: NESTED_ID,
|
|
218
|
+
id: NESTED_ID,
|
|
219
|
+
required: true
|
|
220
|
+
}]
|
|
221
|
+
}]
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
expect((0, _validateComponent.default)(COMPONENT, undefined, FORMDATA)).toEqual([{
|
|
225
|
+
id: "nestedId",
|
|
226
|
+
error: "Field is required"
|
|
227
|
+
}]);
|
|
228
|
+
});
|
|
229
|
+
it('should return no error when a non selected radio component contains nested components with errors', function () {
|
|
230
|
+
var NESTED_ID = 'nestedId';
|
|
231
|
+
var COMPONENT = {
|
|
232
|
+
type: 'radios',
|
|
233
|
+
id: 'a',
|
|
234
|
+
data: {
|
|
235
|
+
options: [{
|
|
236
|
+
value: 'optionValue',
|
|
237
|
+
nested: [{
|
|
238
|
+
type: 'text',
|
|
239
|
+
fieldId: NESTED_ID,
|
|
240
|
+
id: NESTED_ID,
|
|
241
|
+
required: true
|
|
242
|
+
}]
|
|
243
|
+
}]
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
expect((0, _validateComponent.default)(COMPONENT, undefined, {})).toBeUndefined();
|
|
247
|
+
});
|
|
248
|
+
it('should return no error when the checkbox component contains nested components without errors', function () {
|
|
249
|
+
var NESTED_ID = 'nestedId';
|
|
250
|
+
var NESTED_VALUE = 'nestedValue';
|
|
251
|
+
var FORMDATA = _defineProperty({}, NESTED_ID, NESTED_VALUE);
|
|
252
|
+
var COMPONENT = {
|
|
253
|
+
type: 'checkboxes',
|
|
254
|
+
id: 'a',
|
|
255
|
+
data: {
|
|
256
|
+
options: [{
|
|
257
|
+
nested: [{
|
|
258
|
+
type: 'text',
|
|
259
|
+
fieldId: NESTED_ID,
|
|
260
|
+
id: NESTED_ID,
|
|
261
|
+
required: true
|
|
262
|
+
}]
|
|
263
|
+
}]
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
expect((0, _validateComponent.default)(COMPONENT, undefined, FORMDATA)).toBeUndefined();
|
|
267
|
+
});
|
|
268
|
+
it('should return an error when the checkbox component contains nested components with errors', function () {
|
|
269
|
+
var NESTED_ID = 'nestedId';
|
|
270
|
+
var OPTION_VALUE = 'optionValue';
|
|
271
|
+
var FORMDATA = {
|
|
272
|
+
'a': [OPTION_VALUE]
|
|
273
|
+
};
|
|
274
|
+
var COMPONENT = {
|
|
275
|
+
type: 'checkboxes',
|
|
276
|
+
id: 'a',
|
|
277
|
+
data: {
|
|
278
|
+
options: [{
|
|
279
|
+
value: OPTION_VALUE,
|
|
280
|
+
nested: [{
|
|
281
|
+
type: 'text',
|
|
282
|
+
fieldId: NESTED_ID,
|
|
283
|
+
id: NESTED_ID,
|
|
284
|
+
required: true
|
|
285
|
+
}]
|
|
286
|
+
}]
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
expect((0, _validateComponent.default)(COMPONENT, undefined, FORMDATA)).toEqual([{
|
|
290
|
+
id: NESTED_ID,
|
|
291
|
+
error: "Field is required"
|
|
292
|
+
}]);
|
|
293
|
+
});
|
|
294
|
+
it('should return no error when a non selected checkbox component contains nested components with errors', function () {
|
|
295
|
+
var NESTED_ID_NOT_SELECTED = 'nestedIdNotSelected';
|
|
296
|
+
var NESTED_ID_SELECTED = 'nestedIdSelected';
|
|
297
|
+
var NESTED_VALUE_SELECTED = 'nestedValueSelected';
|
|
298
|
+
var FORMDATA = _defineProperty({
|
|
299
|
+
'a': ['optionValueSelected']
|
|
300
|
+
}, NESTED_ID_SELECTED, NESTED_VALUE_SELECTED);
|
|
301
|
+
var COMPONENT = {
|
|
302
|
+
type: 'checkboxes',
|
|
303
|
+
id: 'a',
|
|
304
|
+
data: {
|
|
305
|
+
options: [{
|
|
306
|
+
id: 'optionIdNotSelected',
|
|
307
|
+
value: 'optionValueNotSelected',
|
|
308
|
+
nested: [{
|
|
309
|
+
type: 'text',
|
|
310
|
+
fieldId: NESTED_ID_NOT_SELECTED,
|
|
311
|
+
id: NESTED_ID_NOT_SELECTED,
|
|
312
|
+
required: true
|
|
313
|
+
}]
|
|
314
|
+
}, {
|
|
315
|
+
id: 'optionIdSelected',
|
|
316
|
+
value: 'optionValueSelected',
|
|
317
|
+
nested: [{
|
|
318
|
+
type: 'text',
|
|
319
|
+
fieldId: NESTED_ID_SELECTED,
|
|
320
|
+
id: NESTED_ID_SELECTED,
|
|
321
|
+
required: true
|
|
322
|
+
}]
|
|
323
|
+
}]
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
expect((0, _validateComponent.default)(COMPONENT, undefined, FORMDATA)).toBeUndefined();
|
|
327
|
+
});
|
|
328
|
+
it('should return no error when all selected checkbox components contains nested components without errors', function () {
|
|
329
|
+
var _FORMDATA4;
|
|
330
|
+
var NESTED_ID_SELECTED_1 = 'nestedIdSelected1';
|
|
331
|
+
var NESTED_ID_SELECTED_2 = 'nestedIdSelected2';
|
|
332
|
+
var NESTED_VALUE_SELECTED_1 = 'nestedValueSelected1';
|
|
333
|
+
var NESTED_VALUE_SELECTED_2 = 'nestedValueSelected2';
|
|
334
|
+
var FORMDATA = (_FORMDATA4 = {
|
|
335
|
+
'a': ['optionValueSelected1', 'optionValueSelected2']
|
|
336
|
+
}, _defineProperty(_FORMDATA4, NESTED_ID_SELECTED_1, NESTED_VALUE_SELECTED_1), _defineProperty(_FORMDATA4, NESTED_ID_SELECTED_2, NESTED_VALUE_SELECTED_2), _FORMDATA4);
|
|
337
|
+
var COMPONENT = {
|
|
338
|
+
type: 'checkboxes',
|
|
339
|
+
id: 'a',
|
|
340
|
+
data: {
|
|
341
|
+
options: [{
|
|
342
|
+
id: 'optionIdSelected1',
|
|
343
|
+
value: 'optionValueSelected1',
|
|
344
|
+
nested: [{
|
|
345
|
+
type: 'text',
|
|
346
|
+
fieldId: NESTED_ID_SELECTED_1,
|
|
347
|
+
id: NESTED_ID_SELECTED_1,
|
|
348
|
+
required: true
|
|
349
|
+
}]
|
|
350
|
+
}, {
|
|
351
|
+
id: 'optionIdSelected2',
|
|
352
|
+
value: 'optionValueSelected2',
|
|
353
|
+
nested: [{
|
|
354
|
+
type: 'text',
|
|
355
|
+
fieldId: NESTED_ID_SELECTED_2,
|
|
356
|
+
id: NESTED_ID_SELECTED_2,
|
|
357
|
+
required: true
|
|
358
|
+
}]
|
|
359
|
+
}]
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
expect((0, _validateComponent.default)(COMPONENT, undefined, FORMDATA)).toBeUndefined();
|
|
363
|
+
});
|
|
364
|
+
});
|
|
365
|
+
});
|
|
@@ -7,9 +7,12 @@ exports.default = void 0;
|
|
|
7
7
|
var _validateComponent = _interopRequireDefault(require("./validateComponent"));
|
|
8
8
|
var _additional = _interopRequireDefault(require("./additional"));
|
|
9
9
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
+
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
10
11
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
11
12
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
12
|
-
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; }
|
|
13
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
14
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
15
|
+
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
13
16
|
/**
|
|
14
17
|
* Validates all components within a container.
|
|
15
18
|
* @param {object} container The container to validate.
|
|
@@ -33,9 +36,12 @@ var validateContainer = function validateContainer(container, outerData, formDat
|
|
|
33
36
|
if (Array.isArray(container.components)) {
|
|
34
37
|
var containerData = outerData && container.fieldId ? outerData[container.fieldId] : outerData;
|
|
35
38
|
container.components.forEach(function (component) {
|
|
36
|
-
|
|
39
|
+
// It is possible that the container being passed in is a
|
|
40
|
+
// dummy container just for validation. In that scenario we want
|
|
41
|
+
// to ignore the full_path and just use the component's fieldId.
|
|
42
|
+
var fullPath = container.full_path || container.fieldId ? "".concat(container.full_path || container.fieldId, ".").concat(component.fieldId) : component.fieldId;
|
|
37
43
|
errors.push((0, _validateComponent.default)(_objectSpread(_objectSpread({}, component), {}, {
|
|
38
|
-
full_path:
|
|
44
|
+
full_path: fullPath
|
|
39
45
|
}), containerData, fd));
|
|
40
46
|
});
|
|
41
47
|
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _models = require("../../models");
|
|
4
|
+
var _validateContainer = _interopRequireDefault(require("./validateContainer"));
|
|
5
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
6
|
+
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
7
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
8
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
9
|
+
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
10
|
+
describe('utils.Validate.Container', function () {
|
|
11
|
+
var setup = function setup(id, type, label, required, additionalValidation) {
|
|
12
|
+
return {
|
|
13
|
+
id: id,
|
|
14
|
+
fieldId: id,
|
|
15
|
+
type: type,
|
|
16
|
+
label: label,
|
|
17
|
+
required: required,
|
|
18
|
+
additionalValidation: additionalValidation
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
it('should return an empty array when the component is null', function () {
|
|
22
|
+
expect((0, _validateContainer.default)(null, {})).toEqual([]);
|
|
23
|
+
});
|
|
24
|
+
it('should return an empty array when the container has an undefined components array', function () {
|
|
25
|
+
var ID = 'container';
|
|
26
|
+
var LABEL = 'field';
|
|
27
|
+
var CONTAINER = setup(ID, _models.ComponentTypes.CONTAINER, LABEL, false);
|
|
28
|
+
expect((0, _validateContainer.default)(CONTAINER, null)).toEqual([]);
|
|
29
|
+
});
|
|
30
|
+
it('should return an empty array when the container has no components', function () {
|
|
31
|
+
var ID = 'container';
|
|
32
|
+
var LABEL = 'field';
|
|
33
|
+
var CONTAINER = setup(ID, _models.ComponentTypes.CONTAINER, LABEL, false);
|
|
34
|
+
CONTAINER.components = [];
|
|
35
|
+
expect((0, _validateContainer.default)(CONTAINER, null)).toEqual([]);
|
|
36
|
+
});
|
|
37
|
+
it('should return an empty array when the container has only valid components', function () {
|
|
38
|
+
var EMAIL_ID = 'email';
|
|
39
|
+
var EMAIL_LABEL = 'Email';
|
|
40
|
+
var EMAIL = setup(EMAIL_ID, _models.ComponentTypes.EMAIL, EMAIL_LABEL, false);
|
|
41
|
+
var ID = 'container';
|
|
42
|
+
var LABEL = 'field';
|
|
43
|
+
var CONTAINER = setup(ID, _models.ComponentTypes.CONTAINER, LABEL, false);
|
|
44
|
+
CONTAINER.components = [EMAIL];
|
|
45
|
+
var DATA = _defineProperty({}, ID, _defineProperty({}, EMAIL_ID, 'alpha.bravo@digital.homeoffice.gov.uk'));
|
|
46
|
+
expect((0, _validateContainer.default)(CONTAINER, DATA)).toEqual([]);
|
|
47
|
+
});
|
|
48
|
+
it('should return an array containing an error when the container has an invalid component', function () {
|
|
49
|
+
var EMAIL_ID = 'email';
|
|
50
|
+
var EMAIL_LABEL = 'Email';
|
|
51
|
+
var EMAIL = setup(EMAIL_ID, _models.ComponentTypes.EMAIL, EMAIL_LABEL, false);
|
|
52
|
+
var ID = 'container';
|
|
53
|
+
var LABEL = 'field';
|
|
54
|
+
var CONTAINER = setup(ID, _models.ComponentTypes.CONTAINER, LABEL, false);
|
|
55
|
+
CONTAINER.components = [EMAIL];
|
|
56
|
+
var DATA = _defineProperty({}, ID, _defineProperty({}, EMAIL_ID, 'alpha.bravo@digital.homeoffice.com'));
|
|
57
|
+
expect((0, _validateContainer.default)(CONTAINER, DATA)).toEqual([{
|
|
58
|
+
id: "".concat(ID, ".").concat(EMAIL_ID),
|
|
59
|
+
error: "Enter ".concat(EMAIL_LABEL.toLowerCase(), " in the correct format, like jane.doe@homeoffice.gov.uk")
|
|
60
|
+
}]);
|
|
61
|
+
});
|
|
62
|
+
it('should apply optional validators when specified', function () {
|
|
63
|
+
var ID = 'container';
|
|
64
|
+
var DATA = {};
|
|
65
|
+
var ADDITIONAL_VALIDATION = [{
|
|
66
|
+
function: 'mustEnterAtLeastOne',
|
|
67
|
+
message: 'Must enter at least one'
|
|
68
|
+
}];
|
|
69
|
+
var COMPONENT = setup(ID, _models.ComponentTypes.DATE, null, false, ADDITIONAL_VALIDATION);
|
|
70
|
+
expect((0, _validateContainer.default)(COMPONENT, DATA)).toEqual({
|
|
71
|
+
error: 'Must enter at least one',
|
|
72
|
+
id: ID
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
});
|
|
@@ -13,8 +13,8 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
13
13
|
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
14
14
|
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
15
15
|
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
16
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++)
|
|
17
|
-
function _iterableToArrayLimit(arr, i) { var _i =
|
|
16
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
17
|
+
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
|
|
18
18
|
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
19
19
|
_dayjs.default.extend(_customParseFormat.default);
|
|
20
20
|
_dayjs.default.extend(_isToday.default);
|
|
@@ -22,7 +22,8 @@ _dayjs.default.extend(_isLeapYear.default);
|
|
|
22
22
|
var maxMonthDays = function maxMonthDays(month, year) {
|
|
23
23
|
if (month === '02') {
|
|
24
24
|
return (0, _dayjs.default)().year(year).isLeapYear() ? 29 : 28;
|
|
25
|
-
}
|
|
25
|
+
}
|
|
26
|
+
if (['04', '06', '09', '11'].includes(month)) {
|
|
26
27
|
return 30;
|
|
27
28
|
}
|
|
28
29
|
return 31;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
4
|
+
var _validateDate = _interopRequireWildcard(require("./validateDate"));
|
|
5
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
6
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
7
|
+
describe('utils', function () {
|
|
8
|
+
describe('Validate', function () {
|
|
9
|
+
describe('date', function () {
|
|
10
|
+
test('should return no error when the value is an empty string', function () {
|
|
11
|
+
expect((0, _validateDate.default)('', 'DD-MM-YYYY')).toEqual({
|
|
12
|
+
message: undefined,
|
|
13
|
+
propsInError: undefined
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
test('should return undefined if a date string is valid', function () {
|
|
17
|
+
var output = (0, _validateDate.default)('28-02-2024', 'DD-MM-YYYY');
|
|
18
|
+
expect(output).toEqual({
|
|
19
|
+
message: undefined,
|
|
20
|
+
propsInError: undefined
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
test('should return undefined to a correct leap year date', function () {
|
|
24
|
+
var output = (0, _validateDate.default)('29-02-2024', 'DD-MM-YYYY');
|
|
25
|
+
expect(output).toEqual({
|
|
26
|
+
message: undefined,
|
|
27
|
+
propsInError: undefined
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
test('should validate false if NOT a leap year & 29 Feb is entered', function () {
|
|
31
|
+
var output = (0, _validateDate.default)('29-02-2023', 'DD-MM-YYYY');
|
|
32
|
+
expect(output).toEqual({
|
|
33
|
+
message: 'Day must be between 1 and 28',
|
|
34
|
+
propsInError: {
|
|
35
|
+
day: true
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
test('should return an error if no year is given', function () {
|
|
40
|
+
var output = (0, _validateDate.default)('29-02-', 'DD-MM-YYYY');
|
|
41
|
+
expect(output).toEqual({
|
|
42
|
+
message: 'Date must include a year',
|
|
43
|
+
propsInError: {
|
|
44
|
+
year: true
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
test('should return an error if the year contains less than 4 numbers', function () {
|
|
49
|
+
var output = (0, _validateDate.default)('29-02-20', 'DD-MM-YYYY');
|
|
50
|
+
expect(output).toEqual({
|
|
51
|
+
message: 'Year must be 4 numbers',
|
|
52
|
+
propsInError: {
|
|
53
|
+
year: true
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
test('should return an error if the year contains more than 4 numbers', function () {
|
|
58
|
+
var output = (0, _validateDate.default)('29-02-20202', 'DD-MM-YYYY');
|
|
59
|
+
expect(output).toEqual({
|
|
60
|
+
message: 'Year must be 4 numbers',
|
|
61
|
+
propsInError: {
|
|
62
|
+
year: true
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
test('should return an error if no month is given', function () {
|
|
67
|
+
var output = (0, _validateDate.default)('29--2020', 'DD-MM-YYYY');
|
|
68
|
+
expect(output).toEqual({
|
|
69
|
+
message: 'Date must include a month',
|
|
70
|
+
propsInError: {
|
|
71
|
+
month: true
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
test('should return an error if the month is not between 1 and 12', function () {
|
|
76
|
+
var output = (0, _validateDate.default)('29-14-2020', 'DD-MM-YYYY');
|
|
77
|
+
expect(output).toEqual({
|
|
78
|
+
message: 'Month must be between 1 and 12',
|
|
79
|
+
propsInError: {
|
|
80
|
+
month: true
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
test('should return an error if no day is given', function () {
|
|
85
|
+
var output = (0, _validateDate.default)('-03-2020', 'DD-MM-YYYY');
|
|
86
|
+
expect(output).toEqual({
|
|
87
|
+
message: 'Date must include a day',
|
|
88
|
+
propsInError: {
|
|
89
|
+
day: true
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
test('should return an error if the day is not between 1 and 31', function () {
|
|
94
|
+
var output = (0, _validateDate.default)('45-12-2020', 'DD-MM-YYYY');
|
|
95
|
+
expect(output).toEqual({
|
|
96
|
+
message: 'Day must be between 1 and 31',
|
|
97
|
+
propsInError: {
|
|
98
|
+
day: true
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
test('should correctly identify the maximum numbers of days in a given month and year', function () {
|
|
103
|
+
var max = (0, _validateDate.maxMonthDays)('02', '2024');
|
|
104
|
+
expect(max).toEqual(29);
|
|
105
|
+
max = (0, _validateDate.maxMonthDays)('02', '2023');
|
|
106
|
+
expect(max).toEqual(28);
|
|
107
|
+
max = (0, _validateDate.maxMonthDays)('06', '2023');
|
|
108
|
+
expect(max).toEqual(30);
|
|
109
|
+
max = (0, _validateDate.maxMonthDays)('07', '2023');
|
|
110
|
+
expect(max).toEqual(31);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
});
|
|
@@ -4,6 +4,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
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;
|
|
7
9
|
var HODS_EMAIL_REGEX = /^[a-z0-9._-]+@(digital\.)?homeoffice.gov.uk$/i;
|
|
8
10
|
|
|
9
11
|
/**
|
|
@@ -14,17 +16,28 @@ var HODS_EMAIL_REGEX = /^[a-z0-9._-]+@(digital\.)?homeoffice.gov.uk$/i;
|
|
|
14
16
|
* validateRequired (Validate.required) for that sort of validation.
|
|
15
17
|
* @param {*} value The value to validate.
|
|
16
18
|
* @param {string} label The label to use in any error message.
|
|
19
|
+
* @param {array} customErrors The array of custom errors to use for format message.
|
|
17
20
|
* @returns An error if the email address is invalid.
|
|
18
21
|
*/
|
|
19
22
|
var validateEmail = function validateEmail(value) {
|
|
20
23
|
var label = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
|
24
|
+
var customErrors = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
|
|
25
|
+
// eslint-disable-next-line no-extra-boolean-cast
|
|
21
26
|
if (!!value) {
|
|
27
|
+
var name = label ? label.toLowerCase() : 'email address';
|
|
22
28
|
if (typeof value === 'string') {
|
|
23
29
|
if (HODS_EMAIL_REGEX.test(value)) {
|
|
24
30
|
return undefined;
|
|
25
31
|
}
|
|
26
32
|
}
|
|
27
|
-
|
|
33
|
+
if (Array.isArray(customErrors)) {
|
|
34
|
+
var result = customErrors.filter(function (error) {
|
|
35
|
+
return error.type === 'format';
|
|
36
|
+
});
|
|
37
|
+
if (result && result.length > 0 && result[0].message) {
|
|
38
|
+
return result[0].message;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
28
41
|
return "Enter ".concat(name, " in the correct format, like jane.doe@homeoffice.gov.uk");
|
|
29
42
|
}
|
|
30
43
|
return undefined;
|