govuk_schemas 4.2.0 → 4.4.1

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
  SHA256:
3
- metadata.gz: 8a44876dfefc37456d8e08d8ba63a9bd95c07425f12d44ca51aafdbbd01e513c
4
- data.tar.gz: '0928c1cae2cf6eb2cf31adfb2eb2d4da30942178ac2e5b9bc2e3681aef3bce3b'
3
+ metadata.gz: ad5834f6042340a4e2749eca28ebe9da5f3c7d4020aa8c80d4139b66b3cabf40
4
+ data.tar.gz: 9d3f754aeeb7e3628a5ee853d60c33c9cfd52c5440e874ab2e7ecdbd0ffd58f6
5
5
  SHA512:
6
- metadata.gz: 14265d1073c7a9b8efe6225d2f7e5508ea91fa4f25e04cb377afe09e9cd3423d98c3fcc319888773301e0f2e7e940b911c159cf83ee184f793f98d756d3909e3
7
- data.tar.gz: b1dd19d78453ac71cae823c987c558f18bcca9f6295908cfb96e6b084a7a85d0543ff72da724fd2c8fa6cc700adbce39b200fcca14ad113333b00a8260b3cc77
6
+ metadata.gz: 3d1fa913598d9058f76c7406878043ad564bd1c443840af54e37a13130890a653abb9ab9725aa85368c449cb610782affc5d89a63e82e76255b130066217a779
7
+ data.tar.gz: d8e9fb819a1f85d3e287377382a1777688becbd2d18be394d7dc6b551f16e5dd1b31ded118c6b469c3240c6483660493c05fed1e6ffcf81648a9a68b15e21522
@@ -0,0 +1,44 @@
1
+ on: [push, pull_request]
2
+
3
+ jobs:
4
+ # This matrix job runs the test suite against multiple Ruby versions
5
+ test_matrix:
6
+ strategy:
7
+ fail-fast: false
8
+ matrix:
9
+ # Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
10
+ ruby: [ 2.7, '3.0', 3.1 ]
11
+ runs-on: ubuntu-latest
12
+ steps:
13
+ - uses: actions/checkout@v2
14
+ - name: Clone govuk-content-schemas
15
+ uses: actions/checkout@v2
16
+ with:
17
+ repository: alphagov/govuk-content-schemas
18
+ ref: deployed-to-production
19
+ path: tmp/govuk-content-schemas
20
+ - uses: ruby/setup-ruby@v1
21
+ with:
22
+ ruby-version: ${{ matrix.ruby }}
23
+ bundler-cache: true
24
+ - run: bundle exec rake
25
+ env:
26
+ GOVUK_CONTENT_SCHEMAS_PATH: tmp/govuk-content-schemas
27
+
28
+ # Branch protection rules cannot directly depend on status checks from matrix jobs.
29
+ # So instead we define `test` as a dummy job which only runs after the preceding `test_matrix` checks have passed.
30
+ # Solution inspired by: https://github.community/t/status-check-for-a-matrix-jobs/127354/3
31
+ test:
32
+ needs: test_matrix
33
+ runs-on: ubuntu-latest
34
+ steps:
35
+ - run: echo "All matrix tests have passed 🚀"
36
+
37
+ publish:
38
+ needs: test
39
+ if: ${{ github.ref == 'refs/heads/main' }}
40
+ permissions:
41
+ contents: write
42
+ uses: alphagov/govuk-infrastructure/.github/workflows/publish-rubygem.yaml@main
43
+ secrets:
44
+ GEM_HOST_API_KEY: ${{ secrets.ALPHAGOV_RUBYGEMS_API_KEY }}
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.6.6
1
+ 2.7.6
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ # 4.4.1
2
+
3
+ * Fix `Validator` module to handle JSON or other object types being passed as the payload ([#68](https://github.com/alphagov/govuk_schemas/pull/68))
4
+
5
+ # 4.4.0
6
+
7
+ * Adds support for applications that use Minitest by adding an `AssertMatchers` module ([#66](https://github.com/alphagov/govuk_schemas/pull/66))
8
+
9
+ # 4.3.0
10
+
11
+ * Generate unique items for arrays with the "uniqueItems" property. ([#63](https://github.com/alphagov/govuk_schemas/pull/63))
12
+
1
13
  # 4.2.0
2
14
 
3
15
  * Add support for generating random HH:MM time strings that match a regex. ([#62](https://github.com/alphagov/govuk_schemas/pull/62))
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "pry-byebug"
25
25
  spec.add_development_dependency "rake", "~> 13.0"
26
26
  spec.add_development_dependency "rspec", "~> 3.4"
27
- spec.add_development_dependency "rubocop-govuk", "~> 3.8"
27
+ spec.add_development_dependency "rubocop-govuk", "~> 4.0"
28
28
  spec.add_development_dependency "yard", "~> 0.8"
29
29
 
30
30
  spec.required_ruby_version = ">= 2.6"
@@ -0,0 +1,28 @@
1
+ require "govuk_schemas/validator"
2
+
3
+ module GovukSchemas
4
+ module AssertMatchers
5
+ def assert_valid_against_publisher_schema(payload, schema_name)
6
+ assert_valid_against_schema(payload, schema_name, "publisher")
7
+ end
8
+
9
+ def assert_valid_against_links_schema(payload, schema_name)
10
+ assert_valid_against_schema(payload, schema_name, "links")
11
+ end
12
+
13
+ def assert_valid_against_frontend_schema(payload, schema_name)
14
+ assert_valid_against_schema(payload, schema_name, "frontend")
15
+ end
16
+
17
+ def assert_valid_against_notification_schema(payload, schema_name)
18
+ assert_valid_against_schema(payload, schema_name, "notification")
19
+ end
20
+
21
+ private
22
+
23
+ def assert_valid_against_schema(payload, schema_name, schema_type)
24
+ validator = GovukSchemas::Validator.new(schema_name, schema_type, payload)
25
+ assert validator.valid?, validator.error_message
26
+ end
27
+ end
28
+ end
@@ -116,12 +116,27 @@ module GovukSchemas
116
116
  def generate_random_array(props)
117
117
  min = props["minItems"] || 0
118
118
  max = props["maxItems"] || 10
119
+ unique = props["uniqueItems"] == true
119
120
  num_items = @random.rand(min..max)
121
+ items = []
122
+ attempts = 0
123
+ max_attempts = num_items * 100
120
124
 
121
- num_items.times.map do
125
+ until items.length == num_items
122
126
  # sometimes arrays don't have `items` specified, not sure if this is a bug
123
- generate_value(props["items"] || {})
127
+ new_value = generate_value(props["items"] || {})
128
+
129
+ if unique && items.include?(new_value)
130
+ attempts += 1
131
+ raise "Failed to create a unique array item after #{max_attempts} attempts" if attempts >= max_attempts
132
+ next
133
+ end
134
+
135
+ attempts = 0
136
+ items << new_value
124
137
  end
138
+
139
+ items
125
140
  end
126
141
 
127
142
  def generate_random_string(props)
@@ -1,57 +1,20 @@
1
+ require "govuk_schemas/validator"
2
+
1
3
  module GovukSchemas
2
4
  module RSpecMatchers
5
+
3
6
  %w[links frontend publisher notification].each do |schema_type|
4
7
  RSpec::Matchers.define "be_valid_against_#{schema_type}_schema".to_sym do |schema_name|
5
8
  match do |item|
6
- schema = Schema.find(Hash["#{schema_type}_schema".to_sym, schema_name])
7
- validator = JSON::Validator.fully_validate(schema, item)
8
- validator.empty?
9
+ @validator = GovukSchemas::Validator.new(schema_name, schema_type, item)
10
+ @validator.valid?
9
11
  end
10
12
 
13
+
11
14
  failure_message do |actual|
12
- ValidationErrorMessage.new(schema_name, "schema", actual).message
15
+ @validator.error_message
13
16
  end
14
17
  end
15
18
  end
16
19
  end
17
-
18
- # @private
19
- class ValidationErrorMessage
20
- attr_reader :schema_name, :type, :payload
21
-
22
- def initialize(schema_name, type, payload)
23
- @schema_name = schema_name
24
- @type = type
25
- @payload = payload
26
- end
27
-
28
- def message
29
- <<~DOC
30
- expected the payload to be valid against the '#{schema_name}' schema:
31
-
32
- #{formatted_payload}
33
-
34
- Validation errors:
35
- #{errors}
36
- DOC
37
- end
38
-
39
- private
40
-
41
- def errors
42
- schema = Schema.find(publisher_schema: schema_name)
43
- validator = JSON::Validator.fully_validate(schema, payload)
44
- validator.map { |message| "- " + humanized_error(message) }.join("\n")
45
- end
46
-
47
- def formatted_payload
48
- return payload if payload.is_a?(String)
49
-
50
- JSON.pretty_generate(payload)
51
- end
52
-
53
- def humanized_error(message)
54
- message.gsub("The property '#/'", "The item")
55
- end
56
- end
57
20
  end
@@ -0,0 +1,51 @@
1
+ module GovukSchemas
2
+ class Validator
3
+ attr_reader :schema_name, :type
4
+ attr_accessor :payload
5
+
6
+ def initialize(schema_name, type, payload)
7
+ @schema_name = schema_name
8
+ @type = type
9
+ @payload = ensure_json(payload)
10
+ end
11
+
12
+ def valid?
13
+ errors.empty?
14
+ end
15
+
16
+ def error_message
17
+ <<~DOC
18
+ expected the payload to be valid against the '#{schema_name}' schema:
19
+
20
+ #{formatted_payload}
21
+
22
+ Validation errors:
23
+ #{errors}
24
+ DOC
25
+ end
26
+
27
+ private
28
+
29
+ def errors
30
+ schema = Schema.find("#{type}_schema": schema_name)
31
+ validator = JSON::Validator.fully_validate(schema, payload)
32
+ validator.map { |message| "- " + humanized_error(message) }.join("\n")
33
+ end
34
+
35
+ def formatted_payload
36
+ JSON.pretty_generate(JSON.parse(payload))
37
+ end
38
+
39
+ def humanized_error(message)
40
+ message.gsub("The property '#/'", "The item")
41
+ .gsub(/in schema [0-9a-f\-]+/, "")
42
+ .strip
43
+ end
44
+
45
+ def ensure_json(payload)
46
+ return payload if payload.is_a?(String)
47
+
48
+ payload.to_json
49
+ end
50
+ end
51
+ end
@@ -1,4 +1,4 @@
1
1
  module GovukSchemas
2
2
  # @private
3
- VERSION = "4.2.0".freeze
3
+ VERSION = "4.4.1".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: 4.2.0
4
+ version: 4.4.1
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: 2020-12-04 00:00:00.000000000 Z
11
+ date: 2022-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '3.8'
75
+ version: '4.0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '3.8'
82
+ version: '4.0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: yard
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -102,6 +102,7 @@ extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
104
  - ".github/dependabot.yml"
105
+ - ".github/workflows/ci.yml"
105
106
  - ".gitignore"
106
107
  - ".rspec"
107
108
  - ".rubocop.yml"
@@ -109,13 +110,13 @@ files:
109
110
  - ".yardopts"
110
111
  - CHANGELOG.md
111
112
  - Gemfile
112
- - Jenkinsfile
113
113
  - LICENSE.md
114
114
  - README.md
115
115
  - Rakefile
116
116
  - bin/console
117
117
  - govuk_schemas.gemspec
118
118
  - lib/govuk_schemas.rb
119
+ - lib/govuk_schemas/assert_matchers.rb
119
120
  - lib/govuk_schemas/document_types.rb
120
121
  - lib/govuk_schemas/example.rb
121
122
  - lib/govuk_schemas/random_content_generator.rb
@@ -123,6 +124,7 @@ files:
123
124
  - lib/govuk_schemas/random_schema_generator.rb
124
125
  - lib/govuk_schemas/rspec_matchers.rb
125
126
  - lib/govuk_schemas/schema.rb
127
+ - lib/govuk_schemas/validator.rb
126
128
  - lib/govuk_schemas/version.rb
127
129
  homepage: https://github.com/alphagov/govuk_schemas_gem
128
130
  licenses:
@@ -143,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
145
  - !ruby/object:Gem::Version
144
146
  version: '0'
145
147
  requirements: []
146
- rubygems_version: 3.1.4
148
+ rubygems_version: 3.3.18
147
149
  signing_key:
148
150
  specification_version: 4
149
151
  summary: Gem to generate test data based on GOV.UK content schemas
data/Jenkinsfile DELETED
@@ -1,9 +0,0 @@
1
- #!/usr/bin/env groovy
2
-
3
- library("govuk")
4
-
5
- node {
6
- govuk.buildProject(
7
- rubyLintDiff: false,
8
- )
9
- }