@@ -210,6 +210,46 @@ defmodule Test.Acceptance.PostTest do
210
210
end
211
211
end
212
212
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
+
213
253
defmodule Domain do
214
254
use Ash.Domain ,
215
255
extensions: [
@@ -224,6 +264,7 @@ defmodule Test.Acceptance.PostTest do
224
264
resources do
225
265
resource ( Author )
226
266
resource ( Post )
267
+ resource ( Pin )
227
268
end
228
269
end
229
270
@@ -372,6 +413,34 @@ defmodule Test.Acceptance.PostTest do
372
413
assert error [ "code" ] == "invalid_attribute"
373
414
assert error [ "source" ] == % { "pointer" => "/data/attributes/review/rating" }
374
415
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
375
444
end
376
445
377
446
test "post with upsert" do
0 commit comments