IvanRublev / Domo

A library to validate values of nested structs with their type spec t() and associated precondition functions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

unowned structs cannot be compiled

ouven opened this issue · comments

Environment

  • Elixir version (elixir -v): 1.12.3
  • Domo version (mix deps | grep domo | head -1): 1.3.2
  • TypedStruct version (mix deps | grep typed_struct | head -1): 2.0.1

Actual behavior

When I try to have a struct with - let's say - Decimal in it. I get the the message:

** Domo.TypeEnsurerFactory.Resolver failed to resolve fields type of the ****** struct due to "Consider to use Domo in Decimal struct for validation speed.
If you don't own the struct you can define custom user type and validate fields in the precondition function attached like the following:

    @type unowned_struct :: term()
    precond unowned_struct: &validate_unowned_struct/1

    def validate_unowned_struct(value) do
      case value do
        %Decimal{} -> ...validate fields here...
        _ -> {:error, \"expected Decimal struct value.\"}
      end
    end
".

This is clear - but when I try to do it like that I get the error, that I cannot put a precond at a term:

== Type ensurer compilation error in file lib/purchase.ex ==
** Domo.TypeEnsurerFactory.Resolver failed to resolve fields type of the ***** struct due to "Precondition for value of term() type is not allowed.".

Expected behavior

Get a hint, who I can get the same behaviour like in 1.3.1

Hey,

Thanks for the bug report! That's unexpected behaviour.

It's fixed with version 1.3.3.

The release supports validation of values of Decimal.t(), that is you can reference the type directly like that:

defmodule SomeStruct do
  use Domo

  defstruct [:value]

  @type t :: %__MODULE__{value: Decimal.t() | nil}
end

Thanks you a lot