publishing_platform_schemas 0.1.1 → 0.2.0

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: 8316073ac789d764c1f29489aea360d74f3b657f58eb501e57994306b38c5fe9
4
- data.tar.gz: 0b7aaccb03710ac2d67ab4dbb5960835f614d5aebc2312eac28faa800512c12b
3
+ metadata.gz: 755a92ec42192fb7a5b02beea49d15893f45fa79c1cb64a30da56eb8e635fa7e
4
+ data.tar.gz: 778ba197d377bd32b434821a0283cbef9245803e3ad964650a51eb487c848d94
5
5
  SHA512:
6
- metadata.gz: e8518793ce8cb1b579474a408cc1c8308f18fc18021769c2eb6364330cc2892a4275b96eb3ff1cdc3c6991fdc8d8a1e0ad1e0f3ead74f3f777e14e270f36f0a1
7
- data.tar.gz: 549f68aa69f26c2a66e3b036b15597510ee24804ce021452aa7a96b011e7e5a467e689521d9505005be8fddaf6e0ae6e8f906a8183cafa639fe520ee188f9ecd
6
+ metadata.gz: 1e76d0bf70f0711d91cc7b1313157567dd8f6b0f4118cd453f56f0f488c8013335c64cd737648276d3ab9ec3a89afe8d140a7c7ebbf9a9efa1b317b79077e671
7
+ data.tar.gz: b5baf59cbb570a6d6276193aafb71534aa6265787691b83f890aeb5bab2b1dc6672142a7aec26402c4e74c76be17d3b5b7a7db186fb1c4bc2c4e05137a4b287b
@@ -0,0 +1,38 @@
1
+ module PublishingPlatformSchemas
2
+ class Example
3
+ # Find all examples for a schema
4
+ #
5
+ # @param schema_name [String] like "detailed_guide", "policy" or "publication"
6
+ # @return [Array] array of example content items
7
+ def self.find_all(schema_name)
8
+ Dir.glob("#{examples_path(schema_name)}/*.json").map do |filename|
9
+ json = File.read(filename)
10
+ JSON.parse(json)
11
+ end
12
+ end
13
+
14
+ # Find an example by name
15
+ #
16
+ # @param schema_name [String] like "detailed_guide", "policy" or "publication"
17
+ # @param example_name [String] the name of the example JSON file
18
+ # @return [Hash] the example content item
19
+ def self.find(schema_name, example_name:)
20
+ json = File.read("#{examples_path(schema_name)}/#{example_name}.json")
21
+ JSON.parse(json)
22
+ end
23
+
24
+ # Examples are changing location in schemas, this allows this utility
25
+ # to work with both locations
26
+ #
27
+ # @param schema_name [String] like "detailed_guide", "policy" or "publication"
28
+ # @return [String] the path to use for examples
29
+ def self.examples_path(schema_name)
30
+ examples_dir = "#{PublishingPlatformSchemas.content_schema_dir}/examples"
31
+ if Dir.exist?(examples_dir)
32
+ "#{examples_dir}/#{schema_name}/frontend"
33
+ else
34
+ "#{PublishingPlatformSchemas.content_schema_dir}/formats/#{schema_name}/frontend/examples"
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,16 @@
1
+ require "publishing_platform_schemas/validator"
2
+
3
+ module PublishingPlatformSchemas
4
+ module RSpecMatchers
5
+ %w[links frontend publisher notification].each do |schema_type|
6
+ RSpec::Matchers.define "be_valid_against_#{schema_type}_schema".to_sym do |schema_name|
7
+ match do |item|
8
+ @validator = PublishingPlatformSchemas::Validator.new(schema_name, schema_type, item)
9
+ @validator.valid?
10
+ end
11
+
12
+ failure_message { @validator.error_message }
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,53 @@
1
+ require "json-schema"
2
+
3
+ module PublishingPlatformSchemas
4
+ class Validator
5
+ attr_reader :schema_name, :type
6
+ attr_accessor :payload
7
+
8
+ def initialize(schema_name, type, payload)
9
+ @schema_name = schema_name
10
+ @type = type
11
+ @payload = ensure_json(payload)
12
+ end
13
+
14
+ def valid?
15
+ errors.empty?
16
+ end
17
+
18
+ def error_message
19
+ <<~DOC
20
+ expected the payload to be valid against the '#{schema_name}' schema:
21
+
22
+ #{formatted_payload}
23
+
24
+ Validation errors:
25
+ #{errors}
26
+ DOC
27
+ end
28
+
29
+ private
30
+
31
+ def errors
32
+ schema = Schema.find("#{type}_schema": schema_name)
33
+ validator = JSON::Validator.fully_validate(schema, payload)
34
+ validator.map { |message| "- #{humanized_error(message)}" }.join("\n")
35
+ end
36
+
37
+ def formatted_payload
38
+ JSON.pretty_generate(JSON.parse(payload))
39
+ end
40
+
41
+ def humanized_error(message)
42
+ message.gsub("The property '#/'", "The item")
43
+ .gsub(/in schema [0-9a-f-]+/, "")
44
+ .strip
45
+ end
46
+
47
+ def ensure_json(payload)
48
+ return payload if payload.is_a?(String)
49
+
50
+ payload.to_json
51
+ end
52
+ end
53
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PublishingPlatformSchemas
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "publishing_platform_schemas/version"
4
4
  require_relative "publishing_platform_schemas/schema"
5
+ require_relative "publishing_platform_schemas/example"
5
6
 
6
7
  module PublishingPlatformSchemas
7
8
  def self.content_schema_dir=(path_to_schemas)
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: publishing_platform_schemas
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Publishing Platform
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-11-29 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: json-schema
@@ -73,7 +72,6 @@ dependencies:
73
72
  - !ruby/object:Gem::Version
74
73
  version: '0'
75
74
  description: Gem to work with the Publishing Platform content schemas
76
- email:
77
75
  executables: []
78
76
  extensions: []
79
77
  extra_rdoc_files: []
@@ -91,14 +89,15 @@ files:
91
89
  - bin/console
92
90
  - bin/setup
93
91
  - lib/publishing_platform_schemas.rb
92
+ - lib/publishing_platform_schemas/example.rb
93
+ - lib/publishing_platform_schemas/rspec_matchers.rb
94
94
  - lib/publishing_platform_schemas/schema.rb
95
+ - lib/publishing_platform_schemas/validator.rb
95
96
  - lib/publishing_platform_schemas/version.rb
96
97
  - publishing_platform_schemas.gemspec
97
- homepage:
98
98
  licenses:
99
99
  - MIT
100
100
  metadata: {}
101
- post_install_message:
102
101
  rdoc_options: []
103
102
  require_paths:
104
103
  - lib
@@ -113,8 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
112
  - !ruby/object:Gem::Version
114
113
  version: '0'
115
114
  requirements: []
116
- rubygems_version: 3.5.23
117
- signing_key:
115
+ rubygems_version: 3.6.8
118
116
  specification_version: 4
119
117
  summary: Gem to work with the Publishing Platform content schemas
120
118
  test_files: []