1. 12
  1.  

  2. 3

    You may be interested in this library I just published:

    https://hexdocs.pm/exonerate/0.1.1/Exonerate.html

    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.

    1. 1

      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.

      1. 1

        Ok! I’ll be sure to link back to the openApi project in the exonerate readme

    2. 1

      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.

      1. 1

        Is your code reusable? Could you share it?

        1. 1

          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
            use Ecto.Schema
            import Ecto.Changeset
            alias RedemptionApiWeb.Request
          
            @required [:redemption_id, :member_id, :program_id, :miles]
          
            schema "CaptureRequest" do
              field(:redemption_id, :string)
              field(:member_id, :string)
              field(:program_id, :string)
              field(:miles, :integer)
            end
          
            @spec validate(%{
                    redemption_id: String.t(),
                    member_id: String.t(),
                    program_id: String.t(),
                    miles: String.t()
                  }) :: Request.t()
            def validate(params),
              do:
                %__MODULE__{}
                |> cast(params, @required)
                |> validate_required(@required)
                |> validate_length(:redemption_id, min: 1)
                |> Request.validate_common()
          end
          
      2. 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

        1. 1

          https://lobste.rs/u/studzien and I have been using our home-grown parser/constructor library to good effect, for similar use cases.