-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
identity.ex
64 lines (57 loc) · 1.82 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
defmodule Ash.Resource.Identity do
@moduledoc """
Represents a unique constraint on a resource
Data layers should (and all built in ones do), discount `nil` or `null` (in the case of postgres) values
when determining if a unique constraint matches. This often means that you should
prefer to use identities with non-nullable columns.
Eventually, features could be added to support including `nil` or `null` values, but they would
need to include a corresponding feature for data layers.
"""
defstruct [:name, :keys, :description, :message, :eager_check_with, :pre_check_with]
@schema [
name: [
type: :atom,
required: true,
doc: "The name of the identity."
],
keys: [
type: {:custom, __MODULE__, :keys, []},
required: true,
doc: "The names of the attributes that uniquely identify this resource."
],
eager_check_with: [
type: {:behaviour, Ash.Api},
doc: """
Validates that the unique identity provided is unique at validation time, outside of any transactions, using the api module provided.
"""
],
pre_check_with: [
type: {:behaviour, Ash.Api},
doc: """
Validates that the unique identity provided is unique in a before_action hook.
"""
],
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