Skip to content

Commit

Permalink
Merge pull request #115 from alphagov/fix-modifying-hash
Browse files Browse the repository at this point in the history
Fix incorrect error for invalid customised payload
  • Loading branch information
kevindew authored May 23, 2024
2 parents 96875f3 + 5497366 commit eb9767b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 5.0.2

* Fix issue where customised schema validation message was incorrect for a modified payload

# 5.0.1

* Improve speed of random schema generation for customised content items
Expand Down
3 changes: 2 additions & 1 deletion lib/govuk_schemas/random_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ def payload(&block)
private

def customise_payload(payload)
original_payload = payload
# Use Marshal to create a deep dup of the payload so the original can be mutated
original_payload = Marshal.load(Marshal.dump(payload))
customised_payload = yield(payload)
customised_errors = validation_errors_for(customised_payload)

Expand Down
2 changes: 1 addition & 1 deletion lib/govuk_schemas/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module GovukSchemas
# @private
VERSION = "5.0.1".freeze
VERSION = "5.0.2".freeze
end
11 changes: 11 additions & 0 deletions spec/lib/random_example_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@
}.to raise_error(GovukSchemas::InvalidContentGenerated, /The item was valid before being customised/)
end

it "raises when modifying the hash directly creates an invalid content item" do
schema = GovukSchemas::Schema.random_schema(schema_type: "frontend")

expect {
GovukSchemas::RandomExample.new(schema:).payload do |hash|
hash["base_path"] = nil
hash
end
}.to raise_error(GovukSchemas::InvalidContentGenerated, /The item was valid before being customised/)
end

it "raises if the non-customised content item was invalid" do
generator = instance_double(GovukSchemas::RandomSchemaGenerator, payload: {})
allow(GovukSchemas::RandomSchemaGenerator).to receive(:new).and_return(generator)
Expand Down

0 comments on commit eb9767b

Please sign in to comment.