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

Consistency with route configurations #39

Open
KrisSiegel opened this issue Aug 6, 2015 · 6 comments
Open

Consistency with route configurations #39

KrisSiegel opened this issue Aug 6, 2015 · 6 comments

Comments

@KrisSiegel
Copy link

I've been exploring a few different options for automatic schema validation and API documentation. I noticed both lout and hapi-swagger support the following configuration for routes:

server.route({
    method: "GET",
    path: "/something/{somethingId}",
    config: {
        description: "Get's a something",
        tags: ["api"],
        validate: {
            params: {
                somethingId: Joi.string()
            },
            headers: Joi.object({
                "authorization": Joi.string().required()
            }).options({ allowUnknown: true })
        },
        response: {
            schema: Joi.object({

            })
        }
    },
    handler: function (request, reply) {
        // ...
    }
});

But ratify seems to require a different type of configuration structure with everything under a plugins and then a ratify object.

Is there a reason for the different structure? Would it be possible to re-use the same structure that others use that don't specify the ratify plugin explicitly? I mostly just want an easy way to keep things consistent but have the option of changing out documentation and / or validations.

@mac-
Copy link
Owner

mac- commented Aug 6, 2015

Great question. This issue may give you some context.

At the time I built this module, they didn't allow other implementations to be used within the validate config.

With that said, I actually wouldn't mind changing the module to use the built in validate config, but I just haven't had time to update it.

@KrisSiegel
Copy link
Author

Understood. Thanks!

@mac- mac- added the question label Aug 6, 2015
@mac- mac- closed this as completed Aug 6, 2015
@KrisSiegel
Copy link
Author

With this ticket closed does that mean you won't end up moving to the built in config? Or will that go into a new ticket?

@mac-
Copy link
Owner

mac- commented Aug 6, 2015

I was going to create a new one, but I'll actually just leave this one open for some additional context

@mac- mac- reopened this Aug 6, 2015
@ozum
Copy link

ozum commented Dec 10, 2015

Hi,

I tried to write a plugin which would check if object given to config.validate is a JSON schema and act according to, but I failed. How is it possible even in latest version of Hapi? (v11.1.2 as of this message is written)

From hapi.js /lib/route.js line: 260

internals.compileRule = function (rule) {

    // null, undefined, true - anything allowed
    // false - nothing allowed
    // {...} - ... allowed

    return (rule === false ? Joi.object({}).allow(null)
                           : typeof rule === 'function' ? rule
                                                        : !rule || rule === true ? null                     // false tested earlier
                                                                                 : Joi.compile(rule));
};

If you provide JSON schema as an object to standard config.validate.payload etc. of a route, hapi directly tries to convert it to JOI object. This is also clearly stated in API docs of hapi. As a result you cannot access bare JSON schema even in onPostAuth, since it is already converted as below:

{ options: {},
  params: 
   { isJoi: true,
     _type: 'object',
     _settings: null,
     _valids: { _set: [] },
     _invalids: { _set: [] },
     _tests: [],
     _refs: [],
     _flags: {},
     _description: null,
     _unit: null,
     _notes: [],
     _tags: [],
     _examples: [],
     _meta: [],
     _inner: 
      { children: [Object],
        renames: [],
        dependencies: [],
        patterns: [] } },
  payload: null,
  headers: null,
  query: null }

I will also open a ticket about this in hapi repository, but I will appreciate if you suggest any ideas how to achieve this in current version?

@ozum
Copy link

ozum commented Dec 10, 2015

For update. I asked in hapi repository and got an answer. It seems impossible according to hapijs/hapi#2967.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants