-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from alphagov/json-schema-integration
Add JSON schema validation to integration tests
- Loading branch information
Showing
6 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,4 +35,5 @@ | |
end | ||
|
||
config.include FixtureHelpers | ||
config.include SchemaHelpers | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
RSpec::Matchers.define :match_json_schema do |expected_schema| | ||
match do |json_string| | ||
@errors = expected_schema.validate(json_string).map { _1["error"] } | ||
@errors.none? | ||
end | ||
|
||
failure_message do | ||
"Expected JSON to match schema, but validation failed:\n#{@errors.join("\n")}" | ||
end | ||
|
||
failure_message_when_negated do | ||
"Expected JSON not to match schema, but it did." | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module SchemaHelpers | ||
# TODO: This is the best option out of a bad bunch right now as this schema lives in a different | ||
# repository. Maybe eventually we will vendor the schema, or get it directly from the GCP API | ||
# client? | ||
METADATA_JSON_SCHEMA_URI = URI.parse( | ||
"https://raw.githubusercontent.com/alphagov/search-v2-infrastructure/main/terraform/modules/google_discovery_engine_restapi/files/datastore_schema.json", | ||
) | ||
|
||
# Returns a JSONSchemer object representing the JSON schema for document metadata | ||
def metadata_json_schema | ||
@metadata_json_schema ||= begin | ||
remote_schema = Net::HTTP.get(METADATA_JSON_SCHEMA_URI) | ||
schema_contents = JSON.parse(remote_schema) | ||
|
||
JSONSchemer.schema(schema_contents) | ||
end | ||
end | ||
end |