We have an OpenAPI spec and we’d like to validate API payloads using dry-validation. The source of truth is the YAML file (OpenAPI) and dry-validation/dry-schema would be our representation within Ruby.
The research I’ve made yield no results for that. There examples where the rules are specified as dry-schema and then exported to
openapi.yml [link], or someone is testing the OpenAPI spec itself [link].
The main problem is creating the schema and not duplicating what’s already in the YAML file. I would like to construct the AST based on the YAML content, but I’m afraid there’s no way of doing that with public methods (at least I couldn’t find it).
I’m considering that maybe the way forward would be to define some basic structure and then load data from the file, e.g (pseudo-code, not sure how it’d look like in the end):
class PersonContract < Dry::Validation::Contract json do require(:data).hash do MyClassThatProcessesOpenApi.new("openapi.yml", self).rules_for(:person, :data) end end end
I predict that would become cumbersome real quickly. What would be recommended way here?