IvanR

IvanR

Dear community,

There are lovely TypedStruct by @ejpcmac and TypedEctoSchema by @bamorim libraries to define structs in combination with t() types.

Using them, I missed the validation of default values conformance to t(). That validation seems good because initial default values defining the app’s state can drift from the type definition later as the code evolves. That, in turn, can confuse the local thinking about what’s correctly defined the value or the type?

To address this, I’ve added the compile-time defaults validation feature to the Domo library. That works for any struct that has a t() type defined. And can be activated by adding use Domo like the following:

defmodule Order do
  use TypedStruct
  use Domo

  typedstruct enforce: true do
    field :id, non_neg_integer()
    field :items, [String.t()], default: []
  end
end

That gives the compile-time error highlighting the issue immediately :smile:

Compiling 1 file (.ex)
Domo is compiling type ensurer for 1 module (.ex)

== Compilation error in file lib/functional_core_struct.ex:55 ==
** A default value given via defstruct/1 in Order module mismatches the type.
"Invalid value nil for field :id of %Order{}. Expected the value matching the non_neg_integer() type."

And adding default: 1 option to field :id or changing its type to non_neg_integer() | nil leads to successful compilation.

How do you see it can be worthful to your projects?

Showing Posts 1 to 5

dimitarvp

dimitarvp

This is extremely useful. I’d use the hell out of it!

ejpcmac

ejpcmac

Hello @IvanR, thank you for mentioning me here, you’ve just made me aware of Domo :slight_smile: This seems very neat!

I am not writing much code in Elixir these times, doing mainly embedded stuff in Rust at home (and C / Python at work), but be sure I’ll use it on my next projects using Elixir (certainly some Nerves firmware communicating with other embedded systems written in Rust) :slight_smile:

Validating the default values was on my roadmap, but since you’ve solved it in an elegant way and I
want TypedStruct to be thin, can I mention it in the README / docs as a solution for this?

wolf4earth

wolf4earth

FYI, if you’re interested to learn more about Domo, we’ve recently had @IvanR as a guest in ElixirMix where we talked about Domo:

IvanR

IvanR OP

Hello @ejpcmac,

Thanks for your reply. As the proverb says, the appropriate tool is to be used for the appropriate problem. Sometimes, other languages fit better, and it’d be super good if you use Elixir/Nerves on the other project! :slightly_smiling_face:

I’m glad that you liked the library! :slightly_smiling_face: I was inspired a lot by TypedStruct working on it. And I’ll be delighted if you mention the Domo solution for the default checks in the README/docs.

IvanR

IvanR OP

Thanks a lot for having me at ElixirMix back then. It was a very welcoming and good atmosphere during the recording, which I liked very much! I’ll be glad to come again :slightly_smiling_face:

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
budgie
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

Other Trending Topics Top

GenericJam
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
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
marciok
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New
Dmk
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews