Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure the validator can handle the payload being JSON #68

Merged

Conversation

davidgisbey
Copy link
Contributor

Description

At the moment, there's some disparity between how applications that user Minitest and applications that use RSpec pass their payloads.

RSpec applications pass the payload as JSON and Minitest applications pass it as a Hash.

Currently, the payload is being converted to JSON before being validated by JSON.fully_validate. If the object is already JSON then .to_json is called again. This means when JSON.parse is called within JSON.fully_validate on the payload it does not convert it to a hash.

This commit updates the validator to ensure that the payload not converted to JSON if it is already a string.

Trello card

https://trello.com/c/Q9O5rXnz/550-stop-using-the-deprecated-govuk-content-schema-test-helpers-gem

@davidgisbey davidgisbey force-pushed the ensure-validator-works-with-object-or-json-passed-in branch 2 times, most recently from 5f6ada0 to f4f69f7 Compare July 22, 2022 11:14
Copy link

@ollietreend ollietreend left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work 👍🏻

A couple of thoughts.

Comment on lines 9 to 10
@payload = payload
ensure_payload_is_json

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be nicer to implement this as a method which returns a JSON string – rather than a method with side effects.

e.g.

Suggested change
@payload = payload
ensure_payload_is_json
@payload = ensure_json(payload)

It feels a bit weird to be setting @payload and then immediately updating it in ensure_payload_is_json.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm yea i think that's a good shout actually. Will update now 👍

@@ -1,4 +1,4 @@
module GovukSchemas
# @private
VERSION = "4.4.0".freeze
VERSION = "4.5.0".freeze

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this is more suited to being a patch release rather than a minor release. It seems like this is a bug-fix rather than a new feature. 🤔

Suggested change
VERSION = "4.5.0".freeze
VERSION = "4.4.1".freeze

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea good shout. Will update 👍

At the moment, there's some disparity between how applications that
user Minitest and applications that use RSpec pass their payloads.

RSpec applications pass the payload as JSON and Minitest applications
pass it as a Hash.

Currently, the payload is being converted to JSON before being validated
by `JSON.fully_validate`. If the object is already JSON then .to_json is
called again. This means when JSON.parse is called on the object it does
not convert it back to a hash.

This commit updates the validator to ensure that the payload is
converted to JSON if the object passed is not a string.
@davidgisbey davidgisbey force-pushed the ensure-validator-works-with-object-or-json-passed-in branch from f4f69f7 to 72e1e6e Compare July 22, 2022 11:33
Copy link

@ollietreend ollietreend left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍🏻

@davidgisbey davidgisbey merged commit b658cb2 into main Jul 22, 2022
@davidgisbey davidgisbey deleted the ensure-validator-works-with-object-or-json-passed-in branch July 22, 2022 11:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants