kristianmandrup/schema-to-yup

Custom 'required' error message doesn't work on fields with 'refValueFor' prop.

Opened this issue · 1 comments

When a required field also has a refValueFor prop, it no longer returns a custom required error message and instead, falls back to the default NAME is a required field message.

To see the issue, copy the following test into confrim-password.test.js

describe("Custom required error message", () => {
  let loginSchema, config, schema;

  beforeEach(() => {
    loginSchema = {
      $schema: "http://json-schema.org/draft-07/schema#",
      $id: "http://example.com/login.schema.json",
      title: "Login",
      description: "Login form",
      type: "object",
      ...innerSchema,
    };

    config = {
      logging: true,
      // for error messages...
      errMessages: {
        password: {
          "required": "Enter a new password",
          "pattern": "Please enter a valid password"
        },
        confirmPassword: {
          "required": "Enter a confirm password",
          "refValueFor": "Confirm password field must have the same value as New password"
        }
      }
    };
    schema = buildYup(loginSchema, config);
  });

  it("should show custom error message for required field", async () => {
    try {
      schema.validateSync({
        username: "jimmy",
        password: "xyz123"
      });
    } catch (e) {
      expect(e.errors[0]).toBe("Enter a confirm password");
    }
  });

  it("should show custom error message for refsFor field", async () => {
    try {
      schema.validateSync({
        username: "jimmy",
        password: "xyz123",
        confirmPassword: "xyz1234",
      });
    } catch (e) {
      expect(e.errors[0]).toBe("Confirm password field must have the same value as New password");
    }
  });
})

I have tried to debug the issue locally using the test provided above but don't understand the code well enough to get to the bottom of it. This functionality was working up until release V1.12.14. in V1.12.15 the Yup dependency was updated to V1.0.0 which broke this functionality. I know that you are not really maintaining this library much anymore but if you could help me look into it that would be very much appreciated.