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

Include the supplied value in the validation errorHash that is passed to createValidationError #1718

Closed
fiznool opened this issue Mar 20, 2020 · 4 comments · Fixed by #2614
Closed

Comments

@fiznool
Copy link
Contributor

fiznool commented Mar 20, 2020

I've found myself wanting to create a custom ValidationError that includes the value that was validated alongside the error message. For example, if I have the following JSON Schema:

{
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "minLength": 5
    }
  }
}

And I try to validate the following:

{
  "name": "abcd"
}

I'd like the exception to contain the following information:

{
  "name": [
    {
      "message": "should be at least 5 characters",
      "keyword": "minLength", 
      "params": {
        "limit": 5
      },
      "data": "abcd"  // <-- the submitted value
    }
  ]
}

The ajv library will add a 'data' property to its error object if the verbose option is set. The implementation in Objection currently takes the message, keyword and params keys, so even if the verbose option is set via a custom Ajv instance, the data property won't be propagated to the createValidationError static method.

Would you be open to supporting this functionality, either by default or as an opt-in config flag?

Alternatively if there is another way I can achieve this goal, please let me know.

Thanks again for a great library 😄

@fiznool
Copy link
Contributor Author

fiznool commented Apr 9, 2020

Just bumping this.

Happy to implement a PR, but don't want to do so if it's unlikely to be accepted!

@lehni
Copy link
Collaborator

lehni commented Apr 15, 2023

I'd be happy to review a PR for this!

@cesumilo
Copy link
Contributor

I'd be happy to review a PR for this!

Here it is 😉

cesumilo added a commit to inarix/objection.js that referenced this issue Mar 1, 2024
@lehni
Copy link
Collaborator

lehni commented Mar 3, 2024

Thanks @cesumilo !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants