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:
Abhishek Tripathi 2023-06-20 10:35:41 +05:30 committed by GitHub
parent 4b08e9865f
commit 06329b97cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 1 deletions

View file

@ -1332,7 +1332,8 @@ defmodule Ash.Resource.Dsl do
@verifiers [
Ash.Resource.Verifiers.ValidateRelationshipAttributesMatch,
Ash.Resource.Verifiers.VerifyReservedCalculationArguments
Ash.Resource.Verifiers.VerifyReservedCalculationArguments,
Ash.Resource.Verifiers.VerifyIdentityFields
]
@moduledoc """

View 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

View file

@ -69,5 +69,37 @@ defmodule Ash.Test.Resource.IdentitiesTest do
%Ash.Resource.Identity{description: "require one of name/contents"}
] = Ash.Resource.Info.identities(Post)
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