api-platform / core

The server component of API Platform: hypermedia and GraphQL APIs in minutes
https://api-platform.com
MIT License
2.44k stars 872 forks source link

[RFC] Json Schema Validations #2131

Open ragboyjr opened 6 years ago

ragboyjr commented 6 years ago

What would you think about allowing JSON Schema Validations for API Resources that we could then expose inside of the generated OpenAPI documentation file?

Most of the basic symfony validations like Assert\Blank, Assert\Length etc are values that could be defined via the json schema vocabulary and a library like https://github.com/justinrainbow/json-schema would be able to parse and validate those definitions.

I don't think this would obviate the need for symfony validations because you might still want to use a custom validation rule or maybe check the db for uniqueness, but a lot of the simple stuff could easily be written as json schema validations.

ragboyjr commented 6 years ago

I'd be willing to put in the effort coding wise, but not sure if this idea would even be worth adding/maintaining for you guys?

dunglas commented 5 years ago

For the record I'm, 👍 for this!

ragboyjr commented 5 years ago

Implementation wise, what do you think about caching the normalized swagger documentation? We could keep cached instances for v2 and v3?

I'm thinking we create an interface for accessing the normalized docs

interface DocumentationRepository {
  public function getDocs(string $type): array;
}

where type is swagger or openapi_v3.

and then a caching decorator implementation of that, and another that generates the Documentation object and normalizes it accordingly.

We could then use that normalized data to grab the definition per entity and run the json schema validator on it.

We might even want to add a ChainValidator so we can always enforce json schema, and optionally run SF validations on top of it.

ragboyjr commented 5 years ago

hmm, one thing to mention that the normal API Platform validations occur on kernel.view which is after the deserialization step. Part of me wonders if the json schema validations should run on the raw JSON data BEFORE deserialization.

This could (possibly) mess up the various deserialization formats API Platform supports. It's almost as if, we should decode the value, then run JSON schema validation, then deserialize into the DTO or Entity object accordingly.

Tchinkatchuk commented 5 years ago

I do validate the need. If any JSON schema validation could be done as soon as possible, the performance & quality of the response will be improved.

fherbin commented 3 years ago

@ragboyjr Are you making progress on this subject, because it's very interesting?

ragboyjr commented 3 years ago

@fherbin I haven't made much progress on it, I think I hit that road block mentioned on Dec 22, didn't receive any feedback and moved on.

soyuka commented 3 years ago

See also https://github.com/api-platform/core/issues/3828. For the documentation cache I'd suggest using a file that you previously dump as it should be sufficient. Note that on the master branch the OpenApi code has been improved (use the OpenApiFactory).

er1z commented 1 month ago

Any updates?