Lavash.Form.ValidationTranspiler (Lavash v0.4.0-rc.1)

Copy Markdown View Source

Transpiles Ash resource validations (from validations do block) to client-side validation.

Unlike ConstraintTranspiler which reads type constraints on attributes, this module reads explicit validations which support custom messages.

Example

In your Ash resource:

validations do
  validate present(:card_number), message: "Enter a card number"
  validate string_length(:card_number, min: 15, max: 16), message: "Enter a valid card number"
end

These messages will be used in client-side validation instead of the generic defaults.

Summary

Functions

Extracts validations for a specific action from an Ash resource.

Gets the custom message for a validation, or generates a default.

Checks if a resource has any custom-message validations we should use.

Functions

extract_validations_for_action(resource, action_name)

Extracts validations for a specific action from an Ash resource.

Returns a map of field_name => list of validation specs. Each validation spec contains:

  • :type - the validation type (:present, :string_length, etc.)
  • :opts - validation options (min, max, etc.)
  • :message - custom message (or nil for default)

get_message(validation_spec, value \\ nil)

Gets the custom message for a validation, or generates a default.

Supports interpolation of variables like %{min}, %{max}.

has_custom_validations?(resource, action_name)

Checks if a resource has any custom-message validations we should use.

Returns true if there are validations with custom messages that would override the default constraint transpiler messages.