diff --git a/src/process/validation/__tests__/multiple.test.ts b/src/process/validation/__tests__/multiple.test.ts new file mode 100644 index 00000000..a32b686f --- /dev/null +++ b/src/process/validation/__tests__/multiple.test.ts @@ -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); +}); diff --git a/src/process/validation/index.ts b/src/process/validation/index.ts index 2d8387cc..c4129c1d 100644 --- a/src/process/validation/index.ts +++ b/src/process/validation/index.ts @@ -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); } diff --git a/src/process/validation/rules/__tests__/validateMultiple.test.ts b/src/process/validation/rules/__tests__/validateMultiple.test.ts index abbc28f1..f44bcfd7 100644 --- a/src/process/validation/rules/__tests__/validateMultiple.test.ts +++ b/src/process/validation/rules/__tests__/validateMultiple.test.ts @@ -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',