Skip to content
New issue

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

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

Already on GitHub? # to your account

feat(ajv): Keep original validation type while using errorMessage #728

Merged
merged 3 commits into from
Jan 27, 2025

Conversation

ktunador
Copy link
Contributor

@ktunador ktunador commented Dec 10, 2024

When errorMessage is used, ajv returns a special error object, error.keyword='errorMessage', and keeps original validation errors in the error.errors field.

The proposed changes normalize the ajv validation errors by unwrapping the errorMessage error object, so the resolver returns original error types, such as required and pattern, while using custom error messages. The normalization also allows us to use errorMessage at the root level (fixes #615).

As you can see in the before and after outputs, the value of the type field and the keys of the types field use the original validation type instead of errorMessage.

Before

  "password": {
    "message": "One uppercase character",
    "ref": {
      "name": "password",
    },
    "type": "errorMessage",
    "types": {
      "errorMessage": "One uppercase character",
    },
  },

After

  "password": {
    "message": "One uppercase character",
    "ref": {
      "name": "password",
    },
    "type": "pattern",
    "types": {
      "pattern": "One uppercase character",
    },
  },

Example schema

const schema = {
  type: "object",
  properties: {
    username: {
      type: "string",
      minLength: 3,
      errorMessage: {
        minLength: "username should be at least three characters long",
      },
    },
    password: {
      type: "string",
      pattern: ".*[A-Z].*",
      minLength: 8,
      errorMessage: {
        pattern: "One uppercase character",
        minLength: "passwords should be at least eight characters long",
      },
    },
  },
  required: ["username", "password"],
  additionalProperties: false,
  errorMessage: {
    required: {
      username: "username field is required",
      password: "password field is required",
    },
  },
};

Input Value

const value = {
  username: "kt",
  password: "invalid",
};

Resolver Output

{
  "errors": {
    "password": {
      "message": "One uppercase character",
      "ref": {
        "name": "password"
      },
      "type": "pattern",
      "types": {
        "minLength": "passwords should be at least eight characters long",
        "pattern": "One uppercase character"
      }
    },
    "username": {
      "message": "username should be at least three characters long",
      "ref": {
        "name": "username"
      },
      "type": "minLength",
      "types": {
        "minLength": "username should be at least three characters long"
      }
    }
  },
  "values": {}
}

Input Value

const value = {};

Resolver Output

{
  "errors": {
    "password": {
      "message": "password field is required",
      "ref": {
        "name": "password"
      },
      "type": "required",
      "types": {
        "required": "password field is required"
      }
    },
    "username": {
      "message": "username field is required",
      "ref": {
        "name": "username"
      },
      "type": "required",
      "types": {
        "required": "username field is required"
      }
    }
  },
  "values": {}
}

When `errorMessage` is used, ajv returns a special error object,`error.keyword='errorMessage'`,
and keeps original validation errors in `error.errors` field.
The proposed changes normalze the ajv validation errors,
so the resolver returns original error types, like `required` and `pattern`
while using custom error messages.

This also fixes react-hook-form#615
@gwadson
Copy link

gwadson commented Jan 20, 2025

Hi @ktunador,
thanks for the fix. any idea when this will get merge? currently I had to transform errors manually but if the resolver can do that it will be great
thanks

@ktunador
Copy link
Contributor Author

Hi @ktunador, thanks for the fix. any idea when this will get merge? currently I had to transform errors manually but if the resolver can do that it will be great thanks

I’m not sure; someone with write access needs to approve this PR. Right now, We are using a local version of this PR in our projects.

Copy link
Member

@jorisre jorisre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution

@jorisre jorisre merged commit 5030a59 into react-hook-form:master Jan 27, 2025
4 checks passed
@gwadson
Copy link

gwadson commented Jan 28, 2025

Hi @jorisre
will this be on the next release ? because I didn't saw it on
https://github.com/react-hook-form/resolvers/issues?q=label%3A%22next%20release%22%20

Just trying to understand the processes of the project

Thank you for the efforts

@jorisre
Copy link
Member

jorisre commented Jan 28, 2025

Yes, it will be in the next release. I added the label to the issue :)

Copy link
Contributor

🎉 This PR is included in version 4.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ajv resolver not using custom error messages on required errors
3 participants