justify v0.1.1 Justify

Link to this section Summary

Functions

Adds an error to the dataset

Validates the given field is true

Validates that the given field matches the confirmation value for that field

Validates a field’s value is not included in the given enumerable

Validates that a field’s value is of the given format

Validates a field’s value is included in the given enumerable

Validates a field’s value is a string or list of the given length

Validates that one or more fields are present in the dataset

Link to this section Functions

Link to this function add_error(dataset, field, message, keys \\ [])
add_error(Justify.Dataset.t(), atom(), String.t(), Keyword.t()) :: Justify.Dataset.t()

Adds an error to the dataset.

An additional keyword list can be passed to provide additional contextual information for the error.

Link to this function validate_acceptance(data, field, opts \\ [])
validate_acceptance(Justify.Dataset.t() | map() | struct(), atom(), Keyword.t()) :: Justify.Dataset.t()

Validates the given field is true.

Options

  • :message - the message on failure, defaults to “must be accepted”
Link to this function validate_confirmation(data, field, opts \\ [])
validate_confirmation(Justify.Dataset.t() | map() | struct(), atom(), Keyword.t()) :: Justify.Dataset.t()

Validates that the given field matches the confirmation value for that field.

By default, the field will be checked against a field with the same name appended with _confirmation (e.g. :email would be checked against :email_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). Note, the confirmation field does not need to be added to structs.

Options

  • :confirmation_field - the field to check against
  • :message - the message on failure, defaults to “does not match”
  • :required - sets whether existence of a confirmation field is required
Link to this function validate_exclusion(data, field, enum, opts \\ [])
validate_exclusion(Justify.Dataset.t() | map() | struct(), atom(), Enum.t(), Keyword.t()) :: Justify.Dataset.t()

Validates a field’s value is not included in the given enumerable.

Options

  • :message - the message on failure, defaults to “is reserved”
Link to this function validate_format(data, field, format, opts \\ [])
validate_format(Justify.Dataset.t() | map() | struct(), atom(), Regex.t(), Keyword.t()) :: Justify.Dataset.t()

Validates that a field’s value is of the given format.

The format must be expressed as a regular expression.

Options

  • :message - the message on failure, defaults to “has invalid format”
Link to this function validate_inclusion(data, field, enum, opts \\ [])
validate_inclusion(Justify.Dataset.t() | map() | struct(), atom(), Enum.t(), Keyword.t()) :: Justify.Dataset.t()

Validates a field’s value is included in the given enumerable.

Options

  • :message - the message on failure, defaults to “is invalid”
Link to this function validate_length(dataset, field, opts)
validate_length(Justify.Dataset.t() | map() | struct(), atom(), Keyword.t()) :: Justify.Dataset.t()

Validates a field’s value is a string or list of the given length.

Options

  • :is - the length must be exactly this value
  • :min - the length must be greater than or equal to this value
  • :max - the length must be less than or equal to this value
  • :count - what length to count for string, :graphemes (default) or :codepoints
  • :message - the message on failure, depending on the value type, is one of

    • for strings

      • “should be %{count} character(s)”
      • “should be at least %{count} character(s)”
      • “should be at most %{count} character(s)”
    • for lists

      • “should have %{count} item(s)”
      • “should have at least %{count} item(s)”
      • “should have at most %{count} item(s)”
Link to this function validate_required(data, fields, opts \\ [])
validate_required(Justify.Dataset.t() | map() | struct(), list() | atom(), Keyword.t()) :: Justify.Dataset.t()

Validates that one or more fields are present in the dataset.

If the value of a field is nil or a string made only of whitespace, the dataset is marked as invalid.

If a field does not exist within the dataset, the dataset is marked as invalid.

Options

  • :message - the message on failure, defaults to “can’t be blank”