mitgliederverwaltung/lib/membership/validate_property.ex
Moritz 156cdb24d0
Some checks failed
continuous-integration/drone/push Build is failing
WIP: validate required fields
2025-06-02 22:41:04 +02:00

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