justify v1.0.4 Justify
Justify is a data validation library for Elixir.
The primary philosophy behind Justify is that it should be easy to validate data without schemas or types. All of Justify's validation functions will happily accept a plain ol' map.
iex> %{email: "madebyanthony"}
...> |> Justify.validate_required(:email)
...> |> Justify.validate_format(:email, ~r/S+@S+/)
%Justify.Dataset{errors: [email: {"has invalid format", validation: :format}], valid?: false}
Pretty simple. Not much more to it than that.
Supported Validations
Link to this section Summary
Functions
Adds an error to the dataset.
Validates the given field has a value of true
.
Validates the value of a given field matches it's confirmation field.
Applies a validator function to a field containing an embedded value.
Validates the value for the given field is not contained within the provided enumerable.
Validates the value of the given field matches the provided format.
Validates the value for the given field is contained within the provided enumerable.
Validates the length of a string or list.
Validates that one or more fields has a value.
Link to this section Functions
add_error(dataset, field, message, keys \\ [])
add_error(Justify.Dataset.t(), atom(), String.t(), Keyword.t()) ::
Justify.Dataset.t()
add_error(Justify.Dataset.t(), atom(), String.t(), Keyword.t()) :: Justify.Dataset.t()
Adds an error to the dataset.
An optional keyword list can be used to provide additional contextual information about the error.
validate_acceptance(dataset, field, opts \\ [])
validate_acceptance(map(), atom(), Keyword.t()) :: Justify.Dataset.t()
validate_acceptance(map(), atom(), Keyword.t()) :: Justify.Dataset.t()
Validates the given field has a value of true
.
Options
:message
- error message, defaults to "must be accepted"
validate_confirmation(dataset, field, opts \\ [])
validate_confirmation(map(), atom(), Keyword.t()) :: Justify.Dataset.t()
validate_confirmation(map(), atom(), Keyword.t()) :: Justify.Dataset.t()
Validates the value of a given field matches it's confirmation field.
By default, the field will be checked against a field with the same name
but appended with _confirmation
. It’s possible to provide a custom field by
providing a value to the :confirmation_field
option.
Note that if the confirmation field is nil
or missing, by default, an error
will not be added. You can specify that the confirmation field is required in
the options (see below).
Options
:confirmation_field
- name of the field to validate against:message
- error message, defaults to "does not match":required?
- whether the confirmation field must contain a value
validate_embed(dataset, field, validator)
validate_embed(map(), atom(), (... -> any())) :: Justify.Dataset.t()
validate_embed(map(), atom(), (... -> any())) :: Justify.Dataset.t()
Applies a validator function to a field containing an embedded value.
An embedded value can be either a map or a list of maps.
Example
validator = fn(metadata) -> Justify.validate_required(metadata, :key) end
data = %{metadata: [%{value: "a value"}]}
validate_embed(data, :metadata, validator)
#> %Justify.Dataset{errors: [metadata: [[key: {"can't be blank", validation: :required}]]], valid?: false}
validate_exclusion(dataset, field, enum, opts \\ [])
validate_exclusion(map(), atom(), Enum.t(), Keyword.t()) :: Justify.Dataset.t()
validate_exclusion(map(), atom(), Enum.t(), Keyword.t()) :: Justify.Dataset.t()
Validates the value for the given field is not contained within the provided enumerable.
Options
:message
- error message, defaults to "is reserved"
validate_format(dataset, field, format, opts \\ [])
validate_format(map(), atom(), Regex.t(), Keyword.t()) :: Justify.Dataset.t()
validate_format(map(), atom(), Regex.t(), Keyword.t()) :: Justify.Dataset.t()
Validates the value of the given field matches the provided format.
Options
:message
- error message, defaults to "has invalid format"
validate_inclusion(dataset, field, enum, opts \\ [])
validate_inclusion(map(), atom(), Enum.t(), Keyword.t()) :: Justify.Dataset.t()
validate_inclusion(map(), atom(), Enum.t(), Keyword.t()) :: Justify.Dataset.t()
Validates the value for the given field is contained within the provided enumerable.
Options
:message
- error message, defaults to "is invalid"
validate_length(dataset, field, opts)
validate_length(map(), atom(), Keyword.t()) :: Justify.Dataset.t()
validate_length(map(), atom(), Keyword.t()) :: Justify.Dataset.t()
Validates the length of a string or list.
Options
:count
- how to calculate the length of a string. Must be one of:codepoints
,:graphemes
or:bytes
. Defaults to:graphemes
.:is
- the exact length match:min
- match a length greater than or equal to:max
- match a length less than or equal to:message
- error message, defaults to one of the following variants:for strings
- “should be %{count} character(s)”
- “should be at least %{count} character(s)”
- “should be at most %{count} character(s)”
for binary
- “should be %{count} byte(s)”
- “should be at least %{count} byte(s)”
- “should be at most %{count} byte(s)”
for lists
- “should have %{count} item(s)”
- “should have at least %{count} item(s)”
- “should have at most %{count} item(s)”
validate_required(dataset, fields, opts \\ [])
validate_required(map(), atom() | [atom()], Keyword.t()) :: Justify.Dataset.t()
validate_required(map(), atom() | [atom()], Keyword.t()) :: Justify.Dataset.t()
Validates that one or more fields has a value.
Options
:message
- error message, defaults to "must be accepted":trim?
- remove whitespace before validating, defaults totrue