@@ -8,7 +8,7 @@ export class ArrayType<DataType = any, E = ErrorMessageType> extends MixedType<
8
8
E ,
9
9
ArrayTypeLocale
10
10
> {
11
- [ arrayTypeSchemaSpec ] : MixedType < any , DataType , E > ;
11
+ [ arrayTypeSchemaSpec ] : MixedType < any , DataType , E > | MixedType < any , DataType , E > [ ] ;
12
12
private isArrayTypeNested = false ;
13
13
14
14
constructor ( errorMessage ?: E | string ) {
@@ -75,44 +75,66 @@ export class ArrayType<DataType = any, E = ErrorMessageType> extends MixedType<
75
75
return this ;
76
76
}
77
77
78
- of ( type : MixedType < any , DataType , E > ) {
79
- this [ arrayTypeSchemaSpec ] = type ;
80
-
81
- // Mark inner ArrayType as nested when dealing with nested arrays
82
- if ( type instanceof ArrayType ) {
83
- type . isArrayTypeNested = true ;
84
- }
78
+ of ( ...types : MixedType < any , DataType , E > [ ] ) {
79
+ if ( types . length === 1 ) {
80
+ const type = types [ 0 ] ;
81
+ this [ arrayTypeSchemaSpec ] = type ;
85
82
86
- super . pushRule ( {
87
- onValid : ( items , data , fieldName ) => {
88
- // For non-array values in nested arrays, pass directly to inner type validation
89
- if ( ! Array . isArray ( items ) && this . isArrayTypeNested ) {
90
- return type . check ( items , data , fieldName ) ;
91
- }
83
+ // Mark inner ArrayType as nested when dealing with nested arrays
84
+ if ( type instanceof ArrayType ) {
85
+ type . isArrayTypeNested = true ;
86
+ }
92
87
93
- // For non-array values in non-nested arrays, return array type error
94
- if ( ! Array . isArray ( items ) ) {
95
- return {
96
- hasError : true ,
97
- errorMessage : this . locale . type
98
- } ;
99
- }
88
+ super . pushRule ( {
89
+ onValid : ( items , data , fieldName ) => {
90
+ // For non-array values in nested arrays, pass directly to inner type validation
91
+ if ( ! Array . isArray ( items ) && this . isArrayTypeNested ) {
92
+ return type . check ( items , data , fieldName ) ;
93
+ }
100
94
101
- const checkResults = items . map ( ( value , index ) => {
102
- const name = Array . isArray ( fieldName )
103
- ? [ ...fieldName , `[${ index } ]` ]
104
- : [ fieldName , `[${ index } ]` ] ;
95
+ // For non-array values in non-nested arrays, return array type error
96
+ if ( ! Array . isArray ( items ) ) {
97
+ return {
98
+ hasError : true ,
99
+ errorMessage : this . locale . type
100
+ } ;
101
+ }
105
102
106
- return type . check ( value , data , name as string [ ] ) ;
107
- } ) ;
108
- const hasError = ! ! checkResults . find ( item => item ?. hasError ) ;
103
+ const checkResults = items . map ( ( value , index ) => {
104
+ const name = Array . isArray ( fieldName )
105
+ ? [ ...fieldName , `[${ index } ]` ]
106
+ : [ fieldName , `[${ index } ]` ] ;
109
107
110
- return {
111
- hasError,
112
- array : checkResults
113
- } as CheckResult < string | E > ;
114
- }
115
- } ) ;
108
+ return type . check ( value , data , name as string [ ] ) ;
109
+ } ) ;
110
+ const hasError = ! ! checkResults . find ( item => item ?. hasError ) ;
111
+
112
+ return {
113
+ hasError,
114
+ array : checkResults
115
+ } as CheckResult < string | E > ;
116
+ }
117
+ } ) ;
118
+ } else {
119
+ this [ arrayTypeSchemaSpec ] = types ;
120
+ super . pushRule ( {
121
+ onValid : ( items , data , fieldName ) => {
122
+ const checkResults = items . map ( ( value , index ) => {
123
+ const name = Array . isArray ( fieldName )
124
+ ? [ ...fieldName , `[${ index } ]` ]
125
+ : [ fieldName , `[${ index } ]` ] ;
126
+
127
+ return types [ index ] . check ( value , data , name as string [ ] ) ;
128
+ } ) ;
129
+ const hasError = ! ! checkResults . find ( item => item ?. hasError ) ;
130
+
131
+ return {
132
+ hasError,
133
+ array : checkResults
134
+ } as CheckResult < string | E > ;
135
+ }
136
+ } ) ;
137
+ }
116
138
117
139
return this ;
118
140
}
0 commit comments