govuk_schemas 2.1.1 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +1 -1
- data/lib/govuk_schemas/random_example.rb +18 -24
- data/lib/govuk_schemas/rspec_matchers.rb +65 -0
- data/lib/govuk_schemas/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e3774ac312636e52ae77ba38deab3b6f1e749cf
|
4
|
+
data.tar.gz: 43ab66a728f531e8881da0daf96ec0327b42baf5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 231ae75cf41f46f6417a365b1b9bf39bad8fdf49cc65e3dec554371cd6dcbb92b6f47db3e3c1615c14d535c91ae37ad8511790ac72cc08560683b59b9022c72c
|
7
|
+
data.tar.gz: 772c6a9f27ca821ac05d2a5eae881b9be999b25649c6143efe56507c88156f47b900031ef69617390f0726ad5f32dcab0fbf841d112c8cba62d2f1f5c693572c
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -34,41 +34,23 @@ module GovukSchemas
|
|
34
34
|
GovukSchemas::RandomExample.new(schema: schema)
|
35
35
|
end
|
36
36
|
|
37
|
-
# Return a
|
38
|
-
#
|
39
|
-
# Example:
|
40
|
-
#
|
41
|
-
# GovukSchemas::RandomExample.for_schema("detailed_guide", schema_type: "frontend").payload
|
42
|
-
# # => {"base_path"=>"/e42dd28e", "title"=>"dolor est...", "publishing_app"=>"elit"...}
|
43
|
-
#
|
44
|
-
# @return [Hash] A content item
|
45
|
-
def payload
|
46
|
-
item = @random_generator.payload
|
47
|
-
errors = validation_errors_for(item)
|
48
|
-
|
49
|
-
if errors.any?
|
50
|
-
raise InvalidContentGenerated, error_message(item, errors)
|
51
|
-
end
|
52
|
-
|
53
|
-
item
|
54
|
-
end
|
55
|
-
|
56
|
-
# Return a content item merged with a hash. If the resulting content item
|
57
|
-
# isn't valid against the schema an error will be raised.
|
37
|
+
# Return a content item merged with a hash and with the excluded fields removed.
|
38
|
+
# If the resulting content item isn't valid against the schema an error will be raised.
|
58
39
|
#
|
59
40
|
# Example:
|
60
41
|
#
|
61
42
|
# random = GovukSchemas::RandomExample.for_schema("detailed_guide", schema_type: "frontend")
|
62
|
-
# random.
|
43
|
+
# random.customise_and_validate({base_path: "/foo"}, ["withdrawn_notice"])
|
63
44
|
# # => {"base_path"=>"/e42dd28e", "title"=>"dolor est...", "publishing_app"=>"elit"...}
|
64
45
|
#
|
65
46
|
# @param [Hash] hash The hash to merge the random content with
|
47
|
+
# @param [Array] array The array containing fields to exclude
|
66
48
|
# @return [Hash] A content item
|
67
49
|
# @raise [GovukSchemas::InvalidContentGenerated]
|
68
|
-
def
|
50
|
+
def customise_and_validate(user_defined_values = {}, fields_to_exclude = [])
|
69
51
|
random_payload = @random_generator.payload
|
70
52
|
item_merged_with_user_input = random_payload.merge(Utils.stringify_keys(user_defined_values))
|
71
|
-
|
53
|
+
item_merged_with_user_input.reject! { |k| fields_to_exclude.include?(k) }
|
72
54
|
errors = validation_errors_for(item_merged_with_user_input)
|
73
55
|
if errors.any?
|
74
56
|
|
@@ -86,6 +68,18 @@ module GovukSchemas
|
|
86
68
|
item_merged_with_user_input
|
87
69
|
end
|
88
70
|
|
71
|
+
# Return a hash with a random content item
|
72
|
+
#
|
73
|
+
# Example:
|
74
|
+
#
|
75
|
+
# GovukSchemas::RandomExample.for_schema("detailed_guide", schema_type: "frontend").payload
|
76
|
+
# # => {"base_path"=>"/e42dd28e", "title"=>"dolor est...", "publishing_app"=>"elit"...}
|
77
|
+
#
|
78
|
+
# @return [Hash] A content item
|
79
|
+
# Support backwards compatibility
|
80
|
+
alias :payload :customise_and_validate
|
81
|
+
alias :merge_and_validate :customise_and_validate
|
82
|
+
|
89
83
|
private
|
90
84
|
|
91
85
|
def validation_errors_for(item)
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module GovukSchemas
|
2
|
+
module RSpecMatchers
|
3
|
+
RSpec::Matchers.define :be_valid_against_schema do |schema_name|
|
4
|
+
match do |item|
|
5
|
+
schema = Schema.find(publisher_schema: schema_name)
|
6
|
+
validator = JSON::Validator.fully_validate(schema, item)
|
7
|
+
validator.empty?
|
8
|
+
end
|
9
|
+
|
10
|
+
failure_message do |actual|
|
11
|
+
ValidationErrorMessage.new(schema_name, "schema", actual).message
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
RSpec::Matchers.define :be_valid_against_links_schema do |schema_name|
|
16
|
+
match do |item|
|
17
|
+
schema = Schema.find(links_schema: schema_name)
|
18
|
+
validator = JSON::Validator.fully_validate(schema, item)
|
19
|
+
validator.empty?
|
20
|
+
end
|
21
|
+
|
22
|
+
failure_message do |actual|
|
23
|
+
ValidationErrorMessage.new(schema_name, "links", actual).message
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class ValidationErrorMessage
|
29
|
+
attr_reader :schema_name, :type, :payload
|
30
|
+
|
31
|
+
def initialize(schema_name, type, payload)
|
32
|
+
@schema_name = schema_name
|
33
|
+
@type = type
|
34
|
+
@payload = payload
|
35
|
+
end
|
36
|
+
|
37
|
+
def message
|
38
|
+
<<~doc
|
39
|
+
expected the payload to be valid against the '#{schema_name}' schema:
|
40
|
+
|
41
|
+
#{formatted_payload}
|
42
|
+
|
43
|
+
Validation errors:
|
44
|
+
#{errors}
|
45
|
+
doc
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def errors
|
51
|
+
schema = Schema.find(publisher_schema: schema_name)
|
52
|
+
validator = JSON::Validator.fully_validate(schema, payload)
|
53
|
+
validator.map { |message| "- " + humanized_error(message) }.join("\n")
|
54
|
+
end
|
55
|
+
|
56
|
+
def formatted_payload
|
57
|
+
return payload if payload.is_a?(String)
|
58
|
+
JSON.pretty_generate(payload)
|
59
|
+
end
|
60
|
+
|
61
|
+
def humanized_error(message)
|
62
|
+
message.gsub("The property '#/'", "The item")
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: govuk_schemas
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- GOV.UK Dev
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-08-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json-schema
|
@@ -135,6 +135,7 @@ files:
|
|
135
135
|
- lib/govuk_schemas/random.rb
|
136
136
|
- lib/govuk_schemas/random_example.rb
|
137
137
|
- lib/govuk_schemas/random_item_generator.rb
|
138
|
+
- lib/govuk_schemas/rspec_matchers.rb
|
138
139
|
- lib/govuk_schemas/schema.rb
|
139
140
|
- lib/govuk_schemas/utils.rb
|
140
141
|
- lib/govuk_schemas/version.rb
|