Class: GovukSchemas::RandomExample
- Inherits:
-
Object
- Object
- GovukSchemas::RandomExample
- Defined in:
- lib/govuk_schemas/random_example.rb
Overview
Generate random content based on a schema.
Limitations
- The gem doesn't support
patternProperties
yet. On GOV.UK we use this in the expanded frontend links. - It's complicated to generate random data for
oneOf
properties. According to the JSON Schema spec aoneOf
schema is only valid if the data is valid against only one of the clauses. To do this properly, we'd have to make sure that the data generated below doesn't validate against the other schemas properties.
Class Method Summary collapse
-
.for_schema(schema_key_value, &block) ⇒ GovukSchemas::RandomExample
Returns a new
GovukSchemas::RandomExample
object.
Instance Method Summary collapse
-
#initialize(schema:, seed: nil) ⇒ GovukSchemas::RandomExample
constructor
Returns a new
GovukSchemas::RandomExample
object. -
#payload(&block) ⇒ Hash
Return a content item merged with a hash and with the excluded fields removed.
Constructor Details
#initialize(schema:, seed: nil) ⇒ GovukSchemas::RandomExample
Returns a new GovukSchemas::RandomExample
object.
For example:
schema = GovukSchemas::Schema.find(frontend_schema: "detailed_guide")
GovukSchemas::RandomExample.new(schema: schema).payload
Example with seed (for consistent results):
schema = GovukSchemas::Schema.find(frontend_schema: "detailed_guide")
GovukSchemas::RandomExample.new(schema: schema, seed: 777).payload
GovukSchemas::RandomExample.new(schema: schema, seed: 777).payload # returns same as above
35 36 37 38 |
# File 'lib/govuk_schemas/random_example.rb', line 35 def initialize(schema:, seed: nil) @schema = schema @random_generator = RandomSchemaGenerator.new(schema:, seed:) end |
Class Method Details
.for_schema(schema_key_value, &block) ⇒ GovukSchemas::RandomExample
Returns a new GovukSchemas::RandomExample
object.
Example without block:
GovukSchemas::RandomExample.for_schema(frontend_schema: "detailed_guide")
# => {"base_path"=>"/e42dd28e", "title"=>"dolor est...", "publishing_app"=>"elit"...}
Example with block:
GovukSchemas::RandomExample.for_schema(frontend_schema: "detailed_guide") do |payload|
payload.merge('base_path' => "Test base path")
end
# => {"base_path"=>"Test base path", "title"=>"dolor est...", "publishing_app"=>"elit"...}
58 59 60 61 |
# File 'lib/govuk_schemas/random_example.rb', line 58 def self.for_schema(schema_key_value, &block) schema = GovukSchemas::Schema.find(schema_key_value) GovukSchemas::RandomExample.new(schema:).payload(&block) end |
Instance Method Details
#payload(&block) ⇒ Hash
Return a content item merged with a hash and with the excluded fields removed. If the resulting content item isn't valid against the schema an error will be raised.
Example without block:
generator.payload
# => {"base_path"=>"/e42dd28e", "title"=>"dolor est...", "publishing_app"=>"elit"...}
Example with block:
generator.payload do |payload|
payload.merge('base_path' => "Test base path")
end
# => {"base_path"=>"Test base path", "title"=>"dolor est...", "publishing_app"=>"elit"...}
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/govuk_schemas/random_example.rb', line 82 def payload(&block) payload = @random_generator.payload return customise_payload(payload, &block) if block errors = validation_errors_for(payload) raise InvalidContentGenerated, (payload, errors) if errors.any? payload end |