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

Question: How to validate payload with templated response properly ? #1523

Closed
marioosh-net opened this issue Mar 25, 2014 · 4 comments
Closed
Assignees
Labels
feature New functionality or improvement support Questions, discussions, and general support
Milestone

Comments

@marioosh-net
Copy link

I need to validate some inputs with accessing to database. I did it using payload key. After unsuccesful validation need to return view with the same form ('profile' form) and information what is wrong to mark not valid form fields. In payload function i know which fileds are not valid, but don't know how to pass this to failAction and then. I hope You know what i mean. How to do that properly ?

By the way, idea of #1498 would be great.

validate: {
    payload: function(payload, options, next){
        var notvalid = []; // array of not valid fields of form
        // access db
        // ....        
        return next(notvalid.length>0?true:false);
    },
    failAction: function(source, error, next) {
        error.form = 'profile'; // save source form name
        next(error);
    }    
}

server.ext('onPreResponse', function (request, reply) {
    var response = request.response;
    if (!response.isBoom) {
        return reply();
    }
    if(response.form == 'profile') {
        /* validation error in profile form */
        reply.view('profile', {.....});
    }
});
@hueniverse
Copy link
Contributor

In the validation function, instead of returning true for error, return a new Error(). You can set any properties you want on that error and those will show up in the onPreResponse function. I don't think you need failAction here.

@marioosh-net
Copy link
Author

Thank You for response.
I did as You said, but in onPreResponse function i get lost keys setted before. I did using Hapi.error (with and without error.reformat();), but custom key lost too. In the second approach message "survive". Something wrong with that ?

This is what i got in onPreResponse:

/* new Error() */
{
 "isBoom": true,
 "data": null,
 "output": {
  "statusCode": 400,
  "payload": {
   "statusCode": 400,
   "error": "Bad Request",
   "validation": {
    "source": "payload",
    "keys": []
   }
  },
  "headers": {}
 }
}
/* Hapi.error.badRequest(...) */
{
 "isBoom": true,
 "data": null,
 "output": {
  "statusCode": 400,
  "payload": {
   "statusCode": 400,
   "error": "Bad Request",
   "message": "Cannot feed after midnight",
   "validation": {
    "source": "payload",
    "keys": []
   }
  },
  "headers": {}
 }
}
validate: {
    payload: function(payload, options, next){
            /** always error, just for test **/
            var error = new Error();
            error.form = 'profile';
            return next(error);

            /** trying with Hapi.error  - the same effect, custom key lost **/
            var error = Hapi.error.badRequest('Cannot feed after midnight');
            error.output.statusCode = 499;    // Assign a custom error code
            error.reformat();
            error.output.payload.custom = 'abc_123'; // Add custom key
            next(error);
    }
}

@hueniverse
Copy link
Contributor

It is expecting the payload validation function to return an error compatible with joi. This means it is going to use the message and details properties only when creating a new error object internally. I can see how this can be very limiting for you use case. Let me see what I can do to pass along the original error from joi or custom code.

@hueniverse hueniverse reopened this Mar 29, 2014
@hueniverse hueniverse added this to the 3.1.0 milestone Mar 29, 2014
@hueniverse hueniverse self-assigned this Mar 29, 2014
@hueniverse
Copy link
Contributor

You will find your own error under err.data.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New functionality or improvement support Questions, discussions, and general support
Projects
None yet
Development

No branches or pull requests

2 participants