ash-project / ash_json_api

The JSON:API extension for the Ash Framework
https://hexdocs.pm/ash_json_api
MIT License
63 stars 43 forks source link

protocol Jason.Encoder not implemented for Regex #203

Closed paridin closed 5 months ago

paridin commented 5 months ago

Describe the bug using regex validate match(:pin, ~r/^[0-9]{4}$/) can't be directly sent to meta -> match because it's not possible to encode in JSON, in my case protocol Jason.Encoder not implemented for ~r/^[0-9]{4}$/ of type Regex (a struct), Jason.Encoder protocol must always be explicitly implemented.

To Reproduce A minimal set of resource definitions and calls that can reproduce the bug.

defmodule Foo do
  use Ash.Resource,
    domain: Domain.Foo,
    extensions: [AshJsonApi.Resource]

  # to skip the error 
  # require Protocol
  # Protocol.derive(Jason.Encoder, Regex, only: [])

  json_api do
    type "foo"
  end

  create :new_request do
    primary? true
    accept [:pin]
  end

  attributes do
    attribute :pin, :string
  end

 validations do
   validate match(:pin, ~r/^[0-9]{4}$/)
 end
end

request HTTP with the wrong pin.

Expected behavior A successful response with an error is defined.

** Runtime

Additional context

Errors while debugging

[%AshJsonApi.Error{
    id: "4deebdd4-972e-4ec3-ab40-7c0cb0b82054",
    about: :undefined,
    code: "invalid_attribute",
    title: "InvalidAttribute",
    detail: "must match ~r/^[0-9]{4}$/",
    source_pointer: "/data/attributes/pin",
    source_parameter: :undefined,
    meta: %{match: ~r/^[0-9]{4}$/},
    status_code: 400,
    internal_description: nil,
    log_level: :error
  }]