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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 755a92ec42192fb7a5b02beea49d15893f45fa79c1cb64a30da56eb8e635fa7e
|
4
|
+
data.tar.gz: 778ba197d377bd32b434821a0283cbef9245803e3ad964650a51eb487c848d94
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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.
|
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:
|
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.
|
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: []
|