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

Support custom regex for validation via config #84

Merged
merged 3 commits into from
Mar 20, 2024

Conversation

lucca65
Copy link
Contributor

@lucca65 lucca65 commented Mar 9, 2024

Hello there!

I'm a user of this library, its been amazing so far. I love how declarative it is with macros!

However, in my use case, I needed to run custom regex's, which are different from the default ones provided.

I've changed it so those custom regex could live together with the customized email, uuid, in the Configuration. Maybe we could have another mechanism to send custom regex directly, but this use case is perfect for me since my regex's never change nor come from the client.

Configure new regex

In the configuration file

config :goal,
  # From Goal's docs
  uuid_regex: ~r/^[[:alpha:]]+$/,
  email_regex: ~r/^[[:alpha:]]+$/,

  # Custom regex used by my library
  ledger_index_regex: ~r/^(\d+|current|closed|validated)$/,
  ledger_entry_regex: ~r/^[a-fA-F0-9]{64}$/,
  currency_regex: ~r/^(?!(XRP$))[A-Za-z0-9\?\!\@\#\$\%\^\&\*\(\)\{\}\[\]\|]{3}$|^(?![0]{2})[A-Fa-f0-9]{40}$/,
  public_key_regex: ~r/^n[1-9a-np-zA-NP-Z]{1,53}$/,
  account_address_regex: ~r/^r[1-9a-np-zA-NP-Z]{24,34}$/

Use it inside defparams:

  defparams "account_nfts" do
    required(:account, :string, format: :account_address)
    optional(:ledger_hash, :string, format: :ledger_entry)
    optional(:ledger_index, :string, format: :ledger_index)
end

I think this is pretty and idiomatic, while keeping the same structure the library already have.

Let me know your thoughts!

assert errors_on(changeset_2) == %{hash: ["has invalid format"]}

Application.delete_env(:goal, :custom_regex)
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've choose to do a hash regex validation here, not sure if this is the best approach, lmk

@martinthenth
Copy link
Owner

Hey! Thanks for your PR. I'm okay with this solution as a workaround to #64. When that issue is resolved, then this workaround is liable to be removed in a major version update.

@lucca65
Copy link
Contributor Author

lucca65 commented Mar 15, 2024

Ok! Sounds good!

I've fixed the failed CI error, should be good to go now!

Copy link
Owner

@martinthenth martinthenth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! 👏🏻

@martinthenth martinthenth merged commit b43fbb8 into martinthenth:main Mar 20, 2024
1 check passed
@lucca65 lucca65 deleted the regex branch March 21, 2024 01:43
@martinthenth
Copy link
Owner

Published the new version on Hex: https://hex.pm/packages/goal/0.3.2

@martinthenth
Copy link
Owner

Hi @lucca65! I'm preparing a version that includes support for passing module attributes to the schema. I know I said that this feature that was introduced may be removed when that support is added, but I see no harm in keeping it and I think it is a useful addition. So the feature will stay in version 1.0.0 🙇🏻

@lucca65
Copy link
Contributor Author

lucca65 commented May 13, 2024

This is nice! Thanks for the headsup! I'll update my deps as soon as I get the release notifications and will let you know if I stumble upon any issues!

Happy to know the library is moving forward to 1.0.0!! 🎉

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

Successfully merging this pull request may close these issues.

2 participants