@@ -11,8 +11,8 @@ interface CheckOptions {
11
11
12
12
export class Schema < DataType = any , ErrorMsgType = string > {
13
13
readonly $spec : SchemaDeclaration < DataType , ErrorMsgType > ;
14
-
15
14
private data : PlainObject ;
15
+ private checkedFields : string [ ] = [ ] ;
16
16
private checkResult : SchemaCheckResult < DataType , ErrorMsgType > = { } ;
17
17
18
18
constructor ( schema : SchemaDeclaration < DataType , ErrorMsgType > ) {
@@ -102,19 +102,21 @@ export class Schema<DataType = any, ErrorMsgType = string> {
102
102
getSchemaSpec ( ) {
103
103
return this . $spec ;
104
104
}
105
-
106
- checkForField < T extends keyof DataType > (
105
+ _checkForField < T extends keyof DataType > (
107
106
fieldName : T ,
108
107
data : DataType ,
109
108
options : CheckOptions = { }
110
109
) : CheckResult < ErrorMsgType | string > {
111
110
this . setSchemaOptionsForAllType ( data ) ;
112
111
113
112
const { nestedObject } = options ;
113
+
114
+ // Add current field to checked list
115
+ this . checkedFields = [ ...this . checkedFields , fieldName as string ] ;
116
+
114
117
const fieldChecker = this . getFieldType ( fieldName , nestedObject ) ;
115
118
116
119
if ( ! fieldChecker ) {
117
- // fieldValue can be anything if no schema defined
118
120
return { hasError : false } ;
119
121
}
120
122
@@ -126,21 +128,33 @@ export class Schema<DataType = any, ErrorMsgType = string> {
126
128
if ( ! checkResult . hasError ) {
127
129
const { checkIfValueExists } = fieldChecker . proxyOptions ;
128
130
129
- // Check other fields if the field depends on them for validation
130
131
fieldChecker . otherFields ?. forEach ( ( field : string ) => {
131
- if ( checkIfValueExists ) {
132
- if ( ! isEmpty ( getFieldValue ( data , field , nestedObject ) ) ) {
133
- this . checkForField ( field as T , data , options ) ;
132
+ if ( ! this . checkedFields . includes ( field ) ) {
133
+ if ( checkIfValueExists ) {
134
+ if ( ! isEmpty ( getFieldValue ( data , field , nestedObject ) ) ) {
135
+ this . _checkForField ( field as T , data , { ...options } ) ;
136
+ }
137
+ return ;
134
138
}
135
- return ;
139
+ this . _checkForField ( field as T , data , { ... options } ) ;
136
140
}
137
- this . checkForField ( field as T , data , options ) ;
138
141
} ) ;
139
142
}
140
143
141
144
return checkResult ;
142
145
}
143
146
147
+ checkForField < T extends keyof DataType > (
148
+ fieldName : T ,
149
+ data : DataType ,
150
+ options : CheckOptions = { }
151
+ ) : CheckResult < ErrorMsgType | string > {
152
+ const result = this . _checkForField ( fieldName , data , options ) ;
153
+ // clean checked fields after check finished
154
+ this . checkedFields = [ ] ;
155
+ return result ;
156
+ }
157
+
144
158
checkForFieldAsync < T extends keyof DataType > (
145
159
fieldName : T ,
146
160
data : DataType ,
0 commit comments