Validating JSON query string as query param using Joi

Viewed 5105

I have a problem of validation JSON that is passed in GET request query param as a serialized string.

What I need to achieve is to parse this serialized string back to JSON and validated it using Joi.

Example: Give is the JSON

{
 limit: {size:10, page:0},
 filter: {filter_by: 'foo', filter_val: 'foo', from: '1/1/2016',to: '1/1/2016' }
}

And this JSON is converted to query string is:

limit%5Bsize%5D=10&limit%5Bpage%5D=0&filter%5Bfilter_by%5D=foo&filter%5Bfilter_val%5D=foo&filter%5Bfrom%5D=1%2F1%2F2016&filter%5Bto%5D=1%2F1%2F2016

I need something like this to check:

 validate: {
          query: {
            limit: Joi.someMethodToGetJsonFromString.object().keys({
              size: Joi.number(),
              page: Joi.number()
            }
          filter: Joi.someMethodToGetJsonFromString,.object().keys({
              filter_by: Joi.string().valid(['option1', 'option2']),
              filter_val: Joi.string(),
              from: Joi.date(),
              to: Joi.date(),
            }
        }

Is there anything in Joi that can help in this scenario, Or I need to write custom validation functions for it.

1 Answers
Related