#
View Source Ash.Resource.Validation.Builtins (ash v2.4.22)

Built in validations that are available to all resources

The functions in this module are imported by default in the validations section.

# Link to this section Summary

## Functions

absent(attributes, opts \\ [])

Validates the absence of a list of attributes or arguments.

attribute_does_not_equal(attribute, value)

Validates that an attribute is not being changed to a specific value, or does not equal the given value if it is not being changed.

attribute_equals(attribute, value)

Validates that an attribute is being changed to a specific value, or equals the given value if it is not being changed.

changing(field)

Validates that an attribute or relationship is being changed

compare(attribute, opts \\ [])

Validates that an attribute or argument meets the given comparison criteria.

confirm(field, confirmation)

Validates that a field or argument matches another field or argument

match(attribute, match)

Validates that an attribute's value matches a given regex.

numericality(attribute, opts \\ [])

Validates that an attribute or argument meets the given comparison criteria.

one_of(attribute, values)

Validates that an attribute's value is in a given list

present(attributes, opts \\ [])

Validates the presence of a list of attributes or arguments.

string_length(attribute, opts \\ [])

Validates that an attribute on the original record meets the given length criteria

# Link to this section Functions

Link to this function

# absent(attributes, opts \\ [])

View Source

    @spec absent(attributes_or_arguments :: atom() | [atom()], opts :: Keyword.t()) ::
      Ash.Resource.Validation.ref()

Validates the absence of a list of attributes or arguments.

If no options are provided, validates that they are all absent.

This works by changing your options and providing them to the `present` validation.

##

options

Options

- `:at_least` (`non_neg_integer/0`) - At least this many must be absent. Defaults to the number of attributes provided

- `:at_most` (`non_neg_integer/0`) - At most this many must be absent. Defaults to the number of attributes provided

- `:exactly` (`non_neg_integer/0`) - Exactly this many must be absent

Link to this function

# attribute_does_not_equal(attribute, value)

View Source

    @spec attribute_does_not_equal(attribute :: atom(), value :: term()) ::
      Ash.Resource.Validation.ref()

Validates that an attribute is not being changed to a specific value, or does not equal the given value if it is not being changed.

##

examples

Examples

    validate attribute_does_not_equal(:admin, true)

    # Or to only check for changing to a given value
    validate attribute_does_not_equal(:admin, true), where: [changing(:admin)]

Link to this function

# attribute_equals(attribute, value)

View Source

    @spec attribute_equals(attribute :: atom(), value :: term()) ::
      Ash.Resource.Validation.ref()

Validates that an attribute is being changed to a specific value, or equals the given value if it is not being changed.

##

examples

Examples

    validate attribute_equals(:admin, true)

    # Or to only check for changing to a given value
    validate attribute_equals(:admin, true), where: [changing(:admin)]

Link to this function

# changing(field)

View Source

    @spec changing(attribute :: atom()) :: Ash.Resource.Validation.ref()

Validates that an attribute or relationship is being changed

##

examples

Examples

    validate changing(:first_name)
    validate changing(:comments)

Link to this function

# compare(attribute, opts \\ [])

View Source

    @spec compare(attribute :: atom(), opts :: Keyword.t()) ::
      Ash.Resource.Validation.ref()

Validates that an attribute or argument meets the given comparison criteria.

The values provided for each option may be a literal value, attribute, argument, or a zero argument function.

##

options

Options

- `:greater_than` - The value that the attribute should be greater than.

- `:greater_than_or_equal_to` - The value that the attribute should be greater than or equal to

- `:less_than` - The value that the attribute should be less than

- `:less_than_or_equal_to` - The value that the attribute should be less than or equal to

##

examples

Examples

    validate compare(:age, greater_than_or_equal_to: 18),
      where: [attribute_equals(:show_adult_content, true)],
      message: "Must be over %{greater_than_or_equal_to} to enable adult content."

    validate compare(:points, greater_than: 0, less_than_or_equal_to: 100)

Link to this function

# confirm(field, confirmation)

View Source

    @spec confirm(
      attribute_or_argument :: atom(),
      confirmation_attribute_or_argument :: atom()
    ) ::
      Ash.Resource.Validation.ref()

Validates that a field or argument matches another field or argument

##

examples

Examples

    validate confirm(:password, :password_confirmation)
    validate confirm(:email, :email_confirmation)

Link to this function

# match(attribute, match)

View Source

    @spec match(attribute :: atom(), match :: Regex.t()) :: Ash.Resource.Validation.ref()

Validates that an attribute's value matches a given regex.

`String.match?/2` is used to determine if the value matches.

##

examples

Examples

validate match(:slug, ~r/^[0-9a-z-_]+$/)

Link to this function

# numericality(attribute, opts \\ [])

View Source

    @spec numericality(attribute :: atom(), opts :: Keyword.t()) ::
      Ash.Resource.Validation.ref()

Validates that an attribute or argument meets the given comparison criteria.

The values provided for each option may be a literal value, attribute, argument, or a zero argument function.

##

options

Options

- `:greater_than` - The value that the attribute should be greater than.

- `:greater_than_or_equal_to` - The value that the attribute should be greater than or equal to

- `:less_than` - The value that the attribute should be less than

- `:less_than_or_equal_to` - The value that the attribute should be less than or equal to

##

examples

Examples

    validate numericality(:age, greater_than_or_equal_to: 18),
      where: [attribute_equals(:show_adult_content, true)],
      message: "Must be over %{greater_than_or_equal_to} to enable adult content."

    validate numericality(:points, greater_than: 0, less_than_or_equal_to: 100)

Link to this function

# one_of(attribute, values)

View Source

    @spec one_of(attribute :: atom(), [any()]) :: Ash.Resource.Validation.ref()

Validates that an attribute's value is in a given list

##

examples

Examples

    validate one_of(:status, [:closed_won, :closed_lost])

Link to this function

# present(attributes, opts \\ [])

View Source

    @spec present(attributes_or_arguments :: atom() | [atom()], opts :: Keyword.t()) ::
      Ash.Resource.Validation.ref()

Validates the presence of a list of attributes or arguments.

If no options are provided, validates that they are all present.

##

options

Options

- `:at_least` (`non_neg_integer/0`) - At least this many must be present. Defaults to the number of attributes provided

- `:at_most` (`non_neg_integer/0`) - At most this many must be present. Defaults to the number of attributes provided

- `:exactly` (`non_neg_integer/0`) - Exactly this many must be present

Link to this function

# string_length(attribute, opts \\ [])

View Source

    @spec string_length(attribute :: atom(), opts :: Keyword.t()) ::
      Ash.Resource.Validation.ref()

Validates that an attribute on the original record meets the given length criteria

##

options

Options

- `:min` (`non_neg_integer/0`) - String must be this length at least

- `:max` (`non_neg_integer/0`) - String must be this length at most

- `:exact` (`non_neg_integer/0`) - String must be this length exactly

##

examples

Examples

    validate string_length(:slug, exactly: 8)
    validate string_length(:password, min: 6)
    validate string_length(:secret, min: 4, max: 12)