mirror of
https://github.com/ash-project/ash.git
synced 2024-09-20 05:23:03 +12:00
improvement: Allow a single where
condition for validations (#407)
This commit is contained in:
parent
c59e548244
commit
fc4c60b125
2 changed files with 19 additions and 9 deletions
|
@ -43,16 +43,17 @@ defmodule Ash.Resource.Validation do
|
|||
@callback init(Keyword.t()) :: {:ok, Keyword.t()} | {:error, String.t()}
|
||||
@callback validate(Ash.Changeset.t(), Keyword.t()) :: :ok | {:error, term}
|
||||
|
||||
@validation_type {:spark_behaviour, Ash.Resource.Validation, Ash.Resource.Validation.Builtins}
|
||||
|
||||
@schema [
|
||||
validation: [
|
||||
type: {:spark_behaviour, Ash.Resource.Validation, Ash.Resource.Validation.Builtins},
|
||||
type: @validation_type,
|
||||
required: true,
|
||||
doc: "The module/opts pair of the validation",
|
||||
links: []
|
||||
],
|
||||
where: [
|
||||
type:
|
||||
{:list, {:spark_behaviour, Ash.Resource.Validation, Ash.Resource.Validation.Builtins}},
|
||||
type: {:or, [@validation_type, {:list, @validation_type}]},
|
||||
required: false,
|
||||
default: [],
|
||||
links: [
|
||||
|
@ -113,10 +114,20 @@ defmodule Ash.Resource.Validation do
|
|||
end
|
||||
|
||||
@doc false
|
||||
def transform(%{validation: {module, opts}} = validation) do
|
||||
def transform(%{validation: {module, opts}, where: where} = validation) do
|
||||
case module.init(opts) do
|
||||
{:ok, opts} -> {:ok, %{validation | validation: {module, opts}, module: module, opts: opts}}
|
||||
{:error, error} -> {:error, error}
|
||||
{:ok, opts} ->
|
||||
{:ok,
|
||||
%{
|
||||
validation
|
||||
| validation: {module, opts},
|
||||
module: module,
|
||||
opts: opts,
|
||||
where: List.wrap(where)
|
||||
}}
|
||||
|
||||
{:error, error} ->
|
||||
{:error, error}
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -26,9 +26,8 @@ defmodule Ash.Test.Actions.ValidationTest do
|
|||
where([attribute_equals(:foo, true)])
|
||||
end
|
||||
|
||||
validate attribute_does_not_equal(:status, "foo") do
|
||||
where([attribute_equals(:foo, false)])
|
||||
end
|
||||
validate attribute_does_not_equal(:status, "foo"),
|
||||
where: attribute_equals(:foo, false)
|
||||
end
|
||||
|
||||
attributes do
|
||||
|
|
Loading…
Reference in a new issue