Skip to content

Custom error context with references #3036

Open
@kostas-pblworks

Description

@kostas-pblworks

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    supportQuestions, discussions, and general support

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions