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

presence validation fails for boolean false values #49

Open
sadesyllas opened this issue Dec 15, 2017 · 1 comment
Open

presence validation fails for boolean false values #49

sadesyllas opened this issue Dec 15, 2017 · 1 comment

Comments

@sadesyllas
Copy link

Evaluating:

Vex.validate(%{"a" => false}, [{"a", [presence: true]}])

returns:

{:error, [{:error, "a", :presence, "must be present"}]}

If this is normal how can I validate that the key a exists in the map regardless of its value?

@astery
Copy link
Contributor

astery commented Dec 18, 2017

I think it's intentional, because it's being tested to fail on false in doc test. But I can't see reason behind this. You simply can define your own validator for this case:

defmodule MyValidators.StrictPresence do
  use Vex.Validator

  @message_fields [value: "The bad value"]
  def validate(false, _options), do: :ok
  def validate(value, options), do: Vex.Validators.Presence.validate(value, options)
end

config :vex,  sources: [[strict_presence: MyValidators.StrictPresence], Vex.Validators]

Vex.validate(%{"a" => false}, [{"a", [strict_presence: true]}])

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