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:
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:
- Web-site with Ecto + Domo
- Integration with
TypedStructandTypedEctoSchemalibraries - JSON parsing and validation
- Commanded + Domo combo used in Event Sourcing and CQRS
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, andensure_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:
Trending in Announcing
Other Trending Topics
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
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #elixirconf-us
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #processes
- #security
- #api
- #forms
- #performance










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
IvanR
v1.2.4 – June 6, 2021
Range.t()andMapSet.t()mix cleancommand--verboseoptionIvanR
v1.2.5 – June 14, 2021
remote_types_as_anyoption 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
v1.2.6 - June 20, 2021
defstruct/1at compile-timeIvanR
v1.2.7 - July 4, 2021
alias{:error, message}returned from the range validation function defined withprecond/1macroIvanR
v1.2.9 - August 8, 2021
Domo is lightened by extraction of the tagged_tuple library (finally!
)
Documentation is rewritten from scratch.
The
/example_avialiaproject 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/1andrequired_fields/1functions.Add
maybe_filter_precond_errors: trueoption that filters errors from precondition functions for better output for the user.IvanR
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_hereapp 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 returnstrueat compile-time regarding defaults correctness check.Add examples of integration with
TypedStructandTypedEctoSchema.IvanR
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 theany()typeFix bug to skip validation of struct’s enforced keys default value because they are ignored during the construction anyway
Increase validation speed by generating
TypeEnsurermodules forDate,Date.Range,DateTime,File.Stat,File.Stream,GenEvent.Stream,IO.Stream,Macro.Env,NaiveDateTime,Range,Regex,Task,Time,URI, andVersionstructs from the standard library at the first project compilationFix bug to call the
precondfunction of the user type pointing to a structIncrease validation speed by encouraging to use Domo or to make a
precondfunction for struct referenced by a user typeAdd
Domo.has_type_ensurer?/1that checks whether aTypeEnsurermodule was generated for the given struct.Add example of parsing and validating of the JSON via
Jason+ExJSONPath+DomoIvanR
v1.3.3 - October 7, 2021
Support validation of
Decimal.t()Fix bug to define precondition function for user type referencing
any()orterm()Add link to Commanded + Domo example app in Readme
IvanR
1.3.4 - October 13, 2021
Make error messages to be more informative
Improve compatibility with
Ecto3.7.xExplicitly define
:ectoand:decimalas optional dependenciesFix bug to pass
:remote_types_as_anyoption withuse DomoExplicitly define that
MapSetshould be validated withprecondfunction for custom user type, because parametrizedt(value)types are not supportedReplace
apply()with Module.function calls to run fasterIvanR
1.4.0 - November 15, 2021
Fix bug to detect runtime mode correctly when launched under test.
Add support for
@opaquetypes.Breaking changes:
Change
new_okconstructor function name tonewthat is more convenient.Search and replace
new_ok(→new(in all files of the projectusing
Domoto migrate.Constructor function name generation procedure changes to adding
!to the value of
:name_of_new_functionoption. The defaults arenewandnew!.