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)
})
})
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)
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
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().
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?
Most helpful comment
Is there any way to provide custom error message for
validvalidator?