Skip to content

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

@chibacchie

Description

@chibacchie

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugneeds triageInitial label given, to be assigned correct labels and assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions