error
error(message, [ctx])
Effect
Set the custom error message when validate failed.
By default, the error message is friendly only to the developer. If you want to use custom error message, call error
.
How To Use
You can call error
any number of times you like. Every time you call error
, it will set a custom error message for the restrict before the position you call error
.
Arguments
message
(string | function)
- The custom error message. If it's a function, the function should return a string, respect the custom error message. The function has an argument: the original validate error object.[ctx]
(*) - The execution context ofmessage
when it's a function.
ATTENTION
In the version 1.2.x and before, the function has an argument: the original validate error message string. It's a string, not an object.
Example
const schema = racoon
.string()
.error('Name should be a type of string')
.required()
.error('Name is required');
schema.validate(1); // fail, error message is 'Name should be a type of string'
schema.validate(null); // fail, error message is 'Name is required'
Last updated
Was this helpful?