View Source Ash.Resource.Validation.Builtins (ash v2.9.11)

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

Validates the absence of a list of attributes or arguments.

Validates that the action is a specific action. Primarily meant for use in where.

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.

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

Validates that an attribute is being changed to one of a set of specific values, or is in the the given list if it is not being changed.

Validates that an attribute or relationship is being changed

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

Validates that a field or argument matches another field or argument

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

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

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

Validates the presence of a list of attributes or arguments.

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

@spec action_is(action :: atom()) :: Ash.Resource.Validation.ref()

Validates that the action is a specific action. Primarily meant for use in where.

examples

Examples

validate present(:foo), where: [action_is(:bar)]
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

attribute_in(attribute, list)

View Source
@spec attribute_in(attribute :: atom(), list :: [term()]) ::
  Ash.Resource.Validation.ref()

Validates that an attribute is being changed to one of a set of specific values, or is in the the given list if it is not being changed.

examples

Examples

validate attribute_in(:state, [1, 2, 3])

# Or to only check for changing to a something in a given list
validate attribute_in(:state, [1, 2, 3]), where: [changing(:state)]
@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)
@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

examples

Examples

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