ndaljr
I’m still new to phoenix, prolly somewhere around 2 weeks old. I’m trying to build a json api and I’m having difficulty finding the best practice for validation. For example if my json api expects a body like
{
name: "hello",
email: "test@gmail.com"
}
And my client only sends
{
name: "test"
}
My appliation immediately throws a 500. What’s the current best practice for resolving this?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
NobbZ
Can you please provide an example action where this happens?
I assume, you are haveing a line like
{:ok, data} = JSON.decode(body_data)where it isn’t able to match due to the incomplete data. Thats where you need to use acaseexpression where you branch depending on the result of the call toJSON.decode/1(which in reality can have any other name and arity, depends on the tools you use, which you didn’t even told us which that were).dom
Ecto changesets are pretty much the standard. If you’re not saving the JSON to a DB, you can use “schemaless” changesets:
Your example for instance would work with validate_required.
dimitarvp
You can try a Elixir JSON Schema validator. I used such in Ruby, Go and Java, but not in Elixir so far and I cannot guarantee its quality.
Or, if you only need to guarantee a top-level set of keys, you can just get
Map.keysfrom the info that is sent to your API and compare it to your pre-determined set of mandatory map keys.ndaljr
I’ve actually been able to resolve my issues. It was an error on my end. But thanks for the insights!
kasvith
can you explain how we can use changeset to validate a payload conditionally?
like in Node, with JOI you can do something like
Also I’m concerned of validating nested JSONs. And sometimes values in Payloads arent directly used by DB layer
In node, I had this layer that validates the payload before reaching to the controller.
In elixir I’m kinda lost as it seems everything is now messed up
soup
As above, you can use Ecto + schemaless changesets to validate anything, so I would reach for that.
There are a few ways, you can pipe and pattern match or whatever. Just pass the changeset around to whatever function.
Its also common(?) (maybe becoming common?) to have schemaless models backing your forms/api vs your actual CRUD operations (which also still have their own changesets).
This can give you some separation between “API validation” and “Data validation” if that makes sense. You then don’t need your form to match your schema exactly. You could, as a very trite example, accept “length” + “unit” in your form, validate that as you like, then always convert it to “length in mm” before sending to your actual db schema type which can just validate that the number between 0 and 100000 or whatever. This becomes more applicable as your model complexity increases, for basic stuff you can often stick with 1:1 or a simple “pass through”.
E: in terms of nested JSON, you can make schemaless types for each “nested section” to validate independently too. I do this for a pretty complex migration aide where you can have many different types of data cross associated and it works well and is pretty “brainable”.