You may be interested in this library I just published:
Watch this space, I’ll be open sourcing through work a full-on openAPI validation generator in about a month and a half or so.
Nice. Just added your project to my watch list :)
Perhaps you are already acquainted with Aeson from Haskell, but if you’re not, I’d recommend checking it out. It features very powerful datatype generic encoding/decoding to/from JSON.
Ok! I’ll be sure to link back to the openApi project in the exonerate readme
This is also something I’ve been using Ecto for (and validating a JSON body). It works pretty well and it’s nice to have to learn One Less Thing sometimes.
Is your code reusable? Could you share it?
The technique is so simple that I doubt it needs a separate reusable library on top of Ecto. Just implement a module with the schema and validator functor for each type you want to validate. E.g. here’s a code extract from one of my experiments a few years ago. It looks remarkably similar to OP:
defmodule RedemptionApiWeb.CaptureRequest do
@required [:redemption_id, :member_id, :program_id, :miles]
schema "CaptureRequest" do
}) :: Request.t()
|> cast(params, @required)
|> validate_length(:redemption_id, min: 1)
I don’t remember the details, but I ran into a use case where Ecto’s validation wasn’t sufficient. I used Litmus instead: https://github.com/lob/litmus
https://lobste.rs/u/studzien and I have been using our home-grown parser/constructor library to good effect, for similar use cases.