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

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ktunador
Copy link

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ajv resolver not using custom error messages on required errors
1 participant