This commit is contained in:
parent
967a89b18d
commit
156cdb24d0
5 changed files with 62 additions and 4 deletions
27
lib/membership/validate_property.ex
Normal file
27
lib/membership/validate_property.ex
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue