Skip to content

Commit 972698f

Browse files
authored
fix: Avoid raising the condition converting the regex to string. (#204)
1 parent b17985a commit 972698f

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

lib/ash_json_api/serializer.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ defmodule AshJsonApi.Serializer do
120120
|> add_if_defined(:detail, error.detail)
121121
|> add_if_defined([:source, :pointer], error.source_pointer)
122122
|> add_if_defined([:source, :parameter], error.source_parameter)
123-
|> add_if_defined(:meta, error.meta)
123+
|> add_if_defined(:meta, parse_error(error.meta))
124124
|> add_about_link(error.about, request)
125125
end
126126

@@ -781,4 +781,10 @@ defmodule AshJsonApi.Serializer do
781781
# end)
782782
# |> URI.to_string()
783783
end
784+
785+
defp parse_error(%{match: %Regex{} = match} = error) do
786+
%{error | match: Regex.source(match)}
787+
end
788+
789+
defp parse_error(error), do: error
784790
end

test/acceptance/post_test.exs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,46 @@ defmodule Test.Acceptance.PostTest do
210210
end
211211
end
212212

213+
defmodule Pin do
214+
use Ash.Resource,
215+
domain: Test.Acceptance.PostTest.Domain,
216+
data_layer: Ash.DataLayer.Ets,
217+
extensions: [
218+
AshJsonApi.Resource
219+
]
220+
221+
ets do
222+
private?(true)
223+
end
224+
225+
json_api do
226+
type("pin")
227+
228+
routes do
229+
base("/pins")
230+
231+
post(:create)
232+
end
233+
end
234+
235+
actions do
236+
create :create do
237+
primary? true
238+
accept([:pin])
239+
end
240+
end
241+
242+
attributes do
243+
uuid_primary_key(:id)
244+
attribute(:pin, :string)
245+
end
246+
247+
validations do
248+
validate match(:pin, ~r/^[0-9]{4}$/)
249+
validate string_length(:pin, exact: 4)
250+
end
251+
end
252+
213253
defmodule Domain do
214254
use Ash.Domain,
215255
extensions: [
@@ -224,6 +264,7 @@ defmodule Test.Acceptance.PostTest do
224264
resources do
225265
resource(Author)
226266
resource(Post)
267+
resource(Pin)
227268
end
228269
end
229270

@@ -372,6 +413,34 @@ defmodule Test.Acceptance.PostTest do
372413
assert error["code"] == "invalid_attribute"
373414
assert error["source"] == %{"pointer" => "/data/attributes/review/rating"}
374415
end
416+
417+
test "error validation using match with a regex" do
418+
response =
419+
Domain
420+
|> post(
421+
"/pins",
422+
%{
423+
data: %{
424+
type: "pin",
425+
attributes: %{pin: "12a"}
426+
}
427+
},
428+
status: 400
429+
)
430+
431+
# response is a Plug.
432+
assert %{"errors" => [error_regex, error_length]} = response.resp_body
433+
434+
assert error_regex["code"] == "invalid_attribute"
435+
assert error_regex["detail"] == "must match ~r/^[0-9]{4}$/"
436+
assert error_regex["meta"] == %{"match" => "^[0-9]{4}$"}
437+
assert error_regex["source"] == %{"pointer" => "/data/attributes/pin"}
438+
439+
assert error_length["code"] == "invalid_attribute"
440+
assert error_length["detail"] == "must have length of exactly %{exact}"
441+
assert error_length["meta"] == %{"exact" => 4}
442+
assert error_length["source"] == %{"pointer" => "/data/attributes/pin"}
443+
end
375444
end
376445

377446
test "post with upsert" do

0 commit comments

Comments
 (0)