faker-ruby/faker · error · ArgumentError

Valid credit cards argument can be left blank or include #{v

Error message

Valid credit cards argument can be left blank or include #{valid_tokens.join(', ')}

What it means

Raised by Faker::Stripe.valid_token when card_type is not a key under faker.stripe.valid_tokens in the loaded locale (e.g. :visa, :mastercard, :amex...). Like valid_card, the argument is symified before comparison, so 'visa' and :visa both pass while anything absent from the locale section (e.g. 'visa_debit' in locales that lack that token key) raises.

Source

Thrown at lib/faker/default/stripe.rb:50

      ##
      # Produces a random valid Stripe token.
      #
      # @param card_type [String] Specific valid card type.
      # @return [String]
      #
      # @example
      #   Faker::Stripe.valid_token #=> "tok_visa"
      #   Faker::Stripe.valid_token(card_type: "mc_debit") #=> "tok_mastercard_debit"
      #
      # @faker.version 1.9.0
      def valid_token(card_type: nil)
        valid_tokens = translate('faker.stripe.valid_tokens').keys

        if card_type.nil?
          card_type = sample(valid_tokens).to_s
        else
          unless valid_tokens.include?(card_type.to_sym)
            raise ArgumentError,
                  "Valid credit cards argument can be left blank or include #{valid_tokens.join(', ')}"
          end
        end

        fetch("stripe.valid_tokens.#{card_type}")
      end

      ##
      # Produces a random invalid card number.
      #
      # @return [String]
      #
      # @example
      #   Faker::Stripe.invalid_card #=> "4000000000000002"
      #   Faker::Stripe.invalid_card(card_error: "addressZipFail") #=> "4000000000000010"
      #
      # @faker.version 1.9.0
      def invalid_card(card_error: nil)

View on GitHub (pinned to cca4184947)

Solutions

  1. Omit card_type to get a random valid token.
  2. Use a type listed in the error message (string or lowercase symbol), e.g. card_type: 'mc_debit'.
  3. Derive the accepted set at runtime from the locale (I18n translate keys under faker.stripe.valid_tokens) instead of hardcoding.

Example fix

# before
Faker::Stripe.valid_token(card_type: 'discover') # ArgumentError

# after
Faker::Stripe.valid_token(card_type: 'mc_debit')
# or: Faker::Stripe.valid_token
Defensive patterns

Strategy: validation

Validate before calling

valid_types = I18n.translate('faker.stripe.valid_tokens').keys.map(&:to_s)
card_type = nil unless card_type.nil? || valid_types.include?(card_type.to_s)
Faker::Stripe.valid_token(card_type: card_type)

Type guard

def known_stripe_token_type?(card_type)
  I18n.translate('faker.stripe.valid_tokens').key?(card_type.to_s.to_sym)
end

Try / catch

begin
  Faker::Stripe.valid_token(card_type: card_type)
rescue ArgumentError
  Faker::Stripe.valid_token # random token
end

Prevention

When it happens

Trigger: Faker::Stripe.valid_token(card_type: 'discover') when discover is not defined under stripe.valid_tokens raises; valid_token() with no argument samples randomly and never raises.

Common situations: Assuming the token keys mirror the card keys exactly (valid_cards and valid_tokens sections can differ); sharing one card-type constant list between valid_card and valid_token calls; locale data differences across faker versions.

Related errors


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