bleupen / halacious

a better HAL processor for Hapi
MIT License
108 stars 22 forks source link

How to handle ignored props with schema validation? #96

Closed Bazze closed 7 years ago

Bazze commented 8 years ago

Hey!

How do you handle ignored props when it comes to schema validation? Let's say I have the following hal config on my route:

{
    plugins: {
        hal: {
            prepare: (rep, next) => {
                rep.entity.result.forEach((event) => {
                    rep.embed('events', './' + event.id, event);
                });

                return next();
            },
            ignore: 'result',
        },
    }
}

And then a strict schema validation of the response like this:

{
    schema: Joi.object().keys({
        per_page: Joi.number().required(),
        page: Joi.number().required(),
        total: Joi.number().required(),
        _links: Joi.object().keys({
            self: Joi.object().keys({
                href: Joi.string().required(),
            }).required(),
            [...]
        }).required(),
        _embedded: Joi.object().keys({
            events: Joi.array().items(
                Joi.object().keys({
                    id: Joi.string().required().description('The Google Calendar event ID'),
                    title: Joi.string().required().description('The event title'),
                    [...],
                })
            ).required(),
        }),
    }),
}

So in my route handle I return an object with a result prop which is an array of objects that I embed using the prepare function. I also ignore result since I don't want it to be included. However, that gives me an error since result is not present in my schema validation.

Debug: internal, implementation, error
    Error: "result" is not allowed

...and I don't want result in my schema validation since that will cause it to show up in my swagger documentation.

Has anyone else had this issue, and found a solution for it? (I do not want to use config settings like stripUnknown or allowUnknown)

bleupen commented 8 years ago

Hi Sebastian!

Any luck with this? Currently, halacious runs during the onPreResponse (post validation) phase of the hapi request lifecycle. Because it it modifies the response payload, I think a good case could be made for bumping it up to onPostHandler, or at least making this a configuration setting. This should solve your problem.

Bazze commented 8 years ago

Hey @bleupen! That sounds like a reasonable solution. I'm actually not very familiar with HAPI, but I'll be building most of my new services with it. I discovered this issue during my PoC-ing phase but since I haven't really started using HAPI yet I haven't digged any further into this. I will definitely do so though, probably in a month or two, if it has not yet been resolved by then.

Bazze commented 7 years ago

Hello again @bleupen! I've started to work with HAPI now and coming back to this issue I realised that the response validation using the schema won't work at all, since the response validation sees the result prop and not the _embedded prop which I have in my schema (and what I want shown in my documentation).

I will start looking into the onPostHandler and see what can be done there. If you have any further pointers that could help me save time I would love to hear them.

Bazze commented 7 years ago

Closing this since the PR was merged a while ago 👍