vex
vex copied to clipboard
`presence` validation fails for boolean `false` values
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?
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]}])