govuk_schemas 4.7.0 → 5.0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 34a97a974a896948c52a2e6f4c1460cfbeb931f6aa7ae0a9d148da7b1bb8a890
4
- data.tar.gz: 46de32fac29ba1f29d0b7cc5da20e5e1fa4fe4a7e502912ffd94010b4e0489c3
3
+ metadata.gz: 11fe25fadfae015a3a24eaf4abd396e618821351ce046f5102e3d25f8e2c1b1c
4
+ data.tar.gz: 3ec36c9783619553fbf7a8cafc92af1f6e7788bfb2c7b7b1146c0269a1cf5227
5
5
  SHA512:
6
- metadata.gz: f0da5f9cbaab5127de0a275b0ba156c76a3e65d84661ad8ae86a8bb8bee6764b35b6814a81360fa2d7755123834b327677eb465631205547956c116e7ad93614
7
- data.tar.gz: 93ae4de1d68871747b69455b640df49d4c199032665f2fbf58c5632b0f405db2346450eb2520ff60689278419fec08ed927e5c3b505791bb7ccaec9ae2a8abc6
6
+ metadata.gz: 51c8478cc3b921d0d6f97911f890705634acb7d4fca3d61cee5e3547b2e1ce82e69ac4fbb6df2a7224b3f7710b7a981df4006ec3d3474a8a980254da55af8a85
7
+ data.tar.gz: 14676e6b674e0cf8fbb0957ff9865dbfb91085ea49cffca3156dd15c2616eb15f7b2657679f6508ae28216cbbc95e5356e77f48d0b18f140656eb90a082b8ae1
@@ -11,13 +11,22 @@ on:
11
11
  type: string
12
12
 
13
13
  jobs:
14
+ codeql-sast:
15
+ name: CodeQL SAST scan
16
+ uses: alphagov/govuk-infrastructure/.github/workflows/codeql-analysis.yml@main
17
+ permissions:
18
+ security-events: write
19
+
20
+ dependency-review:
21
+ name: Dependency Review scan
22
+ uses: alphagov/govuk-infrastructure/.github/workflows/dependency-review.yml@main
23
+
14
24
  # This matrix job runs the test suite against multiple Ruby versions
15
25
  test_matrix:
16
26
  strategy:
17
27
  fail-fast: false
18
28
  matrix:
19
- # Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
20
- ruby: ['3.0', 3.1, 3.2]
29
+ ruby: [3.1, 3.2, 3.3]
21
30
  runs-on: ubuntu-latest
22
31
  steps:
23
32
  - uses: actions/checkout@v4
@@ -0,0 +1,7 @@
1
+ api_version: 2
2
+ defaults:
3
+ allowed_semver_bumps:
4
+ - patch
5
+ - minor
6
+ auto_merge: true
7
+ update_external_dependencies: true
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.0.5
1
+ 3.1.4
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ # 5.0.1
2
+
3
+ * Improve speed of random schema generation for customised content items
4
+
5
+ # 5.0.0
6
+
7
+ * BREAKING: Drop support for Ruby 3.0. The minimum required Ruby version is now 3.1.4.
8
+ * Add support for Ruby 3.3.
9
+ * Add support for `json-schema` 3.3.
10
+
1
11
  # 4.7.0
2
12
 
3
13
  * Update `json-schema` dependency.
@@ -19,13 +19,13 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = %w[lib]
20
20
 
21
21
  # This should be kept in sync with the json-schema version of publishing-api.
22
- spec.add_dependency "json-schema", ">= 2.8", "< 4.2"
22
+ spec.add_dependency "json-schema", ">= 2.8", "< 4.4"
23
23
 
24
24
  spec.add_development_dependency "climate_control"
25
25
  spec.add_development_dependency "pry-byebug"
26
26
  spec.add_development_dependency "rake", "~> 13.0"
27
27
  spec.add_development_dependency "rspec", "~> 3.4"
28
- spec.add_development_dependency "rubocop-govuk", "4.12.0"
28
+ spec.add_development_dependency "rubocop-govuk", "4.17.1"
29
29
 
30
- spec.required_ruby_version = ">= 3.0"
30
+ spec.required_ruby_version = ">= 3.1.4"
31
31
  end
@@ -29,11 +29,12 @@ module GovukSchemas
29
29
  # GovukSchemas::RandomExample.new(schema: schema, seed: 777).payload
30
30
  # GovukSchemas::RandomExample.new(schema: schema, seed: 777).payload # returns same as above
31
31
  #
32
- # @param [Hash] schema A JSON schema.
32
+ # @param [Hash] schema A JSON schema.
33
+ # @param [Integer, nil] seed A random number seed for deterministic results
33
34
  # @return [GovukSchemas::RandomExample]
34
35
  def initialize(schema:, seed: nil)
35
36
  @schema = schema
36
- @random_generator = RandomSchemaGenerator.new(schema: schema, seed: seed)
37
+ @random_generator = RandomSchemaGenerator.new(schema:, seed:)
37
38
  end
38
39
 
39
40
  # Returns a new `GovukSchemas::RandomExample` object.
@@ -54,11 +55,9 @@ module GovukSchemas
54
55
  # @param [Block] the base payload is passed inton the block, with the block result then becoming
55
56
  # the new payload. The new payload is then validated. (optional)
56
57
  # @return [GovukSchemas::RandomExample]
57
- # @param [Block] the base payload is passed inton the block, with the block result then becoming
58
- # the new payload. The new payload is then validated. (optional)
59
58
  def self.for_schema(schema_key_value, &block)
60
59
  schema = GovukSchemas::Schema.find(schema_key_value)
61
- GovukSchemas::RandomExample.new(schema: schema).payload(&block)
60
+ GovukSchemas::RandomExample.new(schema:).payload(&block)
62
61
  end
63
62
 
64
63
  # Return a content item merged with a hash and with the excluded fields removed.
@@ -80,24 +79,40 @@ module GovukSchemas
80
79
  # the new payload. The new payload is then validated. (optional)
81
80
  # @return [Hash] A content item
82
81
  # @raise [GovukSchemas::InvalidContentGenerated]
83
- def payload
82
+ def payload(&block)
84
83
  payload = @random_generator.payload
85
- # ensure the base payload is valid
84
+
85
+ return customise_payload(payload, &block) if block
86
+
86
87
  errors = validation_errors_for(payload)
87
88
  raise InvalidContentGenerated, error_message(payload, errors) if errors.any?
88
89
 
89
- if block_given?
90
- payload = yield(payload)
91
- # check the payload again after customisation
92
- errors = validation_errors_for(payload)
93
- raise InvalidContentGenerated, error_message(payload, errors, customised: true) if errors.any?
94
- end
95
-
96
90
  payload
97
91
  end
98
92
 
99
93
  private
100
94
 
95
+ def customise_payload(payload)
96
+ original_payload = payload
97
+ customised_payload = yield(payload)
98
+ customised_errors = validation_errors_for(customised_payload)
99
+
100
+ if customised_errors.any?
101
+ # Check if the original payload had errors and report those over
102
+ # any from customisation. This is not done prior to generating the
103
+ # customised payload because validation is time expensive and we
104
+ # want to avoid it if possible.
105
+ original_errors = validation_errors_for(original_payload)
106
+ errors = original_errors.any? ? original_errors : customised_errors
107
+ payload = original_errors.any? ? original_payload : customised_payload
108
+ message = error_message(payload, errors, customised: original_errors.empty?)
109
+
110
+ raise InvalidContentGenerated, message
111
+ end
112
+
113
+ customised_payload
114
+ end
115
+
101
116
  def validation_errors_for(item)
102
117
  JSON::Validator.fully_validate(@schema, item, errors_as_objects: true)
103
118
  end
@@ -31,7 +31,7 @@ module GovukSchemas
31
31
  # @param schema_type [String] The type: frontend, publisher, notification or links
32
32
  # @return [Hash] a JSON schema as a hash
33
33
  def self.random_schema(schema_type:)
34
- all(schema_type: schema_type).values.sample
34
+ all(schema_type:).values.sample
35
35
  end
36
36
 
37
37
  # Return all schema names
@@ -1,4 +1,4 @@
1
1
  module GovukSchemas
2
2
  # @private
3
- VERSION = "4.7.0".freeze
3
+ VERSION = "5.0.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.7.0
4
+ version: 5.0.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: 2023-09-18 00:00:00.000000000 Z
11
+ date: 2024-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '2.8'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '4.2'
22
+ version: '4.4'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '2.8'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '4.2'
32
+ version: '4.4'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: climate_control
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -92,14 +92,14 @@ dependencies:
92
92
  requirements:
93
93
  - - '='
94
94
  - !ruby/object:Gem::Version
95
- version: 4.12.0
95
+ version: 4.17.1
96
96
  type: :development
97
97
  prerelease: false
98
98
  version_requirements: !ruby/object:Gem::Requirement
99
99
  requirements:
100
100
  - - '='
101
101
  - !ruby/object:Gem::Version
102
- version: 4.12.0
102
+ version: 4.17.1
103
103
  description: Gem to generate test data based on GOV.UK content schemas
104
104
  email:
105
105
  - govuk-dev@digital.cabinet-office.gov.uk
@@ -110,6 +110,7 @@ files:
110
110
  - ".github/dependabot.yml"
111
111
  - ".github/workflows/ci.yml"
112
112
  - ".gitignore"
113
+ - ".govuk_dependabot_merger.yml"
113
114
  - ".rspec"
114
115
  - ".rubocop.yml"
115
116
  - ".ruby-version"
@@ -144,14 +145,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
144
145
  requirements:
145
146
  - - ">="
146
147
  - !ruby/object:Gem::Version
147
- version: '3.0'
148
+ version: 3.1.4
148
149
  required_rubygems_version: !ruby/object:Gem::Requirement
149
150
  requirements:
150
151
  - - ">="
151
152
  - !ruby/object:Gem::Version
152
153
  version: '0'
153
154
  requirements: []
154
- rubygems_version: 3.4.19
155
+ rubygems_version: 3.5.10
155
156
  signing_key:
156
157
  specification_version: 4
157
158
  summary: Gem to generate test data based on GOV.UK content schemas