lib/fakeit/openapi/example/string_example.rb in fakeit-0.6.2 vs lib/fakeit/openapi/example/string_example.rb in fakeit-0.6.3
- old
+ new
@@ -25,10 +25,12 @@
'binary' => -> { Faker::String.random(length: 1..30) },
'byte' => -> { Base64.strict_encode64(Faker::String.random(length: 1..30)) }
}.freeze
def string_example(example_options)
+ @string_pattern ||= Regexp.new(pattern) if pattern
+
if example_options[:use_static][type: 'string', property: example_options[:property]]
static_string_example
else
random_string_example
end
@@ -37,11 +39,11 @@
private
def static_string_example
fixed_faker do
if enum then enum.to_a.first
- elsif pattern then Faker::Base.regexify(pattern)
+ elsif pattern then static_string_pattern
elsif format then static_string_format
elsif length_constraint then static_string_with_length
else 'string'
end
end
@@ -54,11 +56,11 @@
result
end
def random_string_example
if enum then enum.to_a.sample
- elsif pattern then Faker::Base.regexify(pattern)
+ elsif pattern then random_string_pattern
elsif format then random_string_format
elsif length_constraint then string_with_length
else Faker::Book.title
end
end
@@ -69,10 +71,16 @@
def static_string_format
(STATIC_FORMAT_HANDLERS[format] || method(:unknown_format))[]
end
+ def static_string_pattern
+ @static_string_pattern ||= @string_pattern.examples(
+ max_repeater_variance: 1, max_group_results: 1, max_results_limit: 1
+ ).first
+ end
+
def length_constraint
minLength || maxLength
end
def string_with_length
@@ -87,9 +95,13 @@
maxLength || min_string_length + 10
end
def random_string_format
(RANDOM_FORMAT_HANDLERS[format] || method(:unknown_format))[]
+ end
+
+ def random_string_pattern
+ @random_string_pattern ||= @string_pattern.random_example(max_repeater_variance: 1)
end
def unknown_format
Fakeit::Logger.info("Unknown string format: #{format}")
'Unknown string format'