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

Custom error context with references #3036

Open
kostas-pblworks opened this issue May 12, 2024 · 0 comments
Open

Custom error context with references #3036

kostas-pblworks opened this issue May 12, 2024 · 0 comments
Labels
support Questions, discussions, and general support

Comments

@kostas-pblworks
Copy link

Runtime

node.js

Runtime version

20

Module version

17.13

Used with

standalone

Any other relevant information

No response

How can we help?

I would like to add custom properties to the context object of the returned error.

Given the following schema:

const projectSchema = object.keys({
  id: number.required(),
  description: string.required(),
  records: array.items(object.keys({
    id: number.required(),
    title: string.required(),    
  })).min(2).required()
})

I would like to automatically annotate the returned errors with their respective parents' ID. e.g.

[
  {
    message: '"description" is not allowed to be empty',
    path: [ 'description' ],
    type: 'description.empty',
    context: {
     **projectId: 12**
      label: 'title',
      value: '',
      key: 'title'
  },
  {
    message: '"title" is not allowed to be empty',
    path: ['records', '0', 'title' ],
    type: 'string.empty',
    context: {
     **recordId: 21**
     **projectId: 12**
      label: 'title',
      value: '',
      key: 'title'
  }
]

I've tried something like this already but doesn't seem to be working:

title: string
     .error((err) => {
     err.forEach((err) => {
        err.local.msg = Joi.ref('id')
     })
     return err
  })
  .required(),

Thank you!

@kostas-pblworks kostas-pblworks added the support Questions, discussions, and general support label May 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Questions, discussions, and general support
Projects
None yet
Development

No branches or pull requests

1 participant