View Source KineticEcto.ChangesetValidations (KineticEcto v1.1.1)

Additional validations for use with Ecto.Changeset.

Summary

Functions

Validates that the field or fields provided are not changed once set.

Validates that at least one of the fields is present (not nil or blank) in the changeset.

Functions

validate_immutable(changeset, field, opts \\ [])

Validates that the field or fields provided are not changed once set.

If the data value (in the incoming struct) is present (not nil or blank), then any change which contains an update to that value will be rejected.

validate_some_required(changeset, fields, opts \\ [])

Validates that at least one of the fields is present (not nil or blank) in the changeset.

fields must be a list of at least two schema field names.

Options

  • :minimum: The minimum number of fields that must have a value, default 1. The value provided will be clamped between 0 and the number of fields specified.
  • :maximum: The maximum number of fields that must have a value, default nil. Unspecified by default. If specified, will be clamped between specified, must be at least one and greater than or equal to minimum and will be clamped to the number of fields specified.
  • :minimum_message: The message to display when the minimum threshold of presence is not met.
  • :maximum_message: The message to display when the maximum threshold of presence is exceeded.
  • :exact_message: The message to display when minimum and maximum are the same exceeded.
  • :message: The message to be displayed if forwarded to validate_required/2.

There are two special cases:

  • If minimum is the number of fields passed, this calls validate_required/2.
  • If minimum is 0 and maximum is unspecified, this validation will always pass.

On validation failure, the minimum_message or maximum_message will be added to each field specified in fields.

The table below shows the special cases considered.

MinimumMaximumResult
0nilpass
#fields-Ecto.Changeset.validate_required/3
< 0-minimum = 0
-< 1maximum = 1
> #fields-minimum = #fields
-> #fieldsmaximum = #fields
-< minimummaximum = minimum