railsmechanic

railsmechanic

How to handle user inputs without ecto for use with CouchDB?

Hi!

I’m currently planning a proof of concept Phoenix web application, which should utilize CouchDB as its main database. I’ve some experience with CouchDB and I understand how to model the documents for my application.

Now, my main problem is how to handle user inputs. When users submit a form, all parameters are just strings. With ecto it’s quite simple as we can use a specific schema to cast these inputs to the correct data types e.g. integers, floats etc. But how to handle this without ecto? I need to cast user inputs to a struct, with the correct data types, which can be used within the application and to store (encoded to JSON) within CouchDB.

One option is to (ab)use a ecto schema for casting an validating the user inputs. But are there other options I can use? I’m searching for a couple of days but I don’t find anything suitable. Mostly some proof of concepts like “Parse, don’t validate”, which describes (mostly in theory) how to tackle this problem.

Do you have any hints for me?

Many thanks!

Most Liked

gregvaughn

gregvaughn

I’ll echo the other messages. It is not abuse to use ecto without persistence. I gave a talk a couple of years ago “Ecto Without a DB” that has an example of validating user input for search params. Here’s the video queued to that section.

wmnnd

wmnnd

Ecto has been purposefully split into ecto and ecto_sql. So it’s definitely not an “abuse” to use Ecto simply for handling and casting user-provided data when you’re not working with an SQL database.
You can use all the lovely features of Changesets including integration with Phoenix, even if you’re using your own persistence layer.

lud

lud

That would be my first attempt honestly. Once you have a changeset you can write your own logic for persistence. And schemas are useful anyway.

Or you could use a library like Xema. But for example you will still have to cast string-integers to integers, which means that you will have to validate those with custom validators or regexes.

I did not read the entire article you linked but to me validating before inserting makes no sense anyway. Just “validate” right after receiving the data (welll … the input) should be enough.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement