-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option to show parameter names in Validation Errors #32
Add option to show parameter names in Validation Errors #32
Conversation
This information should already be present in the error itself. For example: "name": "ValidationError",
"details": [
{
"message": "lastName is required",
"path": "lastName",
"type": "any.required"
}
],
"_object": {
"firstName": "John",
"age": 45,
"tags": [
"man",
"human"
]
}
} Are you seeing something different? |
Yes, as mentioned it is always In the PR I submitted you should see I have added tests for the new functionality. If you remove the implementation I created, but leave the tests you will see that they fail (because it expects to see the parameter name in the error message.) |
The snippet I posted above is from |
I see what's going on now. When a validation occurs for a parameter, So, on the face of it, I see nothing wrong with modifying the error object in this way. I don't think you actually need to make this optional, since this should be expected behavior (since we do know the context). If you would, please modify the PR to update the error object always, and ensure it modifies Sound like a plan? |
Example: if (error) {
error.message = error.message.replace('value', parameter.name);
error.details.forEach(function (detail) {
detail.message = detail.message.replace('value', parameter.name);
detail.path = parameter.name;
});
utils.debuglog('%s', error.message);
callback(error);
return;
} |
Thanks, this is done as suggested, with some extra tests. |
@@ -31,6 +31,7 @@ Options: | |||
- `handlers` - either a directory structure for route handlers or a premade object (see *Handlers Object* below). | |||
- `basedir` - base directory to search for `handlers` path (defaults to `dirname` of caller). | |||
- `schemas` - an array of `{name: string, schema: string|object}` representing additional schemas to add to validation. | |||
- `useNamedValidators` - whether to show parameter names in validation errors (defaults to false). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this now that it is not necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops, will do.
👍 |
…tors Add option to show parameter names in Validation Errors
Implementation for #31
Adds a
useNamedValidators
property to the options object.If this value is set to
true
then all ValidationError messages will show the name of the parameter that was being validated.For backwards compatibility, if no
useNamedValidators
property is set, then 'value' is used as before (ie, the property defaults to false).