koajs / joi-router

Configurable, input and output validated routing for koa
MIT License
450 stars 96 forks source link

Object/named array in query string #88

Closed tomaskallup closed 3 years ago

tomaskallup commented 5 years ago

I wanted to validate a object in query string (?deactivated[from]=2019-05-23T08:18:45.283Z&deactivated[to]=2019-06-12T08:18:45.283Z) So I went ahead and tried:

query: {
  deactivated: {
    from: Joi.date(),
    to: Joi.date(),
  },
}

But the request just prints out ("deactivated[from]" is not allowed).

I have discovered that ?deactivated={"from": "2019-01-01T08:18:45.283Z"} works, but I would like the use the first approach, as that's how objects are converted to query string by default. Is there any way I can get this behaviour?

From further testings, if I define the query like so:

query: {
  "deactivated[from]": Joi.date(),
  "deactivated[to]": Joi.date(),
}

It seems to work, but that's not a solution, the variable names are unusable.

giall commented 5 years ago

Could you try deactivated['from'] or deactivated.from in the query string? Let me know if that works!

tomaskallup commented 5 years ago

Neither of those works, still getting "deactivated.from" is not allowed and "deactivated.['from']" is not allowed.

3imed-jaberi commented 3 years ago

You need to use parse_str function from locutus here. Or create you one with same behavoir.

example:

// const parse_str = require('locutus/php/strings/parse_str')
import parse_str from 'locutus/php/strings/parse_str'

const query_result = {}
const query_string = "?deactivated[from]=2019-05-23T08:18:45.283Z&deactivated[to]=2019-06-12T08:18:45.283Z".slice(1)
parse_str(query_string, query_result)

console.log(query_result);

// {
//   deactivated: { 
//     from: '2019-05-23T08:18:45.283Z',
//     to: '2019-06-12T08:18:45.283Z'
//   }
// }
3imed-jaberi commented 3 years ago

@aheckmann, please close this issue.