AshCommanded.Commanded.Middleware.ValidationMiddleware (AshCommanded v0.1.0)

View Source

Middleware that validates commands before processing.

This middleware applies validation functions to commands before they are dispatched. If validation fails, the command is rejected with an error.

Configuration

Validation can be configured in several ways:

# Simple validation of required fields
middleware AshCommanded.Commanded.Middleware.ValidationMiddleware,
  required: [:id, :name, :email]
  
# Custom validation function
middleware AshCommanded.Commanded.Middleware.ValidationMiddleware,
  validate: fn command ->
    # Return :ok or {:error, reason}
    if String.contains?(command.email, "@") do
      :ok
    else
      {:error, "Invalid email format"}
    end
  end
  
# Multiple validations
middleware AshCommanded.Commanded.Middleware.ValidationMiddleware,
  validations: [
    required: [:id, :name, :email],
    format: [email: ~r/@/],
    custom: fn command ->
      # Custom validation logic
      :ok
    end
  ]