Funx.Validator.Not (funx v0.8.0)
View SourceValidates that a given validator does not succeed.
Not provides logical negation for validation. It inverts the success and failure
of a single validator while preserving inapplicability semantics for optional
(Prism) foci.
This validator is useful for expressing constraints such as: "value must not satisfy rule A".
Options
:validator(required) A single validator to negate. This may be:- a validator module implementing
Funx.Validate.Behaviour - a
{Validator, opts}tuple for optioned validators
- a validator module implementing
:message(optional) Note: Not uses a zero-arity callback(() -> String.t())used to override the default error message when the negated validator succeeds.
Semantics
- The inner validator is evaluated first.
- If the inner validator returns
Left,Notsucceeds and returns the original value. - If the inner validator returns
Right,Notfails with aValidationError. Nothingvalues are preserved and never cause failure.Justvalues are validated by the inner validator, but the original input is returned unchanged on success.
Examples
iex> Funx.Validator.Not.validate(0,
...> validator: Funx.Validator.Positive
...> )
%Funx.Monad.Either.Right{right: 0}
iex> Funx.Validator.Not.validate(10,
...> validator: Funx.Validator.Positive
...> )
%Funx.Monad.Either.Left{
left: %Funx.Errors.ValidationError{
errors: ["must not satisfy condition"]
}
}
iex> Funx.Validator.Not.validate(%Funx.Monad.Maybe.Nothing{},
...> validator: Funx.Validator.Positive
...> )
%Funx.Monad.Either.Right{right: %Funx.Monad.Maybe.Nothing{}}
iex> Funx.Validator.Not.validate(10,
...> validator: Funx.Validator.Positive,
...> message: fn -> "must not be positive" end
...> )
%Funx.Monad.Either.Left{
left: %Funx.Errors.ValidationError{
errors: ["must not be positive"]
}
}