WIP: validate required fields
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Moritz 2025-06-02 22:41:04 +02:00
parent 967a89b18d
commit 156cdb24d0
Signed by: moritz
GPG key ID: 1020A035E5DD0824
5 changed files with 62 additions and 4 deletions

View file

@ -0,0 +1,27 @@
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