27 lines
688 B
Elixir
27 lines
688 B
Elixir
defmodule Mv.Membership.Validations.ValidateProperty do
|
|
use Ash.Resource.Validation
|
|
|
|
@impl true
|
|
def init(opts) do
|
|
if is_atom(opts[:value]) do
|
|
{:ok, opts}
|
|
else
|
|
{:error, "attribute must be an atom!"}
|
|
end
|
|
end
|
|
|
|
@impl true
|
|
def validate(changeset, _opts, _context) do
|
|
changeset = Ash.Changeset.load(changeset, [:property_type])
|
|
property_type = changeset.data.property_type
|
|
IO.inspect(property_type)
|
|
required? = property_type.required
|
|
union_value = Ash.Changeset.get_attribute(changeset, :value)
|
|
|
|
if required? and union_value in [nil, ""] do
|
|
{:error, field: :value, message: "is required"}
|
|
else
|
|
:ok
|
|
end
|
|
end
|
|
end
|