From c6fdb92dc00842403b05c2d57416707eee13c5d7 Mon Sep 17 00:00:00 2001 From: Abdallah Al-Soqatri Date: Tue, 28 May 2024 13:03:29 +0200 Subject: [PATCH 1/2] Fixed performance issue #4203 --- CHANGELOG.md | 10 +++++++ packages/validator-ajv6/src/validator.ts | 24 ++++++++++++++--- packages/validator-ajv8/src/validator.ts | 27 +++++++++++++------ .../validator-ajv8/test/validator.test.ts | 9 +++---- 4 files changed, 52 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eadf9bea7f..3b4a8a89eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,16 @@ should change the heading of the (upcoming) version to include a major version b --> +# 5.18.5 + +## @rjsf/validator-ajv6 + +- Improved performance issues with large schema dependencies and oneOf conditions [#4203](https://github.com/rjsf-team/react-jsonschema-form/issues/4203). + +## @rjsf/validator-ajv8 + +- Improved performance issues with large schema dependencies and oneOf conditions [#4203](https://github.com/rjsf-team/react-jsonschema-form/issues/4203). + # 5.18.4 ## Dev / docs / playground diff --git a/packages/validator-ajv6/src/validator.ts b/packages/validator-ajv6/src/validator.ts index 02249bd8d2..ee65186c93 100644 --- a/packages/validator-ajv6/src/validator.ts +++ b/packages/validator-ajv6/src/validator.ts @@ -2,6 +2,7 @@ import { Ajv, ErrorObject } from 'ajv'; import { createErrorHandler, CustomValidator, + deepEquals, ErrorSchema, ErrorTransformer, FormContextType, @@ -158,6 +159,16 @@ export default class AJV6Validator({ errors, errorSchema }, userErrorSchema); } + /** + * This function is called when the root schema changes. It removes the old root schema from the ajv instance and adds the new one. + * @param rootSchema - The root schema used to provide $ref resolutions + */ + handleRootSchemaChange(rootSchema: RJSFSchema): void { + const rootSchemaId = ROOT_SCHEMA_PREFIX; + this.ajv.removeSchema(rootSchemaId); + this.ajv.addSchema(rootSchema, rootSchemaId); + } + /** Validates data against a schema, returning true if the data is valid, or * false otherwise. If the schema is invalid, then this function will return * false. @@ -169,16 +180,21 @@ export default class AJV6Validator(schema) as S; const schemaId = schemaWithIdRefPrefix[ID_KEY] ?? hashForSchema(schemaWithIdRefPrefix); let compiledValidator: ValidateFunction | undefined; @@ -155,10 +170,6 @@ export default class AJV8Validator { validator.isValid(schema, formData, rootSchema); // Root schema is added twice - expect(addSchemaSpy).toHaveBeenCalledTimes(3); + expect(addSchemaSpy).toHaveBeenCalledTimes(2); expect(addSchemaSpy).toHaveBeenNthCalledWith(1, expect.objectContaining(rootSchema), rootSchema.$id); expect(addSchemaSpy).toHaveBeenNthCalledWith(2, expect.objectContaining(schema), schema.$id); - expect(addSchemaSpy).toHaveBeenLastCalledWith(expect.objectContaining(rootSchema), rootSchema.$id); }); it('should fallback to using compile', () => { const schema: RJSFSchema = { @@ -594,10 +593,9 @@ describe('AJV8Validator', () => { validator.isValid(schema, formData, rootSchema); // Root schema is added twice - expect(addSchemaSpy).toHaveBeenCalledTimes(3); + expect(addSchemaSpy).toHaveBeenCalledTimes(2); expect(addSchemaSpy).toHaveBeenNthCalledWith(1, expect.objectContaining(rootSchema), rootSchema.$id); expect(addSchemaSpy).toHaveBeenNthCalledWith(2, expect.objectContaining(schema), schema.$id); - expect(addSchemaSpy).toHaveBeenLastCalledWith(expect.objectContaining(rootSchema), rootSchema.$id); }); }); describe('validator.toErrorList()', () => { @@ -1050,10 +1048,9 @@ describe('AJV8Validator', () => { validator.isValid(schema, formData, rootSchema); // Root schema is added twice - expect(addSchemaSpy).toHaveBeenCalledTimes(3); + expect(addSchemaSpy).toHaveBeenCalledTimes(2); expect(addSchemaSpy).toHaveBeenNthCalledWith(1, expect.objectContaining(rootSchema), rootSchema.$id); expect(addSchemaSpy).toHaveBeenNthCalledWith(2, expect.objectContaining(schema), schema.$id); - expect(addSchemaSpy).toHaveBeenLastCalledWith(expect.objectContaining(rootSchema), rootSchema.$id); }); }); describe('validator.toErrorList()', () => { From 042e93c2231a9d3e3d8f6da5d397df8bf580d4c6 Mon Sep 17 00:00:00 2001 From: Abdallah Al-Soqatri Date: Fri, 21 Jun 2024 10:44:13 +0200 Subject: [PATCH 2/2] code improvement based on feedback --- packages/validator-ajv6/src/validator.ts | 26 ++++++++++++------------ packages/validator-ajv8/src/validator.ts | 26 +++++++++++------------- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/packages/validator-ajv6/src/validator.ts b/packages/validator-ajv6/src/validator.ts index ee65186c93..b8369c42f4 100644 --- a/packages/validator-ajv6/src/validator.ts +++ b/packages/validator-ajv6/src/validator.ts @@ -160,13 +160,21 @@ export default class AJV6Validator