Skip to content

Add option to resolve .ref() and .in() values in error messages. #2303

@SimonSchick

Description

@SimonSchick

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/no): No
  • is this issue affecting a production system? (yes/no): No

Context

  • node version: 12-13
  • module version: 17.1.0
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...):
  • any other relevant information:

What problem are you trying to solve?

Currently generated error messages contain [ref:...] when used with references, while this is reasonably useful when dealing with local refs (since the values are usually provided by the client), they are less useful when dealing with references that use context.

Use case:
I am providing some dynamic configuration via $.app.request.... and I'd rather display the value of the ref than the name of the ref in the validation messages as showing the ref name would expose internal information which the client has no use for.

const a = j.object({ a: j.valid(j.ref('$test')).required() }).validate({ a: 2 }, { context: { test: [3] } })

Current:

{
  value: { a: 2 },
  error: [Error [ValidationError]: "a" must be [ref:global:test]] {
    _original: { a: 2 },
    details: [ [Object] ]
  }
}

Desired:

{
  value: { a: 2 },
  error: [Error [ValidationError]: "a" must be 3] {
    _original: { a: 2 },
    details: [ [Object] ]
  }
}

Do you have a new or modified API suggestion to solve the problem?

I have 2 proposed solutions:

  • Allow passing an option resolveRefValuesInMessages (name debatable) in .validate/assert call.
  • Allow passing an option resolveValuesInMessage in the 2nd param of .in() and .ref().

eg.

const a = j.object({
  a: j.valid(j.ref('$test', { resolveValuesInMessage: true })).required()
}).validate({ a: 2 }, { context: { test: [3] } });

OR

const a = j.object({
  a: j.valid(j.ref('$test')).required()
}).validate({ a: 2 }, { context: { test: [3] }, resolveRefValuesInMessages: true });

Metadata

Metadata

Assignees

Labels

featureNew functionality or improvement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions