View Source Ash.Resource.Validation.Builtins (ash v3.0.0-rc.40)

Built in validations that are available to all resources

The functions in this module are imported by default in the validations 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 argument is not being changed to a specific value, or does not equal the given value if it is not being changed.

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

Validates that an argument 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 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 the absence of a list of attributes.

Validates the presence of a list of attributes.

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 other validation does not pass

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

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

  • :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

validate present(:foo), where: [action_is(:bar)]
Link to this function

argument_does_not_equal(argument, value)

View Source
@spec argument_does_not_equal(argument :: atom(), value :: term()) ::
  Ash.Resource.Validation.ref()

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

Examples

validate argument_does_not_equal(:admin, true)

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

argument_equals(argument, value)

View Source
@spec argument_equals(argument :: atom(), value :: term()) ::
  Ash.Resource.Validation.ref()

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

Examples

validate argument_equals(:admin, true)

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

argument_in(argument, list)

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

Validates that an argument 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

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

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

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

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

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)]
Link to this function

attributes_absent(attributes, opts \\ [])

View Source
@spec attributes_absent(attributes :: atom() | [atom()], opts :: Keyword.t()) ::
  Ash.Resource.Validation.ref()

Validates the absence of a list of attributes.

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

  • :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

attributes_present(attributes, opts \\ [])

View Source
@spec attributes_present(attributes :: atom() | [atom()], opts :: Keyword.t()) ::
  Ash.Resource.Validation.ref()

Validates the presence of a list of attributes.

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

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

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

Validates that an attribute or relationship is being changed

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

  • :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

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

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

validate match(:slug, ~r/^[0-9a-z-_]+$/)
@spec negate(validation :: Ash.Resource.Validation.ref()) ::
  Ash.Resource.Validation.ref()

Validates that other validation does not pass

Examples

validate negate(one_of(:status, [:closed, :finished]))
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

  • :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

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

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

  • :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

Examples

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