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

Object/named array in query string #88

Closed
tomaskallup opened this issue May 23, 2019 · 4 comments
Closed

Object/named array in query string #88

tomaskallup opened this issue May 23, 2019 · 4 comments

Comments

@tomaskallup
Copy link

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
Copy link

giall commented May 24, 2019

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

@tomaskallup
Copy link
Author

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

@3imed-jaberi
Copy link
Member

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
Copy link
Member

@aheckmann, please close this issue.

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

No branches or pull requests

4 participants