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

Non Joi object in 'config.validate' of routes #2967

Closed
ozum opened this issue Dec 10, 2015 · 4 comments
Closed

Non Joi object in 'config.validate' of routes #2967

ozum opened this issue Dec 10, 2015 · 4 comments
Assignees
Labels
feature New functionality or improvement

Comments

@ozum
Copy link

ozum commented Dec 10, 2015

Hi,

Sorry for long post. As documented, route's config.validate.xxx expects a function, true, false or a Joi validation object. Other validation plugins such as JSON schema validators etc. need to provide a function and options key, or plugin details in route config.

Reading #1039 I would like to write JSON schema validator plugin and let users provide schema in validate section as below:

server.route([{
    method: 'GET',
    path: '/accounts',
    handler: getAccount,
    config: {
        validate: {
            params: { '$schema': '...', properties: { id: { type: 'integer' } } }
        }
    }
}])

However it is not possible to write a plugin which would use config.validate with an object.

hapi/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));
};

Providing JSON schema as an object to config.validate.param, hapi converts it to Joi object (even it is not a Joi object) which is expected as seen hapi's API docs. 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 }

My question is: How to not to touch config.validate object if it is not a valid Joi object?

If this isn't possible yet, I would gladly make a pull request to hapi. However IMHO this also touches Joi.

My backwards compatible suggestion for hapi would be using an imaginative method from Joi.

hapi/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.isValidJoiObject(rule) ? rule
                                                                                  : Joi.compile(rule));
};

hapi/lib/validation.js line: 130

//    if (typeof schema !== 'function') {
    if (Joi.isValidJoiObject(schema)) {
        return Joi.validate(request[source], schema, localOptions, postValidate);
    }

I can't think of an efficient way for this imaginative Joi.isValidJoiObject method. Perhaps @Marsup would suggest.

@ozum ozum changed the title Non Joi object in config.validate of routes Non Joi object in 'config.validate' of routes Dec 10, 2015
@Marsup
Copy link
Contributor

Marsup commented Dec 10, 2015

Unlikely to happen, anything in javascript is a valid joi object. Your best bet is to put your stuff in the plugins part of the route and have your plugin process that.

@ozum
Copy link
Author

ozum commented Dec 10, 2015

Thank you for fast response @Marsup. I will put my effort to that direction then.

Regards,

@hueniverse
Copy link
Contributor

If you want to store some other validation config, just use the app config key and put your own validation logic there, then handle it with a plugin.

@hueniverse hueniverse self-assigned this Dec 10, 2015
@ozum
Copy link
Author

ozum commented Dec 11, 2015

Thanks.

@Marsup Marsup added feature New functionality or improvement and removed request labels Sep 20, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Mar 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

3 participants