Joi: Unable to use .message() with equal() or valid()

Created on 9 Nov 2019  路  4Comments  路  Source: sideway/joi

Support plan

  • which support plan is this issue covered by? Community
  • is this issue currently blocking your project? yes
  • is this issue affecting a production system? no

Context

  • node version: v12.6.0
  • module version with issue: @hapi/joi ^16.1.7
  • last module version without issue: N/A
  • environment (e.g. node, browser, native): nodejs
  • used with (e.g. hapi application, another framework, standalone, ...): standalone (used for lamda request validation)
  • any other relevant information: Working with regexp

What are you trying to achieve or the steps to reproduce?

I try to use .message() after .equal() or .valid() or .invalid(). A simple use case is when you have an API_KEY you want to hidde. If you don't set the message you are receiving:

WRONG_API_KEY must be equal to VALUE_API_KEY_SECRET

Simple use cases with mocha

describe("Test suite 1", () => {
    it("Test 1", () => {
        const schema = Joi.string().required().equal("VALUE_API_KEY_SECRET").message("Invalid API_KEY")
        schema.validate("WRONG_API_KEY")
    })

    it("Test 2", () => {
        const schema = Joi.string().required().valid("VALUE_API_KEY_SECRET").message("Invalid API_KEY")
        schema.validate("WRONG_API_KEY")
    })

    it("Test 3", () => {
        const schema = Joi.boolean().required().valid(true).message("Invalid boolean")
        schema.validate(false)
    })
})

What was the result you got?

 1) Test suite 1
       Test 1:
     Error: Cannot apply rules to empty ruleset or the last rule added does not support rule properties
      at new module.exports (node_modules\@hapi\hoek\lib\error.js:23:19)
      at module.exports (node_modules\@hapi\hoek\lib\assert.js:20:11)
      at internals.Base.rule (node_modules\@hapi\joi\lib\base.js:462:9)
      at internals.Base.method [as message] (node_modules\@hapi\joi\lib\extend.js:145:29)
      at Context.<anonymous> (test\Index.test.ts:88:78)
      at processImmediate (internal/timers.js:439:21)

  2) Test suite 1
       Test 2:
     Error: Cannot apply rules to empty ruleset or the last rule added does not support rule properties
      at new module.exports (node_modules\@hapi\hoek\lib\error.js:23:19)
      at module.exports (node_modules\@hapi\hoek\lib\assert.js:20:11)
      at internals.Base.rule (node_modules\@hapi\joi\lib\base.js:462:9)
      at internals.Base.method [as message] (node_modules\@hapi\joi\lib\extend.js:145:29)
      at Context.<anonymous> (test\Index.test.ts:93:78)
      at processImmediate (internal/timers.js:439:21)

  3) Test suite 1
       Test 3:
     Error: Cannot apply rules to empty ruleset or the last rule added does not support rule properties
      at new module.exports (node_modules\@hapi\hoek\lib\error.js:23:19)
      at module.exports (node_modules\@hapi\hoek\lib\assert.js:20:11)
      at internals.Base.rule (node_modules\@hapi\joi\lib\base.js:462:9)
      at internals.Base.method [as message] (node_modules\@hapi\joi\lib\extend.js:145:29)
      at Context.<anonymous> (test\Index.test.ts:98:61)
      at processImmediate (internal/timers.js:439:21)

What result did you expect?

I expect to have .message() working after theses methods. It is working if I use:

const schema = Joi.string().required().regex(new RegExp(`^${API_KEY}$`)).message("Invalid API_KEY")

For boolean I was obliged to use a simple if(myboolean)

Thanks a lot for your job.

Have a good weekend

support

Most helpful comment

Is there any way to provide custom error message for valid validator?

All 4 comments

I also experiencing this issue with .when method, any help appreciated, thank you.

The error is pretty clear: last rule added does not support rule properties. You cannot use message() with flag based rules which valid() is one of. You have to use messages().

https://hapi.dev/family/joi/?v=16.1.7#anyruleoptions

maybe the one that is not clear is what to use then, I don't know it should use messages().

if I use messages how I differentiate each of the validation error message? I use message() because I need to make my own error message, thank you.

Is there any way to provide custom error message for valid validator?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chrisegner picture chrisegner  路  4Comments

neroaugustus1 picture neroaugustus1  路  4Comments

jamesdixon picture jamesdixon  路  4Comments

a-c-m picture a-c-m  路  3Comments

kevbook picture kevbook  路  4Comments