jonericcook

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

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.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement