-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
validate_relationship_attributes.ex
69 lines (61 loc) · 2.65 KB
/
validate_relationship_attributes.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
defmodule Ash.Resource.Verifiers.ValidateRelationshipAttributes do
@moduledoc """
Validates that all relationships point to valid fields
"""
use Spark.Dsl.Verifier
alias Spark.Dsl.Verifier
@impl true
def verify(dsl) do
attribute_names =
dsl
|> Verifier.get_entities([:attributes])
|> Enum.map(& &1.name)
dsl
|> Verifier.get_entities([:relationships])
|> Enum.reject(fn relationship ->
Map.get(relationship, :manual) || Map.get(relationship, :no_attributes?)
end)
|> Enum.filter(& &1.validate_destination_attribute?)
|> Enum.each(&validate_relationship(&1, attribute_names))
end
defp validate_relationship(relationship, attribute_names) do
unless relationship.source_attribute in attribute_names do
raise Spark.Error.DslError,
path: [:relationships, relationship.name],
message:
"Relationship `#{relationship.name}` expects source attribute `#{relationship.source_attribute}` to be defined"
end
if Code.ensure_loaded?(relationship.destination) do
if relationship.type == :many_to_many do
if Code.ensure_loaded?(relationship.through) do
through_attributes =
relationship.through
|> Ash.Resource.Info.attributes()
|> Enum.map(& &1.name)
unless relationship.source_attribute_on_join_resource in through_attributes do
raise Spark.Error.DslError,
path: [:relationships, relationship.name],
message:
"Relationship `#{relationship.name}` expects source attribute on resource `#{relationship.source_attribute_on_join_resource}` to be defined on #{inspect(relationship.through)}"
end
unless relationship.destination_attribute_on_join_resource in through_attributes do
raise Spark.Error.DslError,
path: [:relationships, relationship.name],
message:
"Relationship `#{relationship.name}` expects destination attribute on join resource `#{relationship.destination_attribute_on_join_resource}` to be defined on #{inspect(relationship.through)}"
end
end
end
destination_attributes =
relationship.destination
|> Ash.Resource.Info.attributes()
|> Enum.map(& &1.name)
unless relationship.destination_attribute in destination_attributes do
raise Spark.Error.DslError,
path: [:relationships, relationship.name],
message:
"Relationship `#{relationship.name}` expects destination field `#{relationship.destination_attribute}` to be defined on #{inspect(relationship.destination)}"
end
end
end
end