Custom Error Messages
Is there currently a mechanism where I could pass an optional argument to a validation rule that if the test fails, it would be used as the message value of the error?
The built in error messages are sometimes not very friendly. Is this something that I can already do and just missed it in the docs?
Is this a matter of fixing the current error message?
A little of column a a little of column b. I would like the ability to express the error messages like so:
name[first] cannot be blank
name[last] cannot be blank
I am using Joi to validate form data in an express app and I would like to be able to send messages back to the client that are formatted more to how a form would be expressed.
That being said, some of the built-in messages I've encountered aren't very helpful... Having a rule that something is a Number type and have the value be "" ends up returning a message like "not valid" which isn't super helpful if you are a user.
Is this something I should try to do myself or is someone else tackling it? Or is there the third option that I'm just crazy :beer:
This is a reasonable request but we don't have anyone working on yet. You can give it a try and submit a PR.
This looks a little too complicated for me to do in my free time. I have an idea of what I want to do, but I'm not sure where to start to make it happen. I was thinking that we should send the rule that failed into internals.Errors.prototype.add that way we could access notes, tags, description, and in my case, printPath when creating the error messages.
This wouldn't be so bad if all I had to change was "base.js", but I would have to make changes to ALL the other types as well. The location where "errors.add" is invoked seems to be very different from type to type so I'm a little lost.
Any suggestions?
How about spec out the interface with some examples?
Sample Schema
var schema = {
password: T.String().regex(/[a-zA-Z0-9]{3,30}/).path('super_password'),
birthday: T.Object({
year: T.Number().min(1850).max(2012).path('birthday[year]'),
day: T.Number().min(1).max(31).path('birthday[day]')
}).required()
};
I suggest adding a .path() function to "base.js". While I was in there, I noticed other functions like "tags" and "description" and I assume they are there for things like this, to control the error output. The .path function would then attach a value to this that can be used later during error printing.
I also suggest updating the function signature for internals.Errors.prototype.add so it will take the validation rule that failed.
internals.Errors.prototype.add = function (message, path, rule)
By exposing the rule in this function, we would have access to all of the values that get attached via .path() or .notes() and any other functions that we might add down the line.
We could even group the information for error handing into a separate object literal as well to keep the this object clean.
internals.BaseType.prototype.path = function(path) {
Utils.assert(typeof path === 'string', 'Validator path must be a string');
this._errorPrint = this._errorPrint || {};
if (path) {
this._errorPrint.path = path;
}
return this;
}
Something along those lines. While it would be nice to be able to override the built in error messages, this will be a great first step in having more control over error message output.
I am using Hapi with server side Joi validation and also just ran across some unfriendly messages I would like to customize.
validate: {
payload: {
name: Hapi.types.String().required().min(1).max(100),
email: Hapi.types.String().required().email(),
password: Hapi.types.String().min(8).max(100)
}}
If I leave the name, email, and password fields blank on my signup form, this is the error message that is returned:
the value of name is not allowed to be , the value of email is not allowed to be , the value of password is not allowed to be
I tracked this down to https://github.com/spumko/joi/blob/master/lib/types/base.js
errors.add('the value of ' + key + ' is not allowed to be ' + this.__invalids._values[this.__invalids._values.indexOf(value)], keyPath);
Perhaps all the error messages could be in a JSON language file instead of hard coded into the functions. In addition to allowing overriding English wording, this would also allow for internationalization via adding extra JSON configuration files or programmatically via direct object manipulation.