-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
identity.ex
45 lines (40 loc) · 1.11 KB
/
identity.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
defmodule Ash.Resource.Identity do
@moduledoc "Represents a unique constraint on a resource"
defstruct [:name, :keys, :description, :message]
@schema [
name: [
type: :atom,
required: true,
doc:
"The name of the identity. Used by extensions to target specific identities for fetching single instances of a resource"
],
keys: [
type: {:custom, __MODULE__, :keys, []},
required: true,
doc:
"The names of attributes, aggregates or calculations that uniquely identify this resource."
],
description: [
type: :string,
doc: "An optional description for the identity"
],
message: [
type: :string,
doc: "An error message to use when the unique identity would be violated"
]
]
def schema, do: @schema
@type t :: %__MODULE__{
name: atom(),
keys: list(atom()),
description: String.t() | nil
}
def keys(keys) do
keys = List.wrap(keys)
if Enum.all?(keys, &is_atom/1) do
{:ok, keys}
else
{:error, "Expected a list of atoms for the identity keys"}
end
end
end