mirror of
https://github.com/ash-project/ash.git
synced 2024-09-20 05:23:03 +12:00
improvement: add verifier for identity fields. (#626)
* improvement: adds verifier for identity fields. --------- Co-authored-by: abhishek <abhishek.tripathi@tinymesh.in>
This commit is contained in:
parent
4b08e9865f
commit
06329b97cf
3 changed files with 57 additions and 1 deletions
|
@ -1332,7 +1332,8 @@ defmodule Ash.Resource.Dsl do
|
||||||
|
|
||||||
@verifiers [
|
@verifiers [
|
||||||
Ash.Resource.Verifiers.ValidateRelationshipAttributesMatch,
|
Ash.Resource.Verifiers.ValidateRelationshipAttributesMatch,
|
||||||
Ash.Resource.Verifiers.VerifyReservedCalculationArguments
|
Ash.Resource.Verifiers.VerifyReservedCalculationArguments,
|
||||||
|
Ash.Resource.Verifiers.VerifyIdentityFields
|
||||||
]
|
]
|
||||||
|
|
||||||
@moduledoc """
|
@moduledoc """
|
||||||
|
|
23
lib/ash/resource/verifiers/verfiy_identities.ex
Normal file
23
lib/ash/resource/verifiers/verfiy_identities.ex
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
defmodule Ash.Resource.Verifiers.VerifyIdentityFields do
|
||||||
|
@moduledoc """
|
||||||
|
Raises an error on potentially incompatible identity attributes.
|
||||||
|
"""
|
||||||
|
use Spark.Dsl.Verifier
|
||||||
|
|
||||||
|
def verify(dsl) do
|
||||||
|
identities = Ash.Resource.Info.identities(dsl)
|
||||||
|
|
||||||
|
for identity <- identities do
|
||||||
|
for key <- identity.keys do
|
||||||
|
if !Ash.Resource.Info.attribute(dsl, key) do
|
||||||
|
raise Spark.Error.DslError,
|
||||||
|
module: Spark.Dsl.Verifier.get_persisted(dsl, :module),
|
||||||
|
message: "All identity keys must be attributes. Got: #{inspect(key)}",
|
||||||
|
path: [:identities, identity.name]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
end
|
|
@ -69,5 +69,37 @@ defmodule Ash.Test.Resource.IdentitiesTest do
|
||||||
%Ash.Resource.Identity{description: "require one of name/contents"}
|
%Ash.Resource.Identity{description: "require one of name/contents"}
|
||||||
] = Ash.Resource.Info.identities(Post)
|
] = Ash.Resource.Info.identities(Post)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "enforce identity constraints on attributes" do
|
||||||
|
assert_raise Spark.Error.DslError,
|
||||||
|
~r/All identity keys must be attributes. Got: :naem/,
|
||||||
|
fn ->
|
||||||
|
defmodule Site do
|
||||||
|
@moduledoc false
|
||||||
|
use Ash.Resource
|
||||||
|
|
||||||
|
attributes do
|
||||||
|
uuid_primary_key :id
|
||||||
|
attribute :url, :string
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defposts do
|
||||||
|
actions do
|
||||||
|
read :read do
|
||||||
|
primary? true
|
||||||
|
end
|
||||||
|
|
||||||
|
relationships do
|
||||||
|
belongs_to :site, Site
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
identities do
|
||||||
|
identity :name_site, [:naem, :site]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue