-
Notifications
You must be signed in to change notification settings - Fork 30
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
Validate subkey #6
Comments
Yes, you're right. Slim-Validation doesn't know about your json request, but we could add some kind of logic to also validate it. What do you think? |
somethign like this |
If you want you can prepare a PR ;) |
will do :) |
The problem is without knowing the dimension of the array traversing the elements becomes more expensive. `
|
Thanks for the suggestion @Lo5t . The problem with that snippet is that it won't work anymore with the old style ( |
@Lo5t ping |
It would be best to integrate json-schema for real JSON validation: https://github.com/justinrainbow/json-schema A Slim middleware implementation is found here: https://github.com/moffe42/json-schema-middleware One typically has both standard request parameters (query, form parameters in the body) and JSON in the body so it makes sense to merge both approaches into one. I wouldn't bother doing some minimal JSON inspection, it will only marginally help and a more thorough solution is available as JSON Schema. |
Hi @achimha, thanks for your comment. I think that we could create another |
I agree with I'd very much appreciate JSON Schema support! |
Ok nice, I'll see if I could create a solution in the next days. Anyway did you try the solution proposed here? |
Yes, I saw that. It's a lot of typing and it only covers very simple cases. You can have a lot of optional values, arrays with certain allowed sizes, etc. Your example mentioned above uses a function injected into the container. I think that is a bit inflexible as you normally have many APIs with different parameters and I believe it would be beneficial to have a middleware that you parameterize for each request, something like this: $this->post('/myroute', function(Request $request, Response $response) {
return $response
->withHeader('Content-Type', 'application/json')
->withStatus(myRequestHandler($request->getParsedBody(), $request->getQueryParams()));
})->add(new TheMiddleware({
"validation" => "parameters",
"defining" => "how to validate this route"
}); |
@achimha Be free to create a PR for this 😄 |
I didn't expect any other answer... I'm not using the library at this point, planning to find a solution in the next few weeks so I can't provide anything today. Why did you go the way of injecting validation methods into the container instead of adding them as middleware? Any specific reason? |
The only reason that I could find is: ignorance. You're right about it, it should be something directly accessible in the |
I believe the cleanest approach is to use what they call "invokable class middleware": http://www.slimframework.com/docs/concepts/middleware.html You'd just do
and parametrize it there. If there was a class representing JSON Schema, you would just define the schema in the constructor of that middleware and attach it to the route. |
Did you check the source code of Validation.php? You can see that it is already an "invokable class middleware". What you can't find in the documentation is how to improve the request/response object adding a |
How can I validate sub keys
{"type":"emails", "objectid":"1", "email":{ "id":1, "enable_mapping":"1", "name":"rq3r", "created_at":"2016-08-23 13:36:29", "updated_at":"2016-08-23 14:36:47" } }
I would like to validate the email object.
If I understand correctly then its merely top level
$param = $request->getParam($key);
The text was updated successfully, but these errors were encountered: