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

Feature: Add support for some Validation checks #2

Closed
fireproofsocks opened this issue Jul 2, 2021 · 3 comments
Closed

Feature: Add support for some Validation checks #2

fireproofsocks opened this issue Jul 2, 2021 · 3 comments

Comments

@fireproofsocks
Copy link
Owner

fireproofsocks commented Jul 2, 2021

It may not be enough to cast the incoming ENV value to an Elixir data type using Dotenvy.Transformer.to!/2: sometimes the app will break if the value is wrong. For example: a negative number used for a database port or a random quote from Shakespeare when a subdomain was expected. In both cases, the basic Elixir data type was correct (an integer and a string, respectively), but the app would still break because the values are unusable. Adding validation checks would help strengthen the contract between the app and its environment.

Basic validations:

  • number ranges (e.g. gt (i.e. >), gte, between, etc.)
  • enum or in (only one of set of possible values is allowed)
  • regex: the (string) value must match the given regular expression
  • URL: the (string) value must be a valid URL (ugh... don't try a regex for this)

Rather than trying to fit this into the Dotenvy.env!/2 or Dotenvy.env!/3 functions, it might be clearer to pipe output, e.g.

import Dotenvy

config_value: env!("SOME_ENV", :string) |> match!(~r/^a-z{2, 10}$/),
x: env!("X", :string) |> in!(["a", "b", "c"]),
y: env!("SOME_ENV", :string) |> matches!(~r/^a-z{2, 10}$/),

that way it would be possible to chain together multiple rules if complex criteria exist.

If this does belong as an input to env!/2 or env!/3, that messes with the simplicity of the functions because there's no room for options when arity 3 is already taken.

@lud
Copy link

lud commented Jul 21, 2021

I would like to be able to pass a function as the type.

Basically: def to!(str, fun) when is_function(fun, 1), do: fun.(str)

@fireproofsocks
Copy link
Owner Author

I like that idea!

@fireproofsocks
Copy link
Owner Author

fireproofsocks commented Jul 21, 2021

Resolved #6

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