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
patternPropertiesyet. On GOV.UK we use this in the expanded frontend links. - It's complicated to generate random data for
oneOfproperties. According to the JSON Schema spec aoneOfschema 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::RandomExampleobject.
Instance Method Summary collapse
-
#initialize(schema:, seed: nil) ⇒ GovukSchemas::RandomExample
constructor
Returns a new
GovukSchemas::RandomExampleobject. -
#payload ⇒ 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
34 35 36 37 |
# File 'lib/govuk_schemas/random_example.rb', line 34 def initialize(schema:, seed: nil) @schema = schema @random_generator = RandomSchemaGenerator.new(schema: schema, seed: 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"...}
59 60 61 62 |
# File 'lib/govuk_schemas/random_example.rb', line 59 def self.for_schema(schema_key_value, &block) schema = GovukSchemas::Schema.find(schema_key_value) GovukSchemas::RandomExample.new(schema: schema).payload(&block) end |
Instance Method Details
#payload ⇒ 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"...}
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/govuk_schemas/random_example.rb', line 83 def payload payload = @random_generator.payload # ensure the base payload is valid errors = validation_errors_for(payload) raise InvalidContentGenerated, (payload, errors) if errors.any? if block_given? payload = yield(payload) # check the payload again after customisation errors = validation_errors_for(payload) raise InvalidContentGenerated, (payload, errors, customised: true) if errors.any? end payload end |