/
ci_string.ex
223 lines (184 loc) · 5.28 KB
/
ci_string.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
defmodule Ash.Type.CiString do
@constraints [
max_length: [
type: :non_neg_integer,
doc: "Enforces a maximum length on the value"
],
min_length: [
type: :non_neg_integer,
doc: "Enforces a minimum length on the value"
],
match: [
type: {:custom, __MODULE__, :match, []},
doc: "Enforces that the string matches a passed in regex"
],
trim?: [
type: :boolean,
doc: "Trims the value.",
default: true
],
allow_empty?: [
type: :boolean,
doc: "Sets the value to `nil` if it's empty.",
default: false
],
casing: [
type: {:one_of, [:upper, :lower, nil]},
default: nil,
doc: """
Lowercases or uppercases the value, fully discarding case information.
For example, if you don't set this, a value of `FrEd` could be saved to the data layer.
`FrEd` and `fReD` would still compare as equal, but the original casing information is retained.
In many cases, this is what you want. In some cases, however, you want to remove all case information.
For example, in an email, you may want to support a user inputting an upper case letter, but discard it
when saved.
"""
]
]
@moduledoc """
Stores a case insensitive string in the database
See `Ash.CiString` for more information.
A builtin type that can be referenced via `:ci_string`
### Constraints
#{Spark.OptionsHelpers.docs(@constraints)}
"""
use Ash.Type
@impl true
def storage_type(_), do: :ci_string
@impl true
def constraints, do: @constraints
@impl true
def generator(constraints) do
StreamData.string(
:printable,
Keyword.take(constraints, [:max_length, :min_length])
)
|> StreamData.map(fn value ->
value =
case constraints[:casing] do
:lower ->
String.downcase(value)
:upper ->
String.upcase(value)
nil ->
value
end
Ash.CiString.new(value, constraints[:casing])
end)
end
def apply_constraints(%Ash.CiString{} = value, constraints) do
value
|> Ash.CiString.value()
|> apply_constraints(constraints)
|> case do
{:ok, nil} ->
{:ok, nil}
{:ok, value} ->
{:ok, Ash.CiString.new(value, constraints[:casing])}
other ->
other
end
end
def apply_constraints(nil, _), do: {:ok, nil}
def apply_constraints(value, constraints) do
{value, errors} =
return_value(
Keyword.get(constraints, :allow_empty?, false),
Keyword.get(constraints, :trim?, true),
value,
constraints
)
case errors do
[] -> {:ok, value}
[error] -> {:error, error}
errors -> {:error, errors}
end
end
defp return_value(false, true, value, constraints) do
trimmed = String.trim(value)
if trimmed == "" do
{nil, []}
else
{trimmed, validate(trimmed, constraints)}
end
end
defp return_value(false, false, value, constraints) do
if String.trim(value) == "" do
{nil, []}
else
{value, validate(value, constraints)}
end
end
defp return_value(true, true, value, constraints) do
trimmed = String.trim(value)
{trimmed, validate(trimmed, constraints)}
end
defp return_value(true, false, value, constraints),
do: {value, validate(value, constraints)}
defp validate(value, constraints) do
Enum.reduce(constraints, [], fn
{:max_length, max_length}, errors ->
if String.length(value) > max_length do
[[message: "length must be less than or equal to %{max}", max: max_length] | errors]
else
errors
end
{:min_length, min_length}, errors ->
if String.length(value) < min_length do
[
[message: "length must be greater than or equal to %{min}", min: min_length]
| errors
]
else
errors
end
{:match, regex}, errors ->
if String.match?(value, regex) do
errors
else
[[message: "must match the pattern %{regex}", regex: inspect(regex)] | errors]
end
_, errors ->
errors
end)
end
@impl true
def cast_input(%Ash.CiString{} = value, _), do: {:ok, value}
def cast_input(value, _) do
case Ecto.Type.cast(:string, value) do
{:ok, value} -> {:ok, Ash.CiString.new(value)}
:error -> :error
end
end
@impl true
def cast_stored(nil, _), do: {:ok, nil}
def cast_stored(%Ash.CiString{} = value, _), do: {:ok, value}
def cast_stored(value, _) do
case Ecto.Type.load(:string, value) do
{:ok, value} -> {:ok, Ash.CiString.new(value)}
:error -> :error
end
end
@impl true
def dump_to_native(nil, _), do: {:ok, nil}
def dump_to_native(%Ash.CiString{} = ci_string, _) do
case Ecto.Type.dump(:string, Ash.CiString.value(ci_string)) do
{:ok, value} -> {:ok, value}
:error -> :error
end
end
def dump_to_native(value, _) do
case Ecto.Type.dump(:string, value) do
{:ok, value} -> {:ok, value}
:error -> :error
end
end
@impl true
def equal?(left, right) do
Ash.CiString.compare(left, right) == :eq
end
def match(%Regex{} = regex), do: {:ok, regex}
def match(_) do
{:error, "Must provide a regex to match, e.g ~r/foobar/"}
end
end