jonericcook
Validate all possible API param shapes / types
Hello,
I am writing an elixir library that wraps an HTTP API. If you check out this endpoint ReDoc Interactive Demo you’ll see the POST body can have multiple shapes as there are some fields that are optional and some that are required. I am seeking some advice on how to properly validate all possible API params (and their types) for a given endpoint.
In the simplest form I envision my library having a function build_transaction(params) that accepts a params variable and then validates its shape and types.
I did some research and saw there are some validation libraries out there but wanted to see what you guys use.
This endpoint ReDoc Interactive Demo is a good example of how complex a POST body can be. If you expand the metadata field you’ll see that the type has a set number of possibilities and also per each selection there are additional fields required.
Thanks for any help / guidance!
Marked As Solved
gmile
Based on your description, I would strongly suggest looking into JSON Schema, which is currently being standardised.
Effectively, with JSON Schema you define a JSON object with rules (think of Elixir map here). Then you take a JSON with request body, and attempt to match it with a JSON object containing rules. This would be the validation of request.
This document should give you an idea of what’s possible with JSON schemas. In a nutshell, you’re able to specify:
- what fields the request may contain,
- what is optional/required,
- what expected types of values are,
- if it’s an array, what is the min/max number of acceptable items it may have,
- enums,
- etc.
The link I posted in my previous message refers to two existing implementations of JSON Schema in Elixir.







