Skip to content

Commit

Permalink
Merge pull request #95 from formio/FIO-8336-fix-validation-on-multipl…
Browse files Browse the repository at this point in the history
…e-values

FIO-8336 fix validation on multiple values
  • Loading branch information
brendanbond authored and lane-formio committed Jul 8, 2024
1 parent d5314ae commit 5e82319
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/process/validation/__tests__/multiple.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { expect } from 'chai';
import { validationRules } from '..';
import { rules, serverRules } from '../rules';

const allRules = [...rules, ...serverRules];

const component = {
type: 'textfield',
key: 'multiple_textfield',
label: 'Multiple Textfield',
input: true,
multiple: true,
validate: {
required: true,
maxLength: 10,
minLength: 5,
pattern: '^[0-9]+$',
}
};

const context = {
component,
value: [],
path: 'multiple_textfield',
data: {multiple_textfield: []},
row: {multiple_textfield: []},
scope: {errors: []},
};

it('Validating required rule will work for multiple values component with no rows', async () => {
const fullValueRules = allRules.filter((rule) => rule.fullValue);
const rulesToValidate = validationRules(context, fullValueRules, undefined);
expect(rulesToValidate).to.not.have.length(0);
});

it('Validati olther rules will skip for multiple values component with no rows', async () => {
const otherRules = allRules.filter((rule) => !rule.fullValue);
const rulesToValidate = validationRules(context, otherRules, undefined);
expect(rulesToValidate).to.have.length(0);
});
8 changes: 8 additions & 0 deletions src/process/validation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ export function validationRules(
}
const validationRules: ValidationRuleInfo[] = [];
return rules.reduce((acc, rule: ValidationRuleInfo) => {
if (context.component.multiple &&
Array.isArray(context.value) &&
context.value?.length === 0 &&
!rule.fullValue
) {
return acc;
}

if (rule.shouldProcess && rule.shouldProcess(context)) {
acc.push(rule);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('validateMultiple', () => {

it('should return false for textArea component with as !== json', () => {
const component: TextAreaComponent = {
type: 'textArea',
type: 'textarea',
as: 'text',
input: true,
key: 'textArea',
Expand Down

0 comments on commit 5e82319

Please sign in to comment.