govuk_schemas 2.1.1 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7ae3fcbab8090c1fc21b77822f02f3d13ad1e6b7
4
- data.tar.gz: e417e9c8d4ee30225e1e55c9852af054692f1c45
3
+ metadata.gz: 1e3774ac312636e52ae77ba38deab3b6f1e749cf
4
+ data.tar.gz: 43ab66a728f531e8881da0daf96ec0327b42baf5
5
5
  SHA512:
6
- metadata.gz: 5e87fd5bc7f3c42d7e6cf1193361a9056b6da54d14c7579173ffa3c4b94ea04c932194fe597a84930dcced18c893032dee84834afad9b20e14b8a2cf147b5ed1
7
- data.tar.gz: e803c13e394c7778836a8c87c416de4a38d81f75ab79d44659b1caf0adc4bff314ee2986f59b4e8e9480ea43543aadd9e567b6f30d538d9e9775a61c20332f16
6
+ metadata.gz: 231ae75cf41f46f6417a365b1b9bf39bad8fdf49cc65e3dec554371cd6dcbb92b6f47db3e3c1615c14d535c91ae37ad8511790ac72cc08560683b59b9022c72c
7
+ data.tar.gz: 772c6a9f27ca821ac05d2a5eae881b9be999b25649c6143efe56507c88156f47b900031ef69617390f0726ad5f32dcab0fbf841d112c8cba62d2f1f5c693572c
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 2.2.0
2
+
3
+ * Add RSpec test helpers
4
+ * Add `customise_and_validate` to bring the functionality to remove fields from a payload which has been generated as a random example
5
+
1
6
  # 2.1.1
2
7
 
3
8
  * Support for a new regex in GOV.UK content schemas
data/README.md CHANGED
@@ -17,7 +17,7 @@ gem "govuk_schemas", "~> VERSION"
17
17
 
18
18
  ## Usage
19
19
 
20
- [Read the documentation!](https://alphagov.github.io/govuk_schemas_gem/frames.html)
20
+ [Read the documentation!](https://alphagov.github.io/govuk_schemas/frames.html)
21
21
 
22
22
  ## Running the test suite
23
23
 
@@ -34,41 +34,23 @@ module GovukSchemas
34
34
  GovukSchemas::RandomExample.new(schema: schema)
35
35
  end
36
36
 
37
- # Return a hash with a random content item
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.merge_and_validate(base_path: "/foo")
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 merge_and_validate(user_defined_values)
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
@@ -1,4 +1,4 @@
1
1
  module GovukSchemas
2
2
  # @private
3
- VERSION = "2.1.1".freeze
3
+ VERSION = "2.2.0".freeze
4
4
  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.1.1
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-04-11 00:00:00.000000000 Z
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