Skip to content

Commit 2a56ea7

Browse files
committed
validator performances issues : 1961
rjsf-team#1961 (comment)
1 parent 4ee2075 commit 2a56ea7

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

packages/core/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "@rjsf/core",
3-
"version": "4.2.0",
2+
"name": "@imtf/rjsf-core",
3+
"version": "4.2.0-fix2",
44
"description": "A simple React component capable of building HTML forms out of a JSON schema.",
55
"scripts": {
66
"build": "npm run dist:build:umd && npm run build:cjs && npm run build:es && npm run build:es:lib",

packages/core/src/validate.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -305,19 +305,24 @@ export function withIdRefPrefix(schemaNode) {
305305
* false otherwise. If the schema is invalid, then this function will return
306306
* false.
307307
*/
308+
const validators = new Map();
309+
308310
export function isValid(schema, data, rootSchema) {
311+
let validator = validators.get(rootSchema);
312+
313+
if (!validator) {
314+
validator = createAjvInstance().addSchema(rootSchema, ROOT_SCHEMA_PREFIX);
315+
316+
validators.set(rootSchema, validator);
317+
}
318+
309319
try {
310320
// add the rootSchema ROOT_SCHEMA_PREFIX as id.
311321
// then rewrite the schema ref's to point to the rootSchema
312322
// this accounts for the case where schema have references to models
313323
// that lives in the rootSchema but not in the schema in question.
314-
return ajv
315-
.addSchema(rootSchema, ROOT_SCHEMA_PREFIX)
316-
.validate(withIdRefPrefix(schema), data);
324+
return validator.validate(withIdRefPrefix(schema), data);
317325
} catch (e) {
318326
return false;
319-
} finally {
320-
// make sure we remove the rootSchema from the global ajv instance
321-
ajv.removeSchema(ROOT_SCHEMA_PREFIX);
322327
}
323328
}

0 commit comments

Comments
 (0)