Class: GovukSchemas::RandomExample
- Inherits:
-
Object
- Object
- GovukSchemas::RandomExample
- Defined in:
- lib/govuk_schemas/random_example.rb
Class Method Summary collapse
-
.for_schema(schema_key_value) ⇒ GovukSchemas::RandomExample
Returns a new ‘GovukSchemas::RandomExample` object.
Instance Method Summary collapse
-
#initialize(schema:) ⇒ GovukSchemas::RandomExample
constructor
Returns a new ‘GovukSchemas::RandomExample` object.
-
#merge_and_validate(user_defined_values) ⇒ Hash
Return a content item merged with a hash.
-
#payload ⇒ Hash
Return a hash with a random content item.
Constructor Details
#initialize(schema:) ⇒ GovukSchemas::RandomExample
Returns a new ‘GovukSchemas::RandomExample` object.
For example:
schema = GovukSchemas::Schema.find(frontend_schema: "detailed_guide")
GovukSchemas::RandomExample.new(schema: schema).payload
17 18 19 20 |
# File 'lib/govuk_schemas/random_example.rb', line 17 def initialize(schema:) @schema = schema @random_generator = RandomItemGenerator.new(schema: schema) end |
Class Method Details
.for_schema(schema_key_value) ⇒ GovukSchemas::RandomExample
Returns a new ‘GovukSchemas::RandomExample` object.
For example:
generator = GovukSchemas::RandomExample.for_schema(frontend_schema: "detailed_guide")
generator.payload
# => {"base_path"=>"/e42dd28e", "title"=>"dolor est...", "publishing_app"=>"elit"...}
32 33 34 35 |
# File 'lib/govuk_schemas/random_example.rb', line 32 def self.for_schema(schema_key_value) schema = GovukSchemas::Schema.find(schema_key_value) GovukSchemas::RandomExample.new(schema: schema) end |
Instance Method Details
#merge_and_validate(user_defined_values) ⇒ Hash
Return a content item merged with a hash. If the resulting content item isn’t valid against the schema an error will be raised.
Example:
random = GovukSchemas::RandomExample.for_schema("detailed_guide", schema_type: "frontend")
random.merge_and_validate(base_path: "/foo")
# => {"base_path"=>"/e42dd28e", "title"=>"dolor est...", "publishing_app"=>"elit"...}
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/govuk_schemas/random_example.rb', line 68 def merge_and_validate(user_defined_values) random_payload = @random_generator.payload item_merged_with_user_input = random_payload.merge(Utils.stringify_keys(user_defined_values)) errors = validation_errors_for(item_merged_with_user_input) if errors.any? errors_on_random_payload = validation_errors_for(random_payload) if errors_on_random_payload.any? # The original item was invalid when it was generated, so it's not # the users fault. raise InvalidContentGenerated, (random_payload, errors) else # The random item was valid, but it was merged with something invalid. raise InvalidContentGenerated, (item_merged_with_user_input, user_defined_values, errors) end end item_merged_with_user_input end |
#payload ⇒ Hash
Return a hash with a random content item
Example:
GovukSchemas::RandomExample.for_schema("detailed_guide", schema_type: "frontend").payload
# => {"base_path"=>"/e42dd28e", "title"=>"dolor est...", "publishing_app"=>"elit"...}
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/govuk_schemas/random_example.rb', line 45 def payload item = @random_generator.payload errors = validation_errors_for(item) if errors.any? raise InvalidContentGenerated, (item, errors) end item end |