govuk_schemas 4.4.0 → 4.5.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: e46784ff30a78aee2321e3defb7e2b31ea6eb751c96ddec7ba2ebe695a42b29a
4
- data.tar.gz: 3f6d24f1c5afb4f175d9157f84ced30b20e475b76958c7128872c179aa7bb62d
3
+ metadata.gz: b7843ee3adf4523d3d0a0a8c722f5ae3175edaf3a4d1ee248a4fdafee9b596cf
4
+ data.tar.gz: b04e4942d146c69782e5b0e70f2d441e0f4aa6822afa054ad78d77cd40aaab7f
5
5
  SHA512:
6
- metadata.gz: c2d875e00eaf8440b82c7a9173dbf23a708aabb49157bd960c7c7a94e715ba6429b43864f6b168b2e8fe1c3daa8743a76e6662f02fdf0db86e59e5af585b7b1d
7
- data.tar.gz: 166c8ecb0f9f850e682eba2181ba6187321428fcee4f9dd1e707a6791ee8b5da3d422aef3c404d5d8a175d3601c0fc6388bb8c995dd72c6353a3628886a0ea81
6
+ metadata.gz: 14d5fb78761952ed355573bfd62c5cef13d2b723011a7bad969c5cf43926231e8a02af778aadcc514e73af3137703fa950b11264b90ac5111192c252f567f1f6
7
+ data.tar.gz: 6e13df6f72f725fc504fb4151ca849dd2faeeae26c7d0c3f16f923ce3eec218293d3f66642d9237634c9666c481731f018b47df739ec8719929c82c1429b47dc
@@ -4,14 +4,3 @@ updates:
4
4
  directory: /
5
5
  schedule:
6
6
  interval: daily
7
- allow:
8
- # Internal gems
9
- - dependency-name: "govuk*"
10
- dependency-type: direct
11
- - dependency-name: rubocop-govuk
12
- dependency-type: direct
13
- # Framework gems
14
- - dependency-name: rake
15
- dependency-type: direct
16
- - dependency-name: rspec
17
- dependency-type: direct
@@ -11,19 +11,19 @@ jobs:
11
11
  runs-on: ubuntu-latest
12
12
  steps:
13
13
  - uses: actions/checkout@v2
14
- - name: Clone govuk-content-schemas
14
+ - name: Clone content-schemas
15
15
  uses: actions/checkout@v2
16
16
  with:
17
- repository: alphagov/govuk-content-schemas
17
+ repository: alphagov/publishing-api
18
18
  ref: deployed-to-production
19
- path: tmp/govuk-content-schemas
19
+ path: tmp/publishing-api
20
20
  - uses: ruby/setup-ruby@v1
21
21
  with:
22
22
  ruby-version: ${{ matrix.ruby }}
23
23
  bundler-cache: true
24
24
  - run: bundle exec rake
25
25
  env:
26
- GOVUK_CONTENT_SCHEMAS_PATH: tmp/govuk-content-schemas
26
+ GOVUK_CONTENT_SCHEMAS_PATH: tmp/publishing-api/content_schemas
27
27
 
28
28
  # Branch protection rules cannot directly depend on status checks from matrix jobs.
29
29
  # So instead we define `test` as a dummy job which only runs after the preceding `test_matrix` checks have passed.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ # 4.5.0
