Skip to content
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

Include field information in errors #12

Closed
dschenkelman opened this issue Oct 8, 2014 · 2 comments
Closed

Include field information in errors #12

dschenkelman opened this issue Oct 8, 2014 · 2 comments

Comments

@dschenkelman
Copy link
Contributor

Issue

Error messages on failed types do not include property name.

Example

Currently, the following validation:

ratify: {
  payload: {
    type: "object",
    properties: {
      outer: {
        type: "object",
        properties: {
          inner:{
            type: "string"
          }
        }
      }
    }
  }
}

Together with this payload:

{
  "outer": {
    "inner": false
  }
}

Results in this response:

{
    "statusCode": 400,
    "error": "Bad Request",
    "message": "payload parameters validation error: invalid type: boolean (expected string)"
}

Proposal

The available information when creating the error message is:

{ code: 'INVALID_TYPE',
  message: 'invalid type: boolean (expected string)',
  path: '#/outer/inner',
  params: { expected: 'string', type: 'boolean' } }

The current code looks like this:

if (!report.valid) {
  errorMessages = 'payload parameters validation error: ' +
          _.map(report.errors, function(error) { console.log(error); return error.message; }).join(', ');
  return next(plugin.hapi.error.badRequest(errorMessages));
}

If it were updated to something like this it would provide more detailed error messages:

if (!report.valid) {
  errorMessages = 'payload parameters validation error: ' +
      _.map(report.errors, function(error) { 
        var message = error.message;
        message += ' - on ';
        // handle array case
        message += error.path.substr(2).replace(/\//g, ".").replace(/\.\[/g, "[")
        return message;
      }).join(', ');
@dschenkelman
Copy link
Contributor Author

BTW, I will implement it if you give me the 👍

@mac-
Copy link
Owner

mac- commented Oct 8, 2014

Yeah, go for it! Thanks

dschenkelman added a commit to dschenkelman/ratify that referenced this issue Oct 9, 2014
dschenkelman added a commit to dschenkelman/ratify that referenced this issue Oct 9, 2014
dschenkelman added a commit to dschenkelman/ratify that referenced this issue Oct 9, 2014
@mac- mac- closed this as completed in 59fc55b Oct 9, 2014
mac- added a commit that referenced this issue Oct 9, 2014
Fix #12. Adds friendly path to property on errors
mac- added a commit that referenced this issue Oct 9, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants