publishing_platform_schemas 0.1.2 → 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: 0b9908506b494629e6db1c0e9f7913926bb57d7e7bba972b9cca8f688ee48a3f
4
- data.tar.gz: ae8b12485e1e19ad35971943ca96b50b4703b43d6e16aebcfbb904fa2ab670f9
3
+ metadata.gz: 755a92ec42192fb7a5b02beea49d15893f45fa79c1cb64a30da56eb8e635fa7e
4
+ data.tar.gz: 778ba197d377bd32b434821a0283cbef9245803e3ad964650a51eb487c848d94
5
5
  SHA512:
6
- metadata.gz: 12f1ac7e89d3353f4f83a1f95629022c19328ac7d8942dd95914d7447ef3b2832b1ec89351f12e05300a1bd70698c4b823de8b9c69775957ba58b2c7c6fb9ab4
7
- data.tar.gz: c8da8abc17516ec011c1fa469080b6955513b1b0cc36bbba3a28ac16cbbe40c09851b7573b7e1fa234913869c17960d0972c107887f29de6959113ba12c8c6c6
6
+ metadata.gz: 1e76d0bf70f0711d91cc7b1313157567dd8f6b0f4118cd453f56f0f488c8013335c64cd737648276d3ab9ec3a89afe8d140a7c7ebbf9a9efa1b317b79077e671
7
+ data.tar.gz: b5baf59cbb570a6d6276193aafb71534aa6265787691b83f890aeb5bab2b1dc6672142a7aec26402c4e74c76be17d3b5b7a7db186fb1c4bc2c4e05137a4b287b
@@ -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.2"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,13 +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.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Publishing Platform
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-04-03 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: json-schema
@@ -90,7 +90,9 @@ files:
90
90
  - bin/setup
91
91
  - lib/publishing_platform_schemas.rb
92
92
  - lib/publishing_platform_schemas/example.rb
93
+ - lib/publishing_platform_schemas/rspec_matchers.rb
93
94
  - lib/publishing_platform_schemas/schema.rb
95
+ - lib/publishing_platform_schemas/validator.rb
94
96
  - lib/publishing_platform_schemas/version.rb
95
97
  - publishing_platform_schemas.gemspec
96
98
  licenses:
@@ -110,7 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
112
  - !ruby/object:Gem::Version
111
113
  version: '0'
112
114
  requirements: []
113
- rubygems_version: 3.6.6
115
+ rubygems_version: 3.6.8
114
116
  specification_version: 4
115
117
  summary: Gem to work with the Publishing Platform content schemas
116
118
  test_files: []