faker-ruby/faker · error · ArgumentError

invalid amount

Error message

invalid amount

What it means

Raised by the private ensure_valid_amount in Faker::Measurement (called by height, length, volume, weight, metric_height, metric_length, metric_volume, metric_weight via define_measurement_locale) when amount is anything other than the sentinels 'all'/'none' (NONE/ALL), an Integer, or a Float. Strings like '5', nil, or Decimals are rejected before the measurement string is built.

Source

Thrown at lib/faker/default/measurement.rb:156

        end
      end

      def define_measurement_locale(amount, locale)
        ensure_valid_amount(amount)
        case amount
        when ALL
          make_plural(fetch("measurement.#{locale}"))
        when NONE
          fetch("measurement.#{locale}")
        else
          locale = check_for_plural(fetch("measurement.#{locale}"), amount)

          "#{amount} #{locale}"
        end
      end

      def ensure_valid_amount(amount)
        raise ArgumentError, 'invalid amount' unless amount == NONE || amount == ALL || amount.is_a?(Integer) || amount.is_a?(Float)
      end

      def make_plural(text)
        case text
        when 'foot'
          'feet'
        when 'inch'
          'inches'
        when 'fluid ounce'
          'fluid ounces'
        when 'metric ton'
          'metric tons'
        else
          "#{text}s"
        end
      end
    end
  end

View on GitHub (pinned to cca4184947)

Solutions

  1. Pass an Integer or Float (5, 2.5) or the exact strings 'all'/'none'.
  2. Convert numeric strings first: amount: params[:amount].to_i or .to_f.
  3. If amount may legitimately be absent, omit the keyword (each method defaults to rand(10)) instead of passing nil.

Example fix

# before
Faker::Measurement.height(amount: params[:amount]) # '5' from query string -> ArgumentError

# after
Faker::Measurement.height(amount: params[:amount].to_i)
Defensive patterns

Strategy: validation

Validate before calling

amount = Integer(amount) rescue Float(amount) rescue amount
Faker::Measurement.height(amount: amount) if [Integer, Float].include?(amount.class) || %w[all none].include?(amount)

Type guard

def valid_measurement_amount?(amount)
  amount.is_a?(Integer) || amount.is_a?(Float) || amount == 'all' || amount == 'none'
end

Try / catch

begin
  Faker::Measurement.height(amount: amount)
rescue ArgumentError
  Faker::Measurement.height # omit amount; method defaults to rand(10)
end

Prevention

When it happens

Trigger: Faker::Measurement.height(amount: '5') (numeric string), amount: nil, amount: BigDecimal('5'), or amount: [1, 2] raises 'invalid amount'; amount: 5, amount: 2.5, amount: 'all', amount: 'none' are accepted.

Common situations: Passing amounts parsed from params or config files where numbers arrive as strings ('amount=5' from a query string); BigDecimal from ActiveRecord aggregation; forgetting that the pluralization sentinels are the exact strings 'all'/'none', not symbols :all/:none.

Related errors


AI-assisted analysis of faker-ruby/faker@cca4184947 (2026-08-21). Data as JSON: /api/errors/989480bde06b5c6d. Report an issue: GitHub.