kanishka
Are there patterns similar to having a private constructor along with a public build/validate function to guide users of a struct module to build valid instances? I understand that you may not be able to completely stop a user from creating a struct with invalid field values, but just wonder if there are patterns for something along those lines. I understand that you can use Ecto validations with embedded schemas to express some constraints, but wondering if there some pattern for plain structs
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
- #ai
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
sodapopcan
There is not. A common pattern is to define a
new/1for this and you just have to document that that is what should be used.Edit: sorry “there is not” referring to not being able to mail a struct as private.
kartheek
Elixir does have concept of constructors - they are from OOPS world. There is map and there is struct which is map with additional field
__struct__. There are functions which operate on data. There are Modules which group functions together. I might be oversimplifying things..I use
@enforce_keysandinit(opts)to initialise structs (not everywhere though).Eiji
There is no solution which covers all use cases, but a pure
Elixirvalidation is pretty simple to write, for example with code below:the struct would not be validated (except
@enforce_keyswhen using it “by hand” like%Example{}. Officially there is no support for preventing others to use write struct by hand.dimitarvp
Or you can use the
domolibrary.kanishka
I will probably use this or new, and return a Result for this quick and dirty case and keep domo in mind.
kanishka
My current understanding is that Ecto is popular for validating DB persisted structs, and there is some momentum around libraries that validate data received from external services. It looks like domo has some users, but it’s not used everywhere, and the norm for structs that are not persisted are various patterns mentioned above? I am curious if you think domo will keep growing and start to become a library that people use in most projects, or if there is some resistance to introducing a library for logic that most people feel they can implement on their own quickly?
Eiji
I would say that any library to achieve something like that would need to:
be a part of some generator provided by
Elixircore team or related (ecto,phoenixetc.)have guides about it everywhere (just like now it’s for
phoenixfor example)have a big community (which comes from points 1 and 2, but it may be required by some companies) - especially a big number of users who test every release
Look that
ectodoes not have it’s own generators, but support for it is important inphoenixgenerators. It’s widely adopted, well tested and especially every bigger/complete/seriousElixir/Phoenixtutorial adds lots of information about it.On the other side if we have
xnumber of libraries doing similar thing where:there is no chance that one library would be used in most projects especially if it would not be needed in simplest cases. Developers for each project would use at most one library which:
Look that old
Phoenixtemplates would be used almost only in unmaintained projects. Almost all projects already moved or would move toheexpretty soon not because it’s best for everything and there would never be a better solution, but because aPhoenixcore team (of course they had a reason for it) would drop support for it in newest releases.Personally I’m subscribing Phoenix.HTML goes slim · Issue #372 · phoenixframework/phoenix_html · GitHub issue. You can answer yourself … Do you know and use in most projects
templelibrary? I have used it before introducingheexengine.Of course I’m not saying that only
Elixir/Ecto/Phoenixcore teams are doing a great libraries, but the chances to adopt their libraries in most projects are much higher because of support (as above - supported in official generator), respect for their knowledge/skills and a huge communities around them.dimitarvp
It is. I only used
domoonce and liked it but a lot of people are not happy with compilation and macro hackery and I can understand and sympathize.In your case, a plain old struct +
@enforce_keys+ anewfunction + a non-adversarial usage will do the trick just fine.kartheek
I have been using these for a long time:
You mention about something about stopping a user from creating a struct with invalid fields. User is another developer working on the project or user who will be using the application?
There is NimbleOptions NimbleOptions — NimbleOptions v1.1.1.
Elixir community is small, it does not have the volume/size like other languages:
This does not translate to a problem with language, framework or libraries. Adoption numbers, popularity and momentum numbers are on a different scale in elixir world.
json_xema- i have been using from around 2019/2020(i don’t remember exact date). It just works, it has 57 stars and 4 forks. On hex.pm it has around 200 downloads a day.sabiwara
In addition to other answers, if you are using typespecs and dialyzer it might be interesting to add a definition of an
opaquetype for your struct to achieve some kind of encapsulation.Dialyzer will report:
While this doesn’t offer strong guarantees, it can help detect cases that are by-passing your constructor in your codebase.