Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIO-9081: fixed an issue where required validation is not triggered for empty value of multiple component #158

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
161 changes: 161 additions & 0 deletions src/process/__tests__/process.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3689,6 +3689,167 @@ describe('Process Tests', () => {
expect((context.scope as ValidationScope).errors).to.have.length(1);
});

it('Should validate required an empty array for multiple select', async () => {
const form = {
_id: '66f4141e34ac6c4049cc5144',
title: 'required multiple',
name: 'requiredMultiple',
path: 'requiredmultiple',
type: 'form',
display: 'form',
owner: '637b2e6b48c1227e60b1f910',
components: [
{
label: 'Select',
widget: 'choicesjs',
tableView: true,
multiple: true,
data: {
values: [
{
label: 'a',
value: 'a',
},
{
label: 'b',
value: 'b',
},
{
label: 'c',
value: 'c',
},
],
},
validate: {
required: true,
},
validateWhenHidden: false,
key: 'select',
type: 'select',
input: true,
},
{
type: 'button',
label: 'Submit',
key: 'submit',
disableOnInvalid: true,
input: true,
tableView: false,
},
],
project: '66f26afae0c7ef9920ae59f6',
};

const submission = {
data: { select: [] },
owner: '637b2e6b48c1227e60b1f910',
access: [],
_fvid: 0,
state: 'submitted',
_id: '66c455fc0f00757fd4b0e79d',
form: '66bc5cff7ca1729623a182db',
};

const errors: any = [];
const context = {
form,
submission,
data: submission.data,
components: form.components,
processors: ProcessTargets.submission,
scope: { errors },
config: {
server: true,
},
};
processSync(context);
submission.data = context.data;
context.processors = ProcessTargets.evaluator;
processSync(context);
assert.equal(context.scope.errors.length, 1);
assert.equal(context.scope.errors[0].ruleName, 'required');
});

it('Should validate required an value for multiple select without errors', async () => {
const form = {
_id: '66f4141e34ac6c4049cc5144',
title: 'required multiple',
name: 'requiredMultiple',
path: 'requiredmultiple',
type: 'form',
display: 'form',
owner: '637b2e6b48c1227e60b1f910',
components: [
{
label: 'Select',
widget: 'choicesjs',
tableView: true,
multiple: true,
data: {
values: [
{
label: 'a',
value: 'a',
},
{
label: 'b',
value: 'b',
},
{
label: 'c',
value: 'c',
},
],
},
validate: {
required: true,
},
validateWhenHidden: false,
key: 'select',
type: 'select',
input: true,
},
{
type: 'button',
label: 'Submit',
key: 'submit',
disableOnInvalid: true,
input: true,
tableView: false,
},
],
project: '66f26afae0c7ef9920ae59f6',
};

const submission = {
data: { select: ['a'] },
owner: '637b2e6b48c1227e60b1f910',
access: [],
_fvid: 0,
state: 'submitted',
_id: '66c455fc0f00757fd4b0e79d',
form: '66bc5cff7ca1729623a182db',
};

const errors: any = [];
const context = {
form,
submission,
data: submission.data,
components: form.components,
processors: ProcessTargets.submission,
scope: { errors },
config: {
server: true,
},
};
processSync(context);
submission.data = context.data;
context.processors = ProcessTargets.evaluator;
processSync(context);
assert.equal(context.scope.errors.length, 0);
});

describe('For EditGrid:', () => {
const components = [
{
Expand Down
2 changes: 1 addition & 1 deletion src/process/validation/__tests__/multiple.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ it('Validating required rule will work for multiple values component with no row
});

it('Validati olther rules will skip for multiple values component with no rows', async () => {
const otherRules = allRules.filter((rule) => !rule.fullValue);
const otherRules = allRules.filter((rule) => !rule.fullValue && !rule.emptyMultiValue);
const rulesToValidate = validationRules(context, otherRules, undefined);
expect(rulesToValidate).to.have.length(0);
});
2 changes: 1 addition & 1 deletion src/process/validation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function validationRules(
if (context.component.multiple &&
Array.isArray(context.value) &&
context.value?.length === 0 &&
!rule.fullValue
!rule.fullValue && !rule.emptyMultiValue
) {
return acc;
}
Expand Down
1 change: 1 addition & 0 deletions src/process/validation/rules/validateRequired.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export const validateRequiredSync: RuleFnSync = (context: ValidationContext) =>
export const validateRequiredInfo: ProcessorInfo<ValidationContext, FieldError | null> = {
name: 'validateRequired',
process: validateRequired,
emptyMultiValue: true,
processSync: validateRequiredSync,
shouldProcess: shouldValidate,
};
1 change: 1 addition & 0 deletions src/types/process/ProcessorInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export type ProcessCheckFn<ProcessorContext> = (context: ProcessorContext) => bo
export type ProcessorInfo<ProcessorContext, ProcessorReturnType> = {
name: string;
fullValue?: boolean;
emptyMultiValue?: boolean
process?: (context: ProcessorContext) => Promise<ProcessorReturnType>;
processSync?: (context: ProcessorContext) => ProcessorReturnType;
postProcess?: (context: ProcessorContext) => void;
Expand Down
Loading