Class: GovukSchemas::RandomExample

Inherits:
Object
  • Object
show all
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 a oneOf 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

Instance Method Summary collapse

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

Parameters:

  • schema (Hash)

    A JSON schema.

  • seed (Integer, nil) (defaults to: nil)

    A random number seed for deterministic results



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"...}

Parameters:

  • schema_key_value (Hash)
  • the (Block)

    base payload is passed inton the block, with the block result then becoming the new payload. The new payload is then validated. (optional)

Returns:



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"...}

Parameters:

  • the (Block)

    base payload is passed inton the block, with the block result then becoming the new payload. The new payload is then validated. (optional)

Returns:

  • (Hash)

    A content item

Raises:

  • (GovukSchemas::InvalidContentGenerated)


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, error_message(payload, errors) if errors.any?

  payload
end