Skip to content

Commit 39c012b

Browse files
committed
Separate ajv instances used for evaluating the form data vs subschemas
1 parent 4cf5f7f commit 39c012b

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

packages/core/src/validate.js

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import toPath from "lodash/toPath";
22
import Ajv from "ajv";
3-
let ajv = createAjvInstance();
3+
let formValidationAjv = createAjvInstance();
4+
let schemaValidationAjv = createAjvInstance();
45
import { deepEquals, getDefaultFormState } from "./utils";
56

67
let formerCustomFormats = null;
@@ -182,7 +183,7 @@ export default function validateFormData(
182183
const newFormats = !deepEquals(formerCustomFormats, customFormats);
183184

184185
if (newMetaSchemas || newFormats) {
185-
ajv = createAjvInstance();
186+
formValidationAjv = createAjvInstance();
186187
}
187188

188189
// add more schemas to validate against
@@ -191,30 +192,30 @@ export default function validateFormData(
191192
newMetaSchemas &&
192193
Array.isArray(additionalMetaSchemas)
193194
) {
194-
ajv.addMetaSchema(additionalMetaSchemas);
195+
formValidationAjv.addMetaSchema(additionalMetaSchemas);
195196
formerMetaSchema = additionalMetaSchemas;
196197
}
197198

198199
// add more custom formats to validate against
199200
if (customFormats && newFormats && isObject(customFormats)) {
200201
Object.keys(customFormats).forEach(formatName => {
201-
ajv.addFormat(formatName, customFormats[formatName]);
202+
formValidationAjv.addFormat(formatName, customFormats[formatName]);
202203
});
203204

204205
formerCustomFormats = customFormats;
205206
}
206207

207208
let validationError = null;
208209
try {
209-
ajv.validate(schema, formData);
210+
formValidationAjv.validate(schema, formData);
210211
} catch (err) {
211212
validationError = err;
212213
}
213214

214-
let errors = transformAjvErrors(ajv.errors);
215+
let errors = transformAjvErrors(formValidationAjv.errors);
215216
// Clear errors to prevent persistent errors, see #1104
216217

217-
ajv.errors = null;
218+
formValidationAjv.errors = null;
218219

219220
const noProperMetaSchema =
220221
validationError &&
@@ -307,19 +308,19 @@ export function isValid(schema, data, rootSchema) {
307308
// that lives in the rootSchema but not in the schema in question.
308309
let rootSchemaAdded;
309310
if (rootSchema.$id) {
310-
rootSchemaAdded = !!ajv.getSchema(rootSchema.$id);
311+
rootSchemaAdded = !!schemaValidationAjv.getSchema(rootSchema.$id);
311312
} else {
312-
rootSchemaAdded = !!ajv.getSchema(ROOT_SCHEMA_PREFIX);
313+
rootSchemaAdded = !!schemaValidationAjv.getSchema(ROOT_SCHEMA_PREFIX);
313314
}
314315

315316
if (!rootSchemaAdded) {
316317
if (rootSchema.$id) {
317-
ajv.addSchema(rootSchema);
318+
schemaValidationAjv.addSchema(rootSchema);
318319
} else {
319-
ajv.addSchema(rootSchema, ROOT_SCHEMA_PREFIX);
320+
schemaValidationAjv.addSchema(rootSchema, ROOT_SCHEMA_PREFIX);
320321
}
321322
}
322-
return ajv.validate(withIdRefPrefix(schema), data);
323+
return schemaValidationAjv.validate(withIdRefPrefix(schema), data);
323324
} catch (e) {
324325
console.warn("Encountered error while validating schema", e);
325326
return false;

0 commit comments

Comments
 (0)