From 2a3c7c8388cec15dd44b8e35a5b9c3c203618c2f Mon Sep 17 00:00:00 2001 From: Igor Brasileiro Date: Thu, 16 Nov 2023 16:29:38 -0300 Subject: [PATCH] chore: Avoid Form retrieveSchema twice when liveValidate is true (#3959) * Avoid retrieveSchema multiple times * Fix changelog --- CHANGELOG.md | 4 ++++ packages/core/src/components/Form.tsx | 7 ++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2ad163081..30a172961f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,10 @@ should change the heading of the (upcoming) version to include a major version b - Update `sanitizeDataForNewSchema()` to avoid spreading strings and Arrays into the returned value when the old schema is of type `string` or `array` and the new schema is of type `object`. Fixing [#3922](https://github.com/rjsf-team/react-jsonschema-form/issues/3922) +## @rjsf/core + +- avoid call `retrieveSchema` twice during `getStateFromProps` and `mustValidate` is true [#3959](https://github.com/rjsf-team/react-jsonschema-form/pull/3959) + # 5.14.1 ## @rjsf/utils diff --git a/packages/core/src/components/Form.tsx b/packages/core/src/components/Form.tsx index aa43ffea14..f46b8eb6de 100644 --- a/packages/core/src/components/Form.tsx +++ b/packages/core/src/components/Form.tsx @@ -394,7 +394,7 @@ export default class Form< let schemaValidationErrors: RJSFValidationError[] = state.schemaValidationErrors; let schemaValidationErrorSchema: ErrorSchema = state.schemaValidationErrorSchema; if (mustValidate) { - const schemaValidation = this.validate(formData, schema, schemaUtils); + const schemaValidation = this.validate(formData, schema, schemaUtils, retrievedSchema); errors = schemaValidation.errors; errorSchema = schemaValidation.errorSchema; schemaValidationErrors = errors; @@ -451,11 +451,12 @@ export default class Form< validate( formData: T | undefined, schema = this.props.schema, - altSchemaUtils?: SchemaUtilsType + altSchemaUtils?: SchemaUtilsType, + retrievedSchema?: S ): ValidationData { const schemaUtils = altSchemaUtils ? altSchemaUtils : this.state.schemaUtils; const { customValidate, transformErrors, uiSchema } = this.props; - const resolvedSchema = schemaUtils.retrieveSchema(schema, formData); + const resolvedSchema = retrievedSchema ?? schemaUtils.retrieveSchema(schema, formData); return schemaUtils .getValidator() .validateFormData(formData, resolvedSchema, customValidate, transformErrors, uiSchema);