File tree Expand file tree Collapse file tree 3 files changed +49
-1
lines changed Expand file tree Collapse file tree 3 files changed +49
-1
lines changed Original file line number Diff line number Diff line change 1+ import { expect } from 'chai' ;
2+ import { validationRules } from '..' ;
3+ import { rules , serverRules } from '../rules' ;
4+
5+ const allRules = [ ...rules , ...serverRules ] ;
6+
7+ const component = {
8+ type : 'textfield' ,
9+ key : 'multiple_textfield' ,
10+ label : 'Multiple Textfield' ,
11+ input : true ,
12+ multiple : true ,
13+ validate : {
14+ required : true ,
15+ maxLength : 10 ,
16+ minLength : 5 ,
17+ pattern : '^[0-9]+$' ,
18+ }
19+ } ;
20+
21+ const context = {
22+ component,
23+ value : [ ] ,
24+ path : 'multiple_textfield' ,
25+ data : { multiple_textfield : [ ] } ,
26+ row : { multiple_textfield : [ ] } ,
27+ scope : { errors : [ ] } ,
28+ } ;
29+
30+ it ( 'Validating required rule will work for multiple values component with no rows' , async ( ) => {
31+ const fullValueRules = allRules . filter ( ( rule ) => rule . fullValue ) ;
32+ const rulesToValidate = validationRules ( context , fullValueRules , undefined ) ;
33+ expect ( rulesToValidate ) . to . not . have . length ( 0 ) ;
34+ } ) ;
35+
36+ it ( 'Validati olther rules will skip for multiple values component with no rows' , async ( ) => {
37+ const otherRules = allRules . filter ( ( rule ) => ! rule . fullValue ) ;
38+ const rulesToValidate = validationRules ( context , otherRules , undefined ) ;
39+ expect ( rulesToValidate ) . to . have . length ( 0 ) ;
40+ } ) ;
Original file line number Diff line number Diff line change @@ -49,6 +49,14 @@ export function validationRules(
4949 }
5050 const validationRules : ValidationRuleInfo [ ] = [ ] ;
5151 return rules . reduce ( ( acc , rule : ValidationRuleInfo ) => {
52+ if ( context . component . multiple &&
53+ Array . isArray ( context . value ) &&
54+ context . value ?. length === 0 &&
55+ ! rule . fullValue
56+ ) {
57+ return acc ;
58+ }
59+
5260 if ( rule . shouldProcess && rule . shouldProcess ( context ) ) {
5361 acc . push ( rule ) ;
5462 }
Original file line number Diff line number Diff line change @@ -36,7 +36,7 @@ describe('validateMultiple', () => {
3636
3737 it ( 'should return false for textArea component with as !== json' , ( ) => {
3838 const component : TextAreaComponent = {
39- type : 'textArea ' ,
39+ type : 'textarea ' ,
4040 as : 'text' ,
4141 input : true ,
4242 key : 'textArea' ,
You can’t perform that action at this time.
0 commit comments