Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation errors with localizer do not show title or ui:title #4387

Open
4 tasks done
chibacchie opened this issue Nov 19, 2024 · 0 comments
Open
4 tasks done

Validation errors with localizer do not show title or ui:title #4387

chibacchie opened this issue Nov 19, 2024 · 0 comments
Labels
bug needs triage Initial label given, to be assigned correct labels and assigned

Comments

@chibacchie
Copy link

chibacchie commented Nov 19, 2024

Prerequisites

What theme are you using?

validator-ajv8

Version

5.22.2+

Current Behavior

Validation errors with localizer do not show title or ui:title.

const ajv8 = require("@rjsf/validator-ajv8");
const localizer = require("ajv-i18n");

const schema = {
  type: "object",
  required: ["a"],
  properties: {
    a: {
      type: "string",
      title: "A",
    },
  },
};

const validator = ajv8.customizeValidator({}, localizer.en);
const result = validator.validateFormData({}, schema);
console.log(result.errors[0].message);

This result in must have required property a.

This only happens with localizer and is a side effect of #4349 and compatibility issue with ajv-i18n.

Expected Behavior

must have required property 'A' is expected. It should be better to always show title or ui:title instead of showing property.

Possible workaround might be following but I am not too sure this is a right approach or not.

diff --git a/packages/validator-ajv8/src/validator.ts b/packages/validator-ajv8/src/validator.ts
index 60dce8db..fb5bea0f 100644
--- a/packages/validator-ajv8/src/validator.ts
+++ b/packages/validator-ajv8/src/validator.ts
@@ -90,7 +90,18 @@ export default class AJV8Validator<T = any, S extends StrictRJSFSchema = RJSFSch
     let errors;
     if (compiledValidator) {
       if (typeof this.localizer === 'function') {
+        (compiledValidator.errors ?? []).forEach((error) => {
+          if (error.params?.missingProperty) {
+            error.params.missingProperty = `'${error.params.missingProperty}'`;
+          }
+        });
         this.localizer(compiledValidator.errors);
+        (compiledValidator.errors ?? []).forEach((error) => {
+          if (error.params?.missingProperty) {
+            error.params.missingProperty = error.params.missingProperty.slice(1, -1);
+          }
+        });
       }
       errors = compiledValidator.errors || undefined;

Steps To Reproduce

Please see above.

Environment

- OS: Ubuntu 22.04
- Node: 20.15.1
- npm: 10.7.0

Anything else?

No response

@chibacchie chibacchie added bug needs triage Initial label given, to be assigned correct labels and assigned labels Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs triage Initial label given, to be assigned correct labels and assigned
Projects
None yet
Development

No branches or pull requests

1 participant