Validating Data in Elixir: Using Ecto and NimbleOptions
blog.appsignal.com
blog.appsignal.com
Though! I would say that you might just as well be making a mistake by not at least considering the question, when you reach for "raw" Ecto.Changeset validation, of whether whatever subsystem this is that has all these database-like needs that you're plumbing your way into, is just a Greenspunning of a database — something you could potentially rip out and replace with a database, and in so doing, end up with an easier-to-understand, easier-to-maintain system. (It's "easier-to-understand" not in the sense that your system is now less complex; but rather in that the complexity is now constrained by a formal abstraction — one that allows reasoning about the behavior of that component on a higher level. It's the same reason you'd turn a generic server "with state", into a finite-state machine; or turn a generic recursive-descent parser into a PEG grammar.)
The Erlang runtime is nice, in that it has in-memory database-like features (e.g. ETS), that let you get pretty far in writing internal state-management logic, without using any kind of database. But when you find yourself writing code that relies on 1. taking complex user-submitted data, 2. validating it and casting it into domain types, 3. inserting it record-wise into an ETS table, 4. and then manually indexing that table across multiple fields... maybe it's time for a database!
(It doesn't have to be a big database. Or even one that involves any external dependencies. There's an Mnesia adapter for Ecto, which is a combination I've always found fascinating — especially in the context of a single-node, memory-only Mnesia setup. It's an ORM over transactionally-joined ETS tables! But of course, if you have control over your deploy-time environment and so can suffer a few native deps, then SQLite works great as your "internal database" too.)
So far, everything I’ve done within the Elixir ecosystem has “just worked” (with the exception of some stuff with supervisors, the config and intuition for which are currently eluding me).
For example, I managed to code a full image processing pipeline (classification, labelling, and description) using Elixir, Nx, Bumbkebee, etc on the train on the way into work the other day. Which is about a 35 min trip.
If we’re talking about tooling, the last time I tried to just print “Hello, world!” in either Python or JavaScript, it took me down a hell dungeon of configuration and dependency nightmares. To be fair to both, tho, that was some time ago so I can only hope that situation has cleared up more recently.
The ElixirLS server always crashed for me on init. The problem is assuredly on my end, but I was never able to fix it.
https://elixirforum.com/t/elixirls-not-working-runtime-termi...
The instant feedback actually feels like how it always should've been.
I have recently gotten it working tho with vanilla emacs with eglot, as well as doom emacs (again, with eglot).
No pressure!
I have seen a couple of issues that have been worked-out.
In particular, it's nice to have completion in ".exs" files!
But man, if I could have a smart editor for those templates I would be like... three times as productive on my little side projects.
On Mac:
brew install node
node -e "console.log('hello, world');"
Seems pretty easy?But for any serious work I will more likely use asdf or nvm/rbenv/rvm.
My contact links should be discoverable on the website: https://www.elixir-tools.dev
Try using Zed it comes with Elixir support out of the box, and uses nextls, which works always for me. Snappy as hell! Like Sublime Text 4.0 https://github.com/elixir-tools/next-ls
[1] https://marketplace.visualstudio.com/items?itemName=elixir-t...
Don't get me wrong, I think getting vim/neovim setup to do the same types of things is possibly worse, but I haven't really had much of a better experience in VSCode either.
I’d be interested to know what other kinds of features there could or should be, as I’d like to be as productive as possible and I don’t feel like I really am.
Eclipse got this right, but the JetBrains IDEs (such as IntelliJ and CLion) are a bit heavy-handed.
I've been trying Goal[0] to validate API data at the controller edge, which uses changesets itself.
Faced with this kind of issue, I'd use Ash with the `Simple` data layer. You can get all the power of an embedded Ecto schema and nimble options together in a single dsl.
If you need to validate with arbitrary logic you can do that with a 'validation'
If you need to transform the input data before you return the struct you can do it in a change set.
It's all there!
The problem is the languages one would hear about, fall into the OOP category, functional languages tend to not be as fashionable when you do get into one, you become almost a zealot.
It's hard to fight the imprinting a language has with its first fans, Elixir because of its Erlang origins tends to have the identity of telephonic-based use cases. Similar to how Rust gets its reputation as a specialist language (cli,kernal, etc) even though you could use it for web apps as well.
The last reason is, it's really hard to fight the gravity pool of NextJS and React.
Personally my circle and company and country are solidly on the side of C#/Java/PHP/Python and React/Angular, the average developer who fits into that grouping might not have even heard of Svelt or SolidJs when they do hear about it, they immediately make some snide comment about JS fatigue, even though that hasn't been a thing for a while.
Indeed. I love Elixir and I even struggle with choice when I don't need much or any server-side functionality.
There was a Jekyll-style project for Elixir some years ago but it fell into bitrot and deprecation. I'm still hoping somebody resurrects it or builds an equivalent, because using eex to build a static site is a really great experience.
For what it’s worth, Elixir is now my preferred language for personal projects, although I still haven’t had a chance to use it professionally.
I love working with Elixir and have done so successfully for the past 4 years at various companies, however I have been trying to find a new client for a while now and I'm getting close to the point of looking for other types of work because things are looking bleak. From what I can tell there are also way more people looking for Elixir work than available jobs, unfortunately.
In my 15+ years of software development in various languages Elixir has been the most enjoyable developer experience for me. So I guess I'll just have to think of a side project where I can apply it and do less fun stuff to earn bread.
However I think with Phoenix Liveview Elixir gives regular developers a productivity boost that could be it's own USP.
I have only written a small elixir app but I was mind blown over Liveview and still is convinced that most people would be better of using it over a SPA.
Concurrency? Trivial.
Reliability? Unbelievable. (YouTube Sasa Juric's "Soul of Elixir" talk)
Clustering/horizontal scaling? Built in, even across networks.
Web framework? Competitive with the best (I'd argue better, with liveview but YMMV).
AI/ML? Amazing support and tooling.
Embedded/IoT? Incredible support with the nerves project.
There's only 2 places where I feel it falls down a bit:
1) brittle dev tooling in ElixirLS that trips up new and experienced devs alike. Soon to get better though as competing LSPs are in the works.
2) no official release-as-binary tooling yet. Things are way better in the last few years for releases, and you're probably containerizing anyways, but I am jealous of e.g. Go's single file binaries.
Students, presented with the capabilities of the language (scripting / machine learning / scalable web apps) are quite interested actually!
Looking at NimbleOptions based on this example I think I would prefer to stick to Ecto because I don't like the idea of putting the schema in a giant keyword list - but maybe soon I'll have a better play around with it myself, read the docs and see if there are better ways to use it.