Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/process/__tests__/fixtures/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import form1 from './form1.json';
import subs from './subs.json';
import requiredFieldInsideEditGrid from './requiredFieldInsideConditionalEditGrid.json';
import formWithDefaultValues from './componentsWithDefaultValues.json';
import requiredValidationDisabledByLogic from './requiredValidationDisabledByLogic.json';

export {
addressComponentWithOtherCondComponents,
Expand All @@ -28,4 +29,5 @@ export {
subs,
requiredFieldInsideEditGrid,
formWithDefaultValues,
requiredValidationDisabledByLogic,
};
262 changes: 262 additions & 0 deletions src/process/__tests__/fixtures/requiredValidationDisabledByLogic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
{
"type": "form",
"display": "form",
"components": [
{
"label": "B",
"applyMaskOn": "change",
"tableView": true,
"validate": {
"required": true,
"custom": "",
"customPrivate": false,
"strictDateValidation": false,
"multiple": false,
"unique": false,
"minLength": "",
"maxLength": "",
"pattern": ""
},
"validateWhenHidden": false,
"key": "b",
"logic": [
{
"name": "Skip Required",
"trigger": {
"type": "simple",
"simple": {
"show": true,
"conjunction": "all",
"conditions": [
{
"component": "skipRequired",
"operator": "isEqual",
"value": true
}
]
}
},
"actions": [
{
"name": "Skip Required",
"type": "property",
"property": {
"label": "Required",
"value": "validate.required",
"type": "boolean"
},
"state": false
}
]
}
],
"type": "textfield",
"input": true,
"id": "evjjdls",
"placeholder": "",
"prefix": "",
"customClass": "",
"suffix": "",
"multiple": false,
"defaultValue": null,
"protected": false,
"unique": false,
"persistent": true,
"hidden": false,
"clearOnHide": true,
"refreshOn": "",
"redrawOn": "",
"modalEdit": false,
"dataGridLabel": false,
"labelPosition": "top",
"description": "",
"errorLabel": "",
"tooltip": "",
"hideLabel": false,
"tabindex": "",
"disabled": false,
"autofocus": false,
"dbIndex": false,
"customDefaultValue": "",
"calculateValue": "",
"calculateServer": false,
"widget": {
"type": "input"
},
"attributes": {},
"validateOn": "change",
"conditional": {
"show": null,
"when": null,
"eq": ""
},
"overlay": {
"style": "",
"left": "",
"top": "",
"width": "",
"height": ""
},
"allowCalculateOverride": false,
"encrypted": false,
"showCharCount": false,
"showWordCount": false,
"properties": {},
"allowMultipleMasks": false,
"addons": [],
"serverOverride": {},
"mask": false,
"inputType": "text",
"inputFormat": "plain",
"inputMask": "",
"displayMask": "",
"spellcheck": true,
"truncateMultipleSpaces": false
},
{
"label": "Skip Required",
"tableView": false,
"validateWhenHidden": false,
"key": "skipRequired",
"type": "checkbox",
"input": true,
"defaultValue": false,
"id": "eoz059n",
"placeholder": "",
"prefix": "",
"customClass": "",
"suffix": "",
"multiple": false,
"protected": false,
"unique": false,
"persistent": true,
"hidden": false,
"clearOnHide": true,
"refreshOn": "",
"redrawOn": "",
"modalEdit": false,
"dataGridLabel": true,
"labelPosition": "right",
"description": "",
"errorLabel": "",
"tooltip": "",
"hideLabel": false,
"tabindex": "",
"disabled": false,
"autofocus": false,
"dbIndex": false,
"customDefaultValue": "",
"calculateValue": "",
"calculateServer": false,
"widget": null,
"attributes": {},
"validateOn": "change",
"validate": {
"required": false,
"custom": "",
"customPrivate": false,
"strictDateValidation": false,
"multiple": false,
"unique": false
},
"conditional": {
"show": null,
"when": null,
"eq": ""
},
"overlay": {
"style": "",
"left": "",
"top": "",
"width": "",
"height": ""
},
"allowCalculateOverride": false,
"encrypted": false,
"showCharCount": false,
"showWordCount": false,
"properties": {},
"allowMultipleMasks": false,
"addons": [],
"serverOverride": {},
"inputType": "checkbox",
"value": "",
"name": ""
},
{
"type": "button",
"label": "Submit",
"key": "submit",
"disableOnInvalid": true,
"input": true,
"tableView": false,
"id": "e19px9p",
"placeholder": "",
"prefix": "",
"customClass": "",
"suffix": "",
"multiple": false,
"defaultValue": null,
"protected": false,
"unique": false,
"persistent": false,
"hidden": false,
"clearOnHide": true,
"refreshOn": "",
"redrawOn": "",
"modalEdit": false,
"dataGridLabel": true,
"labelPosition": "top",
"description": "",
"errorLabel": "",
"tooltip": "",
"hideLabel": false,
"tabindex": "",
"disabled": false,
"autofocus": false,
"dbIndex": false,
"customDefaultValue": "",
"calculateValue": "",
"calculateServer": false,
"widget": {
"type": "input"
},
"attributes": {},
"validateOn": "change",
"validate": {
"required": false,
"custom": "",
"customPrivate": false,
"strictDateValidation": false,
"multiple": false,
"unique": false
},
"conditional": {
"show": null,
"when": null,
"eq": ""
},
"overlay": {
"style": "",
"left": "",
"top": "",
"width": "",
"height": ""
},
"allowCalculateOverride": false,
"encrypted": false,
"showCharCount": false,
"showWordCount": false,
"properties": {},
"allowMultipleMasks": false,
"addons": [],
"serverOverride": {},
"size": "md",
"leftIcon": "",
"rightIcon": "",
"block": false,
"action": "submit",
"theme": "primary"
}
]
}
23 changes: 22 additions & 1 deletion src/process/__tests__/process.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';
import assert from 'node:assert';
import type { ContainerComponent, ValidationScope } from 'types';
import type { ContainerComponent, ProcessContext, ValidationScope } from 'types';
import { getComponent } from 'utils/formUtil';
import { process, processSync, Processors } from '../index';
import { fastCloneDeep } from 'utils';
Expand All @@ -16,6 +16,7 @@
skipValidWithHiddenParentComp,
requiredFieldInsideEditGrid,
formWithDefaultValues,
requiredValidationDisabledByLogic,
} from './fixtures';
import _ from 'lodash';

Expand Down Expand Up @@ -898,7 +899,7 @@
form: '65ea368b705068f84a93c87a',
};

const errors: any = [];

Check warning on line 902 in src/process/__tests__/process.test.ts

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
const context = {
_,
form,
Expand Down Expand Up @@ -1038,7 +1039,7 @@
},
},
};
const errors: any = [];

Check warning on line 1042 in src/process/__tests__/process.test.ts

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
const context = {
form,
submission,
Expand Down Expand Up @@ -1173,9 +1174,9 @@
},
};
processSync(context);
assert.equal((context.scope as any).errors.length, 2);

Check warning on line 1177 in src/process/__tests__/process.test.ts

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
assert.equal((context.scope as any).errors[0].ruleName, 'array');

Check warning on line 1178 in src/process/__tests__/process.test.ts

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
assert.equal((context.scope as any).errors[1].ruleName, 'required');

Check warning on line 1179 in src/process/__tests__/process.test.ts

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
});

it('Sets a value based on advanced conditions', async function () {
Expand Down Expand Up @@ -1274,7 +1275,7 @@
const submission = {
data: { test: '1' },
};
const context: any = {

Check warning on line 1278 in src/process/__tests__/process.test.ts

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
form,
submission,
data: submission.data,
Expand Down Expand Up @@ -1542,7 +1543,7 @@
form: '65e8786fc5dacf667eef12fc',
};

const errors: any = [];

Check warning on line 1546 in src/process/__tests__/process.test.ts

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
const context = {
form,
submission,
Expand Down Expand Up @@ -6027,6 +6028,26 @@
expect((scope as ValidationScope).errors).to.have.length(1);
});

it('Should not trigger validation errors for components that remove required property by logic', async function () {
const form = requiredValidationDisabledByLogic;
const submission = {
data: {
b: '',
skipRequired: true,
},
};
const context = {
form,
submission: submission,
data: submission.data,
components: form.components,
processors: Processors,
scope: {} as ValidationScope,
} as ProcessContext<ValidationScope>;
processSync(context);
expect(context.scope.errors.length).to.equal(0);
});

describe('Required component validation in nested form in DataGrid/EditGrid', function () {
const nestedForm = {
key: 'form',
Expand Down
5 changes: 4 additions & 1 deletion src/process/validation/rules/validateRequired.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ const valueIsPresent = (

export const shouldValidate = (context: ValidationContext): boolean => {
const { component } = context;
return !!component.scope?.required || !!component.validate?.required;
if (component.scope?.hasOwnProperty('required')) {
return !!component.scope?.required;
}
return !!component.validate?.required;
};

export const validateRequired: RuleFn = async (context: ValidationContext) => {
Expand Down
Loading