IvanR

IvanR

Domo makes your structure types work for data conformance validation. And it enables automatic range checking for field types at run-time.

Domo adds a new/1 constructor function to the structure module that builds an instance only if all fields conform to the struct’s t() type and all specified precondition functions for filed types return true.

That is useful for boundary data validation coming as decoded JSON from Jason or from :erlang.binary_to_term/1, and for validation when converting one core struct into another like in CQRS framework Commanded.

Domo makes it possible to validate model types in microservice setups between depending applications by sharing common modules with type definitions among codebases.

See typical usage in Readme.md via:

Run in Livebook

Shortly it looks like:

defmodule Customer do
  use Domo

  defstruct title: :none, name: "", age: 0

  @type title :: :mr | :ms | :dr | :none
  @type name :: String.t()

  @type age :: non_neg_integer()
  precond age: &(&1 < 300)

  @type t :: %__MODULE__{title: title(), name: name(), age: age()}
  precond t: &(String.length(&1.name) < 10)
end

iex(1)> Customer.new(title: :dr, name: "John", age: 25)           
{:ok, %Customer{age: 25, name: "John", title: :dr}}

iex(2)> Customer.new(title: :dr, name: nil, age: 25)              
{:error, [name: "Invalid value nil for field :name of %Customer{}. Expected the value \
matching the <<_::_*8>> type."]}

iex(3)> Customer.new(title: :dr, name: "Johnny be good", age: 25) 
{:error, [t: "Invalid value %Customer{age: 25, name: \"Johnny be good\", title: :dr}. \
Expected the value matching the Customer.t() type. And a true value from the precondition \
function \"&(String.length(&1.name) < 10)\" defined for Customer.t() type."]}

Example applications:

Domo main features are:

  • automatic generation of constructor and insurance functions validating struct’s fields conformance to @type t(), which are: new!/1, new/1, ensure_type!/1, and ensure_type/1
  • validation of nested structs referenced in the type spec
  • support for boolean precondition functions attached to the user-defined type or the whole struct’s t() type for values range check
  • validation of struct default values at compile-time
  • recompilation of dependencies when type definition changes

More information here:

Showing Posts 1 to 10

IvanR

IvanR OP

v1.2.4 – June 6, 2021

  • Speedup resolving the struct types
  • Limit the number of allowed fields types combinations to 4096
  • Support Range.t() and MapSet.t()
  • Keep type ensurers source code after compiling an umbrella project
  • Remove preconditions manifest file on mix clean command
  • List processed structs giving mix --verbose option
IvanR

IvanR OP

v1.2.5 – June 14, 2021

  • Add remote_types_as_any option to disable validation of specified remote types for the case of hitting the limit of 4096 type combinations. A precondition for wrapping user-defined type can be used in such cases.
IvanR

IvanR OP

v1.2.6 - June 20, 2021

  • Validates type conformance of default values given with defstruct/1 at compile-time :star_struck:
  • Includes only the most matching type error into the error message
IvanR

IvanR OP

v1.2.7 - July 4, 2021

  • Fix the bug to resolve remote type when giving correct alias
  • Support custom {:error, message} returned from the range validation function defined with precond/1 macro
IvanR

IvanR OP

v1.2.9 - August 8, 2021

Domo is lightened by extraction of the tagged_tuple library (finally! :tada:)

Documentation is rewritten from scratch.

The /example_avialia project is updated to demonstrate using of Domo to validate Ecto changesets.

Other changes:

  • Fix bug to acknowledge that type has been changed after a failed compilation.

  • Fix bug to match structs not using Domo with a field of any() type with and without precondition.

  • Add typed_fields/1 and required_fields/1 functions.

  • Add maybe_filter_precond_errors: true option that filters errors from precondition functions for better output for the user.

IvanR

IvanR OP

v1.3.0 - August 15, 2021

  • Change the default name of the constructor function to new! to follow Elixir naming convention.
    You can always change the name with the config :domo, :name_of_new_function, :new_func_name_here app configuration.

  • Fix bug to validate defaults for every required field in a struct except __underscored__ fields at compile-time.

  • Check whether the precondition function associated with t() type returns true at compile-time regarding defaults correctness check.

  • Add examples of integration with TypedStruct and TypedEctoSchema.

IvanR

IvanR OP

v1.3.2 - September 18, 2021

  • Support remote types in erlang modules like :inet.port_number()

  • Shorten the invalid value output in the error message

  • Increase validation speed by skipping fields that are not in t() type spec or have the any() type

  • Fix bug to skip validation of struct’s enforced keys default value because they are ignored during the construction anyway

  • Increase validation speed by generating TypeEnsurer modules for Date, Date.Range, DateTime, File.Stat, File.Stream, GenEvent.Stream, IO.Stream, Macro.Env, NaiveDateTime, Range, Regex, Task, Time, URI, and Version structs from the standard library at the first project compilation

  • Fix bug to call the precond function of the user type pointing to a struct

  • Increase validation speed by encouraging to use Domo or to make a precond function for struct referenced by a user type

  • Add Domo.has_type_ensurer?/1 that checks whether a TypeEnsurer module was generated for the given struct.

  • Add example of parsing and validating of the JSON via Jason + ExJSONPath + Domo

IvanR

IvanR OP

v1.3.3 - October 7, 2021

  • Support validation of Decimal.t()

  • Fix bug to define precondition function for user type referencing any() or term()

  • Add link to Commanded + Domo example app in Readme

IvanR

IvanR OP

1.3.4 - October 13, 2021

  • Make error messages to be more informative

  • Improve compatibility with Ecto 3.7.x

  • Explicitly define :ecto and :decimal as optional dependencies

  • Fix bug to pass :remote_types_as_any option with use Domo

  • Explicitly define that MapSet should be validated with precond function for custom user type, because parametrized t(value) types are not supported

  • Replace apply() with Module.function calls to run faster

IvanR

IvanR OP

1.4.0 - November 15, 2021

  • Fix bug to detect runtime mode correctly when launched under test.

  • Add support for @opaque types.

Breaking changes:

  • Change new_ok constructor function name to new that is more convenient.
    Search and replace new_ok( → new( in all files of the project
    using Domo to migrate.

  • Constructor function name generation procedure changes to adding !
    to the value of :name_of_new_function option. The defaults are new and new!.

Where Next? Top

Trending in Announcing Top

mudasobwa
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
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
danschultzer
I’ve moved the strategies out of PowAssent and created a multi-provider framework for Elixir. This way the strategies can be used in any ...
New
gtcode
Aloha gang, There’s a new class of AI model out from TypeSafe. Jev is their flagship model. You can learn about it here: ...
New
danschultzer
I needed something like premailer for rails, and after searching, I decided to make an easy-to-use library to handle inline styles and co...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
KristerV
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
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
AstonJ
With advancements in AI gathering pace and set to increasingly accelerate, perhaps it’s worth having an annual thread on the topic - feel...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews