govuk_schemas 4.1.1 → 4.4.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
  SHA256:
3
- metadata.gz: 8123e8bc3461346b31359b47c260930e11505206315aedbfde7532b1601a1e7e
4
- data.tar.gz: 3ad819db46a04377edda67d60f3a49c08493723a9b40eefd0d72eade6e1b752c
3
+ metadata.gz: e46784ff30a78aee2321e3defb7e2b31ea6eb751c96ddec7ba2ebe695a42b29a
4
+ data.tar.gz: 3f6d24f1c5afb4f175d9157f84ced30b20e475b76958c7128872c179aa7bb62d
5
5
  SHA512:
6
- metadata.gz: ad57ca91dbe8784fa643ebcab765565480523243fd21829240afc77337da373c0a40d25cfccb4efb225725cc7e40998192d44bb262992170b56fa1aeb38bbe2a
7
- data.tar.gz: 2cb1d9f10a775ece8e9143a8de21738484d44f1702c14312da55579c8c0cbbad439b28e0eb554ec840a3c8bee3c551340c3ad5d3efd5273cbf4ad46deaac3afb
6
+ metadata.gz: c2d875e00eaf8440b82c7a9173dbf23a708aabb49157bd960c7c7a94e715ba6429b43864f6b168b2e8fe1c3daa8743a76e6662f02fdf0db86e59e5af585b7b1d
7
+ data.tar.gz: 166c8ecb0f9f850e682eba2181ba6187321428fcee4f9dd1e707a6791ee8b5da3d422aef3c404d5d8a175d3601c0fc6388bb8c995dd72c6353a3628886a0ea81
@@ -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.0
2
+
3
+ * Adds support for applications that use Minitest by adding an `AssertMatchers` module ([#66](https://github.com/alphagov/govuk_schemas/pull/66))
4
+
5
+ # 4.3.0
6
+
7
+ * Generate unique items for arrays with the "uniqueItems" property. ([#63](https://github.com/alphagov/govuk_schemas/pull/63))
8
+
9
+ # 4.2.0
10
+
11
+ * Add support for generating random HH:MM time strings that match a regex. ([#62](https://github.com/alphagov/govuk_schemas/pull/62))
12
+
1
13
  # 4.1.1
2
14
 
3
15
  * Fix RandomSchemaGenerator.new always returning equivalent generators ([#60](https://github.com/alphagov/govuk_schemas/pull/60))
@@ -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
@@ -81,6 +81,8 @@ module GovukSchemas
81
81
  Date.today.iso8601
82
82
  when "^[1-9][0-9]{3}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[0-1])$"
83
83
  Date.today.iso8601
84
+ when "^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$"
85
+ Time.now.strftime("%H:%m")
84
86
  when "^#.+$"
85
87
  anchor
86
88
  when "[a-z-]"
@@ -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,46 @@
1
+ module GovukSchemas
2
+ class Validator
3
+ attr_reader :schema_name, :type, :payload
4
+
5
+ def initialize(schema_name, type, payload)
6
+ @schema_name = schema_name
7
+ @type = type
8
+ @payload = payload
9
+ end
10
+
11
+ def valid?
12
+ errors.empty?
13
+ end
14
+
15
+ def error_message
16
+ <<~DOC
17
+ expected the payload to be valid against the '#{schema_name}' schema:
18
+
19
+ #{formatted_payload}
20
+
21
+ Validation errors:
22
+ #{errors}
23
+ DOC
24
+ end
25
+
26
+ private
27
+
28
+ def errors
29
+ schema = Schema.find("#{type}_schema": schema_name)
30
+ validator = JSON::Validator.fully_validate(schema, payload.to_json)
31
+ validator.map { |message| "- " + humanized_error(message) }.join("\n")
32
+ end
33
+
34
+ def formatted_payload
35
+ return payload if payload.is_a?(String)
36
+
37
+ JSON.pretty_generate(payload)
38
+ end
39
+
40
+ def humanized_error(message)
41
+ message.gsub("The property '#/'", "The item")
42
+ .gsub(/in schema [0-9a-f\-]+/, "")
43
+ .strip
44
+ end
45
+ end
46
+ end
@@ -1,4 +1,4 @@
1
1
  module GovukSchemas
2
2
  # @private
3
- VERSION = "4.1.1".freeze
3
+ VERSION = "4.4.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: 4.1.1
4
+ version: 4.4.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: 2020-08-17 00:00:00.000000000 Z
11
+ date: 2022-07-21 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.0.3
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
- }