vex icon indicating copy to clipboard operation
vex copied to clipboard

`presence` validation fails for boolean `false` values

Open sadesyllas opened this issue 7 years ago • 1 comments

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?

sadesyllas avatar Dec 15 '17 16:12 sadesyllas

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]}])

astery avatar Dec 18 '17 07:12 astery