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
event(raw)
View Sourceevent(raw) :: on_submit() | on_blur() | on_related_change() | on_change(raw)
Event describes four different action for fields:
on_blur()validatesfieldwhen user leaves an input formon_change(raw)validatesfieldwhen user changes value in input fieldon_related_change()validatesfieldwhich is tied with anotherfield, for example: password and his confirm formon_submit()validates all model data (it means all fields) before submitting to server
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.
validator(a, b)
View Sourcevalidator(a, b) :: (a -> Result.t(error_or_errors(), b))
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
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"}}
extract_error(arg)
View Sourceextract_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!"
field(raw)
View Sourcefield(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}
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"}
optional_field(raw)
View Sourceoptional_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.
If you need to have the field with valid(a) validity.
iex> "5" |> Validatex.Validation.pre_validated_field(& &1)
{:field, "5", {:valid, "5"}}
Gets raw value from field.
iex> {:field, "bar", :not_validated} |> Validatex.Validation.raw_value()
"bar"
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
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}
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_submit event action.
See example
of using.
Guard for verifying if validation function has an arity equal to 1.
Gets validity from field.
iex> {:field, "bar", :not_validated} |> Validatex.Validation.validity()
:not_validated