Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable or document shared formats #10

Closed
nathanl opened this issue Oct 23, 2024 · 4 comments
Closed

Enable or document shared formats #10

nathanl opened this issue Oct 23, 2024 · 4 comments

Comments

@nathanl
Copy link

nathanl commented Oct 23, 2024

The docs show using formats like this:

attribute :uuid, :string,  format: ~r/\A\d{5}\-\d{5}\-\d{5}\-\d{5}\-\d{5}\z/

When multiple schemas use a uuid of that format, it would be less error-prone to be able to do something like:

attribute :uuid, :string,  format: Formats.uuid()

I don't see anything like this in the docs. Is it supported? If not, could it be?

So far I've attempted to use a separate module and ensure that it's compiled before the schema using require, @before_compile, and elixirc_paths, but I always get an error saying that the module is unavailable. Maybe this is because the Speck compiler runs before any of the Mix.compilers?

@amclain
Copy link
Member

amclain commented Oct 26, 2024

I don't see anything like this in the docs. Is it supported? If not, could it be?

We plan to support this is with custom attribute types, which is a feature in the backlog. I've documented it in this repo as issue #11.

A workaround to do what you wrote is to use Code.require_file in your spec:

Code.require_file "../lib/format.ex"

struct Example.Message

attribute :uuid, :string, format: Format.uuid
defmodule Format do
  def uuid do
    ~r/\A\d{5}\-\d{5}\-\d{5}\-\d{5}\-\d{4}\z/
  end
end

Please continue this thread if you run into any other issues with this approach.

@amclain
Copy link
Member

amclain commented Oct 26, 2024

Functionality will be covered by #11

@amclain amclain closed this as completed Oct 26, 2024
@nathanl
Copy link
Author

nathanl commented Nov 1, 2024

Code.require_file "../lib/format.ex" does not work because the Speck compiler ignores it as unknown syntax here.

@amclain
Copy link
Member

amclain commented Nov 2, 2024

Another workaround is to make the Format module a dependent library. It could be as simple as a path dependency in the same repo. That will cause the Format module to be compiled first, then Speck protocols, then the application code. This route also doesn't require adding any code to the header of the specs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants