faker-ruby/faker · error · ArgumentError
invalid period
Error message
invalid period
What it means
Raised by the private hours helper in Faker::Time, reached from every public generator that takes period: — between_dates, forward, and backward — when period is not a key of TIME_RANGES. Valid symbols are exactly :all, :day, :night, :morning, :afternoon, :evening, :midnight; the lookup is a Hash key comparison, so strings ('morning'), symbols with typos (:morn), or synonyms like :dawn/:dusk/:noon raise ArgumentError.
Source
Thrown at lib/faker/default/time.rb:129
# #=> "14 Oct 07:44"
#
# @faker.version 1.5.0
def backward(days: 365, period: :all, format: nil)
time_with_format(date_with_random_time(Faker::Date.backward(days: days), period), format)
end
private
def date_with_random_time(date, period)
::Time.local(date.year, date.month, date.day, hours(period), minutes, seconds)
end
def time_with_format(time, format)
format.nil? ? time : I18n.localize(time, format: format)
end
def hours(period)
raise ArgumentError, 'invalid period' unless TIME_RANGES.key? period
sample(TIME_RANGES[period].to_a)
end
def minutes
seconds
end
def seconds
sample((0..59).to_a)
end
def get_time_object(time)
time = ::Time.parse(time) if time.is_a? String
time = time.to_time if time.respond_to?(:to_time)
time
end
endView on GitHub (pinned to cca4184947)
Solutions
- Pass one of :all, :day, :night, :morning, :afternoon, :evening, :midnight as a Symbol.
- Convert config/param strings: period: params[:period].to_sym (after whitelist-checking).
- Pick the closest defined range for synonyms you want (e.g. use :morning for 'dawn' since it starts at 6h, or :midnight for 0..4).
Example fix
# before Faker::Time.between_dates(from: from_d, to: to_d, period: params[:period]) # 'morning' String -> ArgumentError # after period = params[:period].presence&.to_sym || :all Faker::Time.between_dates(from: from_d, to: to_d, period: period)
Defensive patterns
Strategy: validation
Validate before calling
VALID_PERIODS = %i[all day night morning afternoon evening midnight].freeze period = period.to_sym if period.is_a?(String) period = :all unless VALID_PERIODS.include?(period) Faker::Time.between_dates(from: from_d, to: to_d, period: period)
Type guard
def valid_faker_period?(period) Faker::Time::TIME_RANGES.key?(period) end
Try / catch
begin Faker::Time.forward(days: days, period: period) rescue ArgumentError Faker::Time.forward(days: days) # default :all end
Prevention
- period must be a Symbol from TIME_RANGES; convert strings with to_sym after whitelisting.
- Use Faker::Time::TIME_RANGES.key? as the source of truth, not a copied list.
- No dawn/dusk/am/pm aliases exist — map synonyms to the nearest defined range.
When it happens
Trigger: Faker::Time.forward(days: 5, period: 'morning') (string instead of symbol), period: :dusk, or period: :am raises 'invalid period'; period: :morning or omitting period (defaults to :all) works. between_dates(from: ..., to: ..., period: :noon) raises too.
Common situations: Passing period values read from params/config where they arrive as strings; using natural synonyms (dawn/dusk) that TIME_RANGES does not define; copying the option name from another library (e.g. Date naming) rather than Faker's list.
Related errors
- Supported colorizations are #{SUPPORTED_COLORIZATIONS.join('
- Supported formats are #{SUPPORTED_FORMATS.join(', ')}
- Familial connections can be left blank or #{familial_connect
- Filters do not match any sciences
- Valid credit cards argument can be left blank or include #{v
AI-assisted analysis of faker-ruby/faker@cca4184947 (2026-08-21).
Data as JSON: /api/errors/a7907898edea3c0e.
Report an issue: GitHub.