Validatex v1.0.1 Validatex.Validation View Source

This module helps with validation of input forms.

Link to this section Summary

Types

Event describes four different action for fields

Defines field data type. It contains a (raw) value from input form and an information about validation of this value.

Defines optional_field data type. For cases when you need optional an input form.

This type defines three state of field (or simply consider the field as an input form)

Functions

Applying function on concrete or all valid fields according to your needs.

Gets error from field.

Defines field with :not_validated validity. It's used as init value of your forms, e.g. for name, password,... See example of using.

If you need to have the field with invalid() validity. Then you have to add an error message.

Guard for verifying if key of map is atom or binary.

Validation of optional variable.

Has similar functionality as for field. But in this case is for an optional input form.

If you need to have the field with valid(a) validity.

Gets raw value from field.

If all fields have a valid values then you can use this function to send these data to server. See example of using.

Verification if field has valid value.

Runs validation for field with given validator and event action.

Runs validation on map which contains fields with given validator for on_blur event action. See example of using.

Runs validation on map which contains fields with given validator for on_change event action. See example of using.

Runs validation on map which contains fields with given validator for on_related_change event action. See example of using.

Runs validation for field which is tied with another field with on_submit event action. For example password and confirm_password.

Runs validation on map which contains fields with given validator for on_submit event action. See example of using.

Guard for verifying if validation function has an arity equal to 1.

Gets validity from field.

Link to this section Types

Link to this type

error_or_errors()

View Source
error_or_errors() :: error() | errors()
Link to this type

errors()

View Source
errors() :: [error()]

Event describes four different action for fields:

  • on_blur() validates field when user leaves an input form
  • on_change(raw) validates field when user changes value in input field
  • on_related_change() validates field which is tied with another field, for example: password and his confirm form
  • on_submit() validates all model data (it means all fields) before submitting to server
Link to this type

field(raw, a)

View Source
field(raw, a) :: {:field, raw, validity(a)}

Defines field data type. It contains a (raw) value from input form and an information about validation of this value.

Link to this type

invalid()

View Source
invalid() :: {:invalid, error_or_errors()}
Link to this type

model()

View Source
model() :: %{required(key()) => field(any(), any())}
Link to this type

not_validated()

View Source
not_validated() :: :not_validated
Link to this type

on_blur()

View Source
on_blur() :: :on_blur
Link to this type

on_change(val)

View Source
on_change(val) :: {:on_change, val}
Link to this type

on_submit()

View Source
on_submit() :: :on_submit
Link to this type

optional_field(raw, a)

View Source
optional_field(raw, a) :: field(raw, ExMaybe.t(a))

Defines optional_field data type. For cases when you need optional an input form.

Link to this type

valid(a)

View Source
valid(a) :: {:valid, a}
Link to this type

validator(a, b)

View Source
validator(a, b) :: (a -> Result.t(error_or_errors(), b))
Link to this type

validity(a)

View Source
validity(a) :: not_validated() | valid(a) | invalid()

This type defines three state of field (or simply consider the field as an input form):

  • not_validated(): it's default value and mean it that input form has not validated yet,
  • valid(a): input form has been validated and has a valid value,
  • invalid(): in opposite case, it has invalid value and thus contain one or more error messages.

Link to this section Functions

Link to this function

apply(data, fields, f)

View Source
apply(
  %{required(key()) => field(any(), a)},
  [key()],
  (%{required(key()) => a} -> b)
) :: validity(b)
when a: var, b: var

Applying function on concrete or all valid fields according to your needs.

iex> nv = Validatex.Validation.field("bar")
{:field, "bar", :not_validated}
iex> pv = Validatex.Validation.pre_validated_field("foo", & &1)
{:field, "foo", {:valid, "foo"}}
iex> data = %{"name" => nv, "surname" => pv}
%{
  "name" => {:field, "bar", :not_validated},
  "surname" => {:field, "foo", {:valid, "foo"}}
}
iex> Validatex.Validation.apply(data, ["name", "surname"], & &1)
{:invalid, "'name' field isn't valid.'"}
iex> Validatex.Validation.apply(data, ["surname"],
...> fn %{"surname" => s} -> %{"surname" => String.capitalize(s)} end)
{:valid, %{"surname" => "Foo"}}
Link to this function

