lib/govuk_schemas/random.rb in govuk_schemas-0.2.0 vs lib/govuk_schemas/random.rb in govuk_schemas-1.0.0

- old
+ new

@@ -26,10 +26,15 @@ def base_path "/" + rand(1..5).times.map { SecureRandom.uuid }.join('/') end + def govuk_campaign_url + subdomain = rand(1..5).times.map { ('a'..'z').to_a.sample(rand(1..5)).join }.join('.') + "https://#{subdomain}.campaign.gov.uk#{base_path}" + end + def string(minimum_chars = nil, maximum_chars = nil) minimum_chars = minimum_chars || 0 maximum_chars = maximum_chars || 100 WORDS.sample(rand(minimum_chars..maximum_chars)).join(' ') end @@ -58,13 +63,26 @@ anchor when "[a-z-]" Utils.parameterize(WORDS.sample(rand(1..10)).join('-')) when "^/(([a-zA-Z0-9._~!$&'()*+,;=:@-]|%[0-9a-fA-F]{2})+(/([a-zA-Z0-9._~!$&'()*+,;=:@-]|%[0-9a-fA-F]{2})*)*)?(\\?([a-zA-Z0-9._~!$&'()*+,;=:@-]|%[0-9a-fA-F]{2})*)?(#([a-zA-Z0-9._~!$&'()*+,;=:@-]|%[0-9a-fA-F]{2})*)?$" base_path + when "^https://([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[A-Za-z0-9])?\\.)+campaign\\.gov\\.uk(/(([a-zA-Z0-9._~!$&'()*+,;=:@-]|%[0-9a-fA-F]{2})+(/([a-zA-Z0-9._~!$&'()*+,;=:@-]|%[0-9a-fA-F]{2})*)*)?(\\?([a-zA-Z0-9._~!$&'()*+,;=:@-]|%[0-9a-fA-F]{2})*)?(#([a-zA-Z0-9._~!$&'()*+,;=:@-]|%[0-9a-fA-F]{2})*)?)?$" + govuk_campaign_url when '[a-z0-9\-_]' "#{SecureRandom.hex}-#{SecureRandom.hex}" else - raise "Regex pattern not found: #{pattern.inspect}" + raise <<-doc + Don't know how to generate random string for pattern #{pattern.inspect} + + This propably means you've introduced a new regex in govuk-content-schemas. + Because it's very hard to generate a valid string from a regex alone, + we have to specify a method to generate random data for each regex in + the schemas. + + To fix this: + + - Add your regex to `lib/govuk_schemas/random.rb` + doc end end end end end