2
+
3
+ General support for content schemas in publishing api:
4
+ * Update default content schemas url to point to publishing api rather than govuk-content-schemas. This is because we are merging schemas into publishing api.
5
+ * Update path of allowed_document_types.yml to reflect new location in publishing api, allowing us to remove a symlink.
6
+ * Introduce a setter method for manually configuring the path to schemas, outside of an env variable
7
+
8
+ # 4.4.1
9
+
10
+ * Fix `Validator` module to handle JSON or other object types being passed as the payload ([#68](https://github.com/alphagov/govuk_schemas/pull/68))
11
+
1
12
  # 4.4.0
2
13
 
3
14
  * Adds support for applications that use Minitest by adding an `AssertMatchers` module ([#66](https://github.com/alphagov/govuk_schemas/pull/66))
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # GOV.UK Schemas
2
2
 
3
- Gem to work with the [govuk-content-schemas](https://github.com/alphagov/govuk-content-schemas).
3
+ Gem to work with the [schemas in publishing api](https://github.com/alphagov/publishing-api/tree/main/content_schemas).
4
4
 
5
5
  ## Installation
6
6
 
@@ -16,7 +16,7 @@ gem "govuk_schemas", "~> VERSION"
16
16
 
17
17
  ## Running the test suite
18
18
 
19
- Make sure you have `govuk-content-schemas` cloned in a sibling directory:
19
+ Make sure you have `publishing-api` cloned in a sibling directory:
20
20
 
21
21
  ```
22
22
  bundle exec rake
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
+ require "rubocop/rake_task"
3
4
 
4
5
  RSpec::Core::RakeTask.new(:spec)
6
+ RuboCop::RakeTask.new(:lint)
5
7
 
6
- task default: [:spec]
8
+ task default: %i[lint spec]
@@ -18,9 +18,10 @@ Gem::Specification.new do |spec|
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = %w[lib]
20
20
 
21
- # This should be kept in sync with the json-schema version of govuk-content-schemas.
22
- spec.add_dependency "json-schema", "~> 2.8.0"
21
+ # This should be kept in sync with the json-schema version of publishing-api.
22
+ spec.add_dependency "json-schema", ">= 2.8", "< 3.1"
23
23
 
24
+ spec.add_development_dependency "climate_control"
24
25
  spec.add_development_dependency "pry-byebug"
25
26
  spec.add_development_dependency "rake", "~> 13.0"
26
27
  spec.add_development_dependency "rspec", "~> 3.4"
@@ -3,7 +3,7 @@ module GovukSchemas
3
3
  # Return all of the document types on GOV.UK
4
4
  def self.valid_document_types
5
5
  @valid_document_types ||= begin
6
- filename = "#{GovukSchemas::CONTENT_SCHEMA_DIR}/lib/govuk_content_schemas/allowed_document_types.yml"
6
+ filename = "#{GovukSchemas.content_schema_dir}/allowed_document_types.yml"
7
7
  YAML.load_file(filename)
8
8
  end
9
9
  end
@@ -27,11 +27,11 @@ module GovukSchemas
27
27
  # @param schema_name [String] like "detailed_guide", "policy" or "publication"
28
28
  # @return [String] the path to use for examples
29
29
  def self.examples_path(schema_name)
30
- examples_dir = "#{GovukSchemas::CONTENT_SCHEMA_DIR}/examples"
30
+ examples_dir = "#{GovukSchemas.content_schema_dir}/examples"
31
31
  if Dir.exist?(examples_dir)
32
32
  "#{examples_dir}/#{schema_name}/frontend"
33
33
  else
34
- "#{GovukSchemas::CONTENT_SCHEMA_DIR}/formats/#{schema_name}/frontend/examples"
34
+ "#{GovukSchemas.content_schema_dir}/formats/#{schema_name}/frontend/examples"
35
35
  end
36
36
  end
37
37
  end
@@ -8,9 +8,10 @@ module GovukSchemas
8
8
  end
9
9
 
10
10
  def string_for_type(type)
11
- if type == "date-time"
11
+ case type
12
+ when "date-time"
12
13
  time
13
- elsif type == "uri"
14
+ when "uri"
14
15
  uri
15
16
  else
16
17
  raise "Unknown attribute type `#{type}`"
@@ -28,7 +29,7 @@ module GovukSchemas
28
29
  end
29
30
 
30
31
  def base_path
31
- "/" + @random.rand(1..5).times.map { uuid }.join("/")
32
+ "/#{@random.rand(1..5).times.map { uuid }.join('/')}"
32
33
  end
33
34
 
34
35
  def govuk_subdomain_url
@@ -101,7 +102,7 @@ module GovukSchemas
101
102
  raise <<-DOC
102
103
  Don't know how to generate random string for pattern #{pattern.inspect}
103
104
 
104
- This propably means you've introduced a new regex in govuk-content-schemas.
105
+ This propably means you've introduced a new regex in publishing api.
105
106
  Because it's very hard to generate a valid string from a regex alone,
106
107
  we have to specify a method to generate random data for each regex in
107
108
  the schemas.
@@ -9,7 +9,7 @@ module GovukSchemas
9
9
  #
10
10
  # - The gem doesn't support `patternProperties` yet. On GOV.UK we [use this in
11
11
  # the expanded frontend
12
- # links](https://github.com/alphagov/govuk-content-schemas/blob/bdd97d18c7a9318e66f332f0748a410fddab1141/formats/frontend_links_definition.json#L67-L71).
12
+ # links](https://github.com/alphagov/publishing-api/blob/a8039d430e44c86c3f54a69569f07ad48a4fc912/content_schemas/formats/shared/definitions/frontend_links.jsonnet#L118-L121).
13
13
  # - It's complicated to generate random data for `oneOf` properties. According
14
14
  # to the JSON Schema spec a `oneOf` schema is only valid if the data is valid
15
15
  # against *only one* of the clauses. To do this properly, we'd have to make
@@ -90,7 +90,7 @@ module GovukSchemas
90
90
  payload = yield(payload)
91
91
  # check the payload again after customisation
92
92
  errors = validation_errors_for(payload)
93
- raise InvalidContentGenerated, error_message(payload, errors, true) if errors.any?
93
+ raise InvalidContentGenerated, error_message(payload, errors, customised: true) if errors.any?
94
94
  end
95
95
 
96
96
  payload
@@ -102,7 +102,7 @@ module GovukSchemas
102
102
  JSON::Validator.fully_validate(@schema, item, errors_as_objects: true)
103
103
  end
104
104
 
105
- def error_message(item, errors, customised = false)
105
+ def error_message(item, errors, customised: false)
106
106
  details = <<~ERR
107
107
  Generated payload:
108
108
  --------------------------
@@ -103,11 +103,7 @@ module GovukSchemas
103
103
  next unless should_generate_value
104
104
 
105
105
  one_of_properties = (one_of_sample["properties"] || {})[attribute_name]
106
- document[attribute_name] = if one_of_properties
107
- generate_value(one_of_properties)
108
- else
109
- generate_value(attribute_properties)
110
- end
106
+ document[attribute_name] = generate_value(one_of_properties || attribute_properties)
111
107
  end
112
108
 
113
109
  document
@@ -129,6 +125,7 @@ module GovukSchemas
129
125
  if unique && items.include?(new_value)
130
126
  attempts += 1
131
127
  raise "Failed to create a unique array item after #{max_attempts} attempts" if attempts >= max_attempts
128
+
132
129
  next
133
130
  end
134
131
 
@@ -2,7 +2,6 @@ require "govuk_schemas/validator"
2
2
 
3
3
  module GovukSchemas
4
4
  module RSpecMatchers
5
-
6
5
  %w[links frontend publisher notification].each do |schema_type|
7
6
  RSpec::Matchers.define "be_valid_against_#{schema_type}_schema".to_sym do |schema_name|
8
7
  match do |item|
@@ -10,10 +9,7 @@ module GovukSchemas
10
9
  @validator.valid?
11
10
  end
12
11
 
13
-
14
- failure_message do |actual|
15
- @validator.error_message
16
- end
12
+ failure_message { @validator.error_message }
17
13
  end
18
14
  end
19
15
  end
@@ -11,7 +11,7 @@ module GovukSchemas
11
11
  # GovukSchemas::Schema.find(notification_schema: "detailed_guide")
12
12
  # @return [Hash] the JSON schema as a hash
13
13
  def self.find(schema)
14
- file_path = "#{GovukSchemas::CONTENT_SCHEMA_DIR}/dist/formats/#{location_for_schema_name(schema)}"
14
+ file_path = "#{GovukSchemas.content_schema_dir}/dist/formats/#{location_for_schema_name(schema)}"
15
15
  JSON.parse(File.read(file_path))
16
16
  end
17
17
 
@@ -21,7 +21,7 @@ module GovukSchemas
21
21
  # @return [Array<Hash>] List of JSON schemas as hashes
22
22
  def self.all(schema_type: "*")
23
23
  schema_type = "publisher_v2" if schema_type == "publisher"
24
- Dir.glob("#{GovukSchemas::CONTENT_SCHEMA_DIR}/dist/formats/*/#{schema_type}/*.json").each_with_object({}) do |file_path, hash|
24
+ Dir.glob("#{GovukSchemas.content_schema_dir}/dist/formats/*/#{schema_type}/*.json").each_with_object({}) do |file_path, hash|
25
25
  hash[file_path] = JSON.parse(File.read(file_path))
26
26
  end
27
27
  end
@@ -38,7 +38,7 @@ module GovukSchemas
38
38
  #
39
39
  # @return [Array] all the schema names
40
40
  def self.schema_names
41
- Dir.glob("#{GovukSchemas::CONTENT_SCHEMA_DIR}/dist/formats/*").map do |directory|
41
+ Dir.glob("#{GovukSchemas.content_schema_dir}/dist/formats/*").map do |directory|
42
42
  File.basename(directory)
43
43
  end
44
44
  end
@@ -1,11 +1,12 @@
1
1
  module GovukSchemas
2
2
  class Validator
3
- attr_reader :schema_name, :type, :payload
3
+ attr_reader :schema_name, :type
4
+ attr_accessor :payload
4
5
 
5
6
  def initialize(schema_name, type, payload)
6
7
  @schema_name = schema_name
7
8
  @type = type
8
- @payload = payload
9
+ @payload = ensure_json(payload)
9
10
  end
10
11
 
11
12
  def valid?
@@ -27,14 +28,12 @@ module GovukSchemas
27
28
 
28
29
  def errors
29
30
  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")
31
+ validator = JSON::Validator.fully_validate(schema, payload)
32
+ validator.map { |message| "- #{humanized_error(message)}" }.join("\n")
32
33
  end
33
34
 
34
35
  def formatted_payload
35
- return payload if payload.is_a?(String)
36
-
37
- JSON.pretty_generate(payload)
36
+ JSON.pretty_generate(JSON.parse(payload))
38
37
  end
39
38
 
40
39
  def humanized_error(message)
@@ -42,5 +41,11 @@ module GovukSchemas
42
41
  .gsub(/in schema [0-9a-f\-]+/, "")
43
42
  .strip
44
43
  end
44
+
45
+ def ensure_json(payload)
46
+ return payload if payload.is_a?(String)
47
+
48
+ payload.to_json
49
+ end
45
50
  end
46
51
  end
@@ -1,4 +1,4 @@
1
1
  module GovukSchemas
2
2
  # @private
3
- VERSION = "4.4.0".freeze
3
+ VERSION = "4.5.0".freeze
4
4
  end
data/lib/govuk_schemas.rb CHANGED
@@ -5,8 +5,13 @@ require "govuk_schemas/document_types"
5
5
  require "govuk_schemas/example"
6
6
 
7
7
  module GovukSchemas
8
- # @private
9
- CONTENT_SCHEMA_DIR = ENV["GOVUK_CONTENT_SCHEMAS_PATH"] || "../govuk-content-schemas"
8
+ def self.content_schema_dir=(path_to_schemas)
9
+ @content_schema_dir = path_to_schemas
10
+ end
11
+
12
+ def self.content_schema_dir
13
+ @content_schema_dir ||= ENV.fetch("GOVUK_CONTENT_SCHEMAS_PATH", "../publishing-api/content_schemas")
14
+ end
10
15
 
11
16
  # @private
12
17
  class InvalidContentGenerated < RuntimeError
metadata CHANGED
@@ -1,29 +1,49 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govuk_schemas
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.4.0
4
+ version: 4.5.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: 2022-07-21 00:00:00.000000000 Z
11
+ date: 2022-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2.8'
20
+ - - "<"
18
21
  - !ruby/object:Gem::Version
19
- version: 2.8.0
22
+ version: '3.1'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
25
28
  - !ruby/object:Gem::Version
26
- version: 2.8.0
29
+ version: '2.8'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '3.1'
33
+ - !ruby/object:Gem::Dependency
34
+ name: climate_control
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
27
47
  - !ruby/object:Gem::Dependency
28
48
  name: pry-byebug
29
49
  requirement: !ruby/object:Gem::Requirement
@@ -145,7 +165,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
165
  - !ruby/object:Gem::Version
146
166
  version: '0'
147
167
  requirements: []
148
- rubygems_version: 3.3.18
168
+ rubygems_version: 3.3.26
149
169
  signing_key:
150
170
  specification_version: 4
151
171
  summary: Gem to generate test data based on GOV.UK content schemas