Skip to content

Access context in external validator #2367

@Ali-Dalal

Description

@Ali-Dalal

Support plan

  • which support plan is this issue covered by? (e.g. Community, Core, Plus, or Enterprise): Community
  • is this issue currently blocking your project? yes
  • is this issue affecting a production system? no

Context

  • node version: 12
  • module version: 17.1.1
  • environment (e.g. node, browser, native): node
  • any other relevant information: I wrote a custom external validator but I got a problem where I need to throw a Joi validation error. the issue is I am not able to access the context in order to pass labels and keys to the JoiError instance.
// I need to throw similar error to this one so the missing part is the context. in external validator, I know nothing about the current rule (only value) but the key, orignal, and the label are not known
[Error [ValidationError]: "student_id" is not allowed] {
  _original: {
    subject: 'this is the subject',
    student_id: 15
  },
  details: [
    {
      message: '"student_id" is not allowed',
      path: [ 'student_id' ],
      type: 'object.unknown',
      context: {
        child: 'student_id',
        label: 'student_id',
        value: '15',
        key: 'student_id'
      }
    }
  ]
}

How can we help?

is there any way to access the context of the current validation rule? something like this

// pass context as another argument
joi.external(async (value, context) => {
            record = await Model.query().findOne(column, value);
            if (!record) {
                throw new JoiError('Record does not exist', [{
                    message: `resource "${value}" doesn't exist in relation ${Model.name}`,
                    type: 'any.exist',
                          context: {
                                  ...context
                          }
                }]);
        }
    })

Metadata

Metadata

Assignees

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