Funx.Validator.Not (funx v0.8.0)

View Source

Validates 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:

  • :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, Not succeeds and returns the original value.
  • If the inner validator returns Right, Not fails with a ValidationError.
  • Nothing values are preserved and never cause failure.
  • Just values 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"]
  }
}

Summary

Functions

validate(value, opts)