1
1
import toPath from "lodash/toPath" ;
2
2
import Ajv from "ajv" ;
3
- let ajv = createAjvInstance ( ) ;
3
+ let formValidationAjv = createAjvInstance ( ) ;
4
+ let schemaValidationAjv = createAjvInstance ( ) ;
4
5
import { deepEquals , getDefaultFormState } from "./utils" ;
5
6
6
7
let formerCustomFormats = null ;
@@ -182,7 +183,7 @@ export default function validateFormData(
182
183
const newFormats = ! deepEquals ( formerCustomFormats , customFormats ) ;
183
184
184
185
if ( newMetaSchemas || newFormats ) {
185
- ajv = createAjvInstance ( ) ;
186
+ formValidationAjv = createAjvInstance ( ) ;
186
187
}
187
188
188
189
// add more schemas to validate against
@@ -191,30 +192,30 @@ export default function validateFormData(
191
192
newMetaSchemas &&
192
193
Array . isArray ( additionalMetaSchemas )
193
194
) {
194
- ajv . addMetaSchema ( additionalMetaSchemas ) ;
195
+ formValidationAjv . addMetaSchema ( additionalMetaSchemas ) ;
195
196
formerMetaSchema = additionalMetaSchemas ;
196
197
}
197
198
198
199
// add more custom formats to validate against
199
200
if ( customFormats && newFormats && isObject ( customFormats ) ) {
200
201
Object . keys ( customFormats ) . forEach ( formatName => {
201
- ajv . addFormat ( formatName , customFormats [ formatName ] ) ;
202
+ formValidationAjv . addFormat ( formatName , customFormats [ formatName ] ) ;
202
203
} ) ;
203
204
204
205
formerCustomFormats = customFormats ;
205
206
}
206
207
207
208
let validationError = null ;
208
209
try {
209
- ajv . validate ( schema , formData ) ;
210
+ formValidationAjv . validate ( schema , formData ) ;
210
211
} catch ( err ) {
211
212
validationError = err ;
212
213
}
213
214
214
- let errors = transformAjvErrors ( ajv . errors ) ;
215
+ let errors = transformAjvErrors ( formValidationAjv . errors ) ;
215
216
// Clear errors to prevent persistent errors, see #1104
216
217
217
- ajv . errors = null ;
218
+ formValidationAjv . errors = null ;
218
219
219
220
const noProperMetaSchema =
220
221
validationError &&
@@ -307,19 +308,19 @@ export function isValid(schema, data, rootSchema) {
307
308
// that lives in the rootSchema but not in the schema in question.
308
309
let rootSchemaAdded ;
309
310
if ( rootSchema . $id ) {
310
- rootSchemaAdded = ! ! ajv . getSchema ( rootSchema . $id ) ;
311
+ rootSchemaAdded = ! ! schemaValidationAjv . getSchema ( rootSchema . $id ) ;
311
312
} else {
312
- rootSchemaAdded = ! ! ajv . getSchema ( ROOT_SCHEMA_PREFIX ) ;
313
+ rootSchemaAdded = ! ! schemaValidationAjv . getSchema ( ROOT_SCHEMA_PREFIX ) ;
313
314
}
314
315
315
316
if ( ! rootSchemaAdded ) {
316
317
if ( rootSchema . $id ) {
317
- ajv . addSchema ( rootSchema ) ;
318
+ schemaValidationAjv . addSchema ( rootSchema ) ;
318
319
} else {
319
- ajv . addSchema ( rootSchema , ROOT_SCHEMA_PREFIX ) ;
320
+ schemaValidationAjv . addSchema ( rootSchema , ROOT_SCHEMA_PREFIX ) ;
320
321
}
321
322
}
322
- return ajv . validate ( withIdRefPrefix ( schema ) , data ) ;
323
+ return schemaValidationAjv . validate ( withIdRefPrefix ( schema ) , data ) ;
323
324
} catch ( e ) {
324
325
console . warn ( "Encountered error while validating schema" , e ) ;
325
326
return false ;
0 commit comments