extract_error(arg)

View Source
extract_error(field(any(), any())) :: ExMaybe.t(error_or_errors())

Gets error from field.

iex> Validatex.Validation.field("bar") |>
...> Validatex.Validation.invalidate("Expected foo!") |>
...> Validatex.Validation.extract_error()
"Expected foo!"
Link to this function

field(raw)

View Source
field(raw) :: {:field, raw, :not_validated} when raw: var

Defines field with :not_validated validity. It's used as init value of your forms, e.g. for name, password,... See example of using.

iex> Validatex.Validation.field("foo")
{:field, "foo", :not_validated}
Link to this function

invalidate(arg, err)

View Source
invalidate(field(raw, any()), String.t()) :: {:field, raw, {:invalid, error()}}
when raw: var

If you need to have the field with invalid() validity. Then you have to add an error message.

iex> Validatex.Validation.field("bar") |> Validatex.Validation.invalidate("Expected foo!")
{:field, "bar", {:invalid, "Expected foo!"}}

Guard for verifying if key of map is atom or binary.

Validation of optional variable.

iex> f = Validatex.Validation.optional(&Validatex.Validators.not_empty(&1))
iex> f.("")
{:ok, nil}
iex> f.("foo")
{:ok, "foo"}
Link to this function

optional_field(raw)

View Source
optional_field(raw) :: {:field, raw, :not_validated} when raw: var

Has similar functionality as for field. But in this case is for an optional input form.

Link to this function

pre_validated_field(val, f)

View Source
pre_validated_field(val, (val -> String.t())) ::
  {:field, String.t(), valid(val)}
when val: var

If you need to have the field with valid(a) validity.

iex> "5" |> Validatex.Validation.pre_validated_field(& &1)
{:field, "5", {:valid, "5"}}
Link to this function

raw_value(arg)

View Source
raw_value(field(raw, any())) :: raw when raw: var

Gets raw value from field.

iex> {:field, "bar", :not_validated} |> Validatex.Validation.raw_value()
"bar"
Link to this function

submit_if_valid(data, fields, f)

View Source
submit_if_valid(
  %{required(key()) => field(any(), a)},
  [key()],
  (%{required(key()) => a} -> b)
) :: b
when b: Result.t(any(), any()), a: var

If all fields have a valid values then you can use this function to send these data to server. See example of using.

Verification if field has valid value.

iex> "5" |> Validatex.Validation.pre_validated_field(& &1) |> Validatex.Validation.valid?()
true

iex> {:field, "bar", :not_validated} |> Validatex.Validation.valid?()
false
Link to this function

validate(field, validator, arg3)

View Source
validate(field(raw, a), validator(raw, a), event(raw)) :: field(raw, a)

Runs validation for field with given validator and event action.

iex> nv = Validatex.Validation.field("bar")
{:field, "bar", :not_validated}
iex> Validatex.Validation.validate(nv, &Validatex.Validators.not_empty(&1), :on_submit)
{:field, "bar", {:valid, "bar"}}
iex> Validatex.Validation.validate(nv,
...> &Validatex.Validators.not_empty(&1),
...> {:on_change, "foo"})
{:field, "foo", :not_validated}
Link to this function

validate_on_blur(map, field, validator)

View Source
validate_on_blur(model(), key(), validator(any(), any())) :: model()

Runs validation on map which contains fields with given validator for on_blur event action. See example of using.

Link to this function

validate_on_change(map, field, value, validator)

View Source
validate_on_change(model(), key(), any(), validator(any(), any())) ::
  model()

Runs validation on map which contains fields with given validator for on_change event action. See example of using.

Link to this function

validate_on_submit(map, field, validator)

View Source
validate_on_submit(model(), key(), validator(any(), any())) :: model()

Runs validation on map which contains fields with given validator for on_submit event action. See example of using.

Guard for verifying if validation function has an arity equal to 1.

Link to this function

validity(arg)

View Source
validity(field(any(), a)) :: validity(a)

Gets validity from field.

iex> {:field, "bar", :not_validated} |> Validatex.Validation.validity()
:not_validated