improvement: Allow a single where condition for validations (#407)

This commit is contained in:
zimt28 2022-10-07 17:24:04 +02:00 committed by GitHub
parent c59e548244
commit fc4c60b125
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 9 deletions

View file

@ -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

View file

@ -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