json_schemer 0.2.12 → 0.2.17
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +3 -4
- data/Gemfile.lock +8 -8
- data/README.md +14 -0
- data/json_schemer.gemspec +3 -3
- data/lib/json_schemer/format.rb +9 -2
- data/lib/json_schemer/schema/base.rb +51 -31
- data/lib/json_schemer/version.rb +1 -1
- metadata +9 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '048e6a2fadd3b4717233464de16b5777ebf94bacd20a44005de1b309d93110db'
|
4
|
+
data.tar.gz: 599aa58b678b2d52b559281b5760fff0f0668e0bf8c771635bbf41db814ffe8b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c806dea39ba83179cda20f09af807314a3b1c1bf82ff79e496f10c45cea28a9597afebe88f7ed57d8d7c28542a68d53beaff1dd3521386c1fc981f38ff09b27f
|
7
|
+
data.tar.gz: 271d8b4d761d60c29ee598298aefc91543f5edeb3e9b7369f6a0cfee67c07b16eb1a8c5874dfab712cd27fc779e4b4218a1511515ea5a2bf295bc85ab316072b
|
data/.github/workflows/ci.yml
CHANGED
@@ -4,14 +4,13 @@ jobs:
|
|
4
4
|
ruby:
|
5
5
|
strategy:
|
6
6
|
matrix:
|
7
|
-
ruby: [2.5
|
7
|
+
ruby: [2.4, 2.5, 2.6, 2.7, head, truffleruby-head]
|
8
8
|
runs-on: ubuntu-latest
|
9
9
|
steps:
|
10
|
-
- uses: actions/checkout@
|
11
|
-
- uses:
|
10
|
+
- uses: actions/checkout@v2
|
11
|
+
- uses: ruby/setup-ruby@v1
|
12
12
|
with:
|
13
13
|
ruby-version: ${{ matrix.ruby }}
|
14
14
|
- run: |
|
15
|
-
gem install bundler
|
16
15
|
bundle install
|
17
16
|
bundle exec rake test
|
data/Gemfile.lock
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
json_schemer (0.2.
|
5
|
-
ecma-re-validator (~> 0.
|
4
|
+
json_schemer (0.2.17)
|
5
|
+
ecma-re-validator (~> 0.3)
|
6
6
|
hana (~> 1.3)
|
7
|
-
regexp_parser (~>
|
7
|
+
regexp_parser (~> 2.0)
|
8
8
|
uri_template (~> 0.7)
|
9
9
|
|
10
10
|
GEM
|
11
11
|
remote: https://rubygems.org/
|
12
12
|
specs:
|
13
|
-
ecma-re-validator (0.
|
14
|
-
regexp_parser (~>
|
15
|
-
hana (1.3.
|
16
|
-
minitest (5.
|
13
|
+
ecma-re-validator (0.3.0)
|
14
|
+
regexp_parser (~> 2.0)
|
15
|
+
hana (1.3.7)
|
16
|
+
minitest (5.14.2)
|
17
17
|
rake (13.0.1)
|
18
|
-
regexp_parser (
|
18
|
+
regexp_parser (2.0.0)
|
19
19
|
uri_template (0.7.0)
|
20
20
|
|
21
21
|
PLATFORMS
|
data/README.md
CHANGED
@@ -92,6 +92,20 @@ JSONSchemer.schema(
|
|
92
92
|
# default: false
|
93
93
|
insert_property_defaults: true,
|
94
94
|
|
95
|
+
# modify properties during validation. You can pass one Proc or a list of Procs to modify data.
|
96
|
+
# Proc/[Proc]
|
97
|
+
# default: nil
|
98
|
+
before_property_validation: proc do |data, property, property_schema, _parent|
|
99
|
+
data[property] ||= 42
|
100
|
+
end,
|
101
|
+
|
102
|
+
# modify properties after validation. You can pass one Proc or a list of Procs to modify data.
|
103
|
+
# Proc/[Proc]
|
104
|
+
# default: nil
|
105
|
+
after_property_validation: proc do |data, property, property_schema, _parent|
|
106
|
+
data[property] = Date.iso8601(data[property]) if property_schema.is_a?(Hash) && property_schema['format'] == 'date'
|
107
|
+
end,
|
108
|
+
|
95
109
|
# resolve external references
|
96
110
|
# 'net/http'/proc/lambda/respond_to?(:call)
|
97
111
|
# 'net/http': proc { |uri| JSON.parse(Net::HTTP.get(uri)) }
|
data/json_schemer.gemspec
CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
21
|
spec.require_paths = ["lib"]
|
22
22
|
|
23
|
-
spec.required_ruby_version = '
|
23
|
+
spec.required_ruby_version = '>= 2.4'
|
24
24
|
|
25
25
|
spec.add_development_dependency "bundler", "~> 2.0"
|
26
26
|
spec.add_development_dependency "rake", "~> 13.0"
|
@@ -34,8 +34,8 @@ Gem::Specification.new do |spec|
|
|
34
34
|
# spec.add_development_dependency "jsonschema", "~> 2.0.2"
|
35
35
|
# spec.add_development_dependency "rj_schema", "~> 0.2.0"
|
36
36
|
|
37
|
-
spec.add_runtime_dependency "ecma-re-validator", "~> 0.
|
37
|
+
spec.add_runtime_dependency "ecma-re-validator", "~> 0.3"
|
38
38
|
spec.add_runtime_dependency "hana", "~> 1.3"
|
39
39
|
spec.add_runtime_dependency "uri_template", "~> 0.7"
|
40
|
-
spec.add_runtime_dependency "regexp_parser", "~>
|
40
|
+
spec.add_runtime_dependency "regexp_parser", "~> 2.0"
|
41
41
|
end
|
data/lib/json_schemer/format.rb
CHANGED
@@ -3,7 +3,7 @@ module JSONSchemer
|
|
3
3
|
module Format
|
4
4
|
# this is no good
|
5
5
|
EMAIL_REGEX = /\A[^@\s]+@([\p{L}\d-]+\.)+[\p{L}\d\-]{2,}\z/i.freeze
|
6
|
-
LABEL_REGEX_STRING = '\p{L}([\p{L}\p{N}\-]*[\p{L}\p{N}])?'
|
6
|
+
LABEL_REGEX_STRING = '[\p{L}\p{N}]([\p{L}\p{N}\-]*[\p{L}\p{N}])?'
|
7
7
|
HOSTNAME_REGEX = /\A(#{LABEL_REGEX_STRING}\.)*#{LABEL_REGEX_STRING}\z/i.freeze
|
8
8
|
JSON_POINTER_REGEX_STRING = '(\/([^~\/]|~[01])*)*'
|
9
9
|
JSON_POINTER_REGEX = /\A#{JSON_POINTER_REGEX_STRING}\z/.freeze
|
@@ -101,7 +101,14 @@ module JSONSchemer
|
|
101
101
|
end
|
102
102
|
|
103
103
|
def iri_escape(data)
|
104
|
-
|
104
|
+
data.gsub(/[^[:ascii:]]/) do |match|
|
105
|
+
us = match
|
106
|
+
tmp = +''
|
107
|
+
us.each_byte do |uc|
|
108
|
+
tmp << sprintf('%%%02X', uc)
|
109
|
+
end
|
110
|
+
tmp
|
111
|
+
end.force_encoding(Encoding::US_ASCII)
|
105
112
|
end
|
106
113
|
|
107
114
|
def valid_uri_template?(data)
|
@@ -4,16 +4,17 @@ module JSONSchemer
|
|
4
4
|
class Base
|
5
5
|
include Format
|
6
6
|
|
7
|
-
Instance = Struct.new(:data, :data_pointer, :schema, :schema_pointer, :parent_uri, :
|
7
|
+
Instance = Struct.new(:data, :data_pointer, :schema, :schema_pointer, :parent_uri, :before_property_validation, :after_property_validation) do
|
8
8
|
def merge(
|
9
9
|
data: self.data,
|
10
10
|
data_pointer: self.data_pointer,
|
11
11
|
schema: self.schema,
|
12
12
|
schema_pointer: self.schema_pointer,
|
13
13
|
parent_uri: self.parent_uri,
|
14
|
-
|
14
|
+
before_property_validation: self.before_property_validation,
|
15
|
+
after_property_validation: self.after_property_validation
|
15
16
|
)
|
16
|
-
self.class.new(data, data_pointer, schema, schema_pointer, parent_uri,
|
17
|
+
self.class.new(data, data_pointer, schema, schema_pointer, parent_uri, before_property_validation, after_property_validation)
|
17
18
|
end
|
18
19
|
end
|
19
20
|
|
@@ -29,10 +30,18 @@ module JSONSchemer
|
|
29
30
|
:eol => '\z'
|
30
31
|
}.freeze
|
31
32
|
|
33
|
+
INSERT_DEFAULT_PROPERTY = proc do |data, property, property_schema, _parent|
|
34
|
+
if !data.key?(property) && property_schema.is_a?(Hash) && property_schema.key?('default')
|
35
|
+
data[property] = property_schema.fetch('default').clone
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
32
39
|
def initialize(
|
33
40
|
schema,
|
34
41
|
format: true,
|
35
42
|
insert_property_defaults: false,
|
43
|
+
before_property_validation: nil,
|
44
|
+
after_property_validation: nil,
|
36
45
|
formats: nil,
|
37
46
|
keywords: nil,
|
38
47
|
ref_resolver: DEFAULT_REF_RESOLVER
|
@@ -40,18 +49,20 @@ module JSONSchemer
|
|
40
49
|
raise InvalidSymbolKey, 'schemas must use string keys' if schema.is_a?(Hash) && !schema.empty? && !schema.first.first.is_a?(String)
|
41
50
|
@root = schema
|
42
51
|
@format = format
|
43
|
-
@
|
52
|
+
@before_property_validation = [*before_property_validation]
|
53
|
+
@before_property_validation.unshift(INSERT_DEFAULT_PROPERTY) if insert_property_defaults
|
54
|
+
@after_property_validation = [*after_property_validation]
|
44
55
|
@formats = formats
|
45
56
|
@keywords = keywords
|
46
57
|
@ref_resolver = ref_resolver == 'net/http' ? CachedRefResolver.new(&NET_HTTP_REF_RESOLVER) : ref_resolver
|
47
58
|
end
|
48
59
|
|
49
60
|
def valid?(data)
|
50
|
-
valid_instance?(Instance.new(data, '', root, '', nil,
|
61
|
+
valid_instance?(Instance.new(data, '', root, '', nil, @before_property_validation, @after_property_validation))
|
51
62
|
end
|
52
63
|
|
53
64
|
def validate(data)
|
54
|
-
validate_instance(Instance.new(data, '', root, '', nil,
|
65
|
+
validate_instance(Instance.new(data, '', root, '', nil, @before_property_validation, @after_property_validation))
|
55
66
|
end
|
56
67
|
|
57
68
|
protected
|
@@ -101,7 +112,7 @@ module JSONSchemer
|
|
101
112
|
if keywords
|
102
113
|
keywords.each do |keyword, callable|
|
103
114
|
if schema.key?(keyword)
|
104
|
-
result = callable.call(data, schema, instance.
|
115
|
+
result = callable.call(data, schema, instance.data_pointer)
|
105
116
|
if result.is_a?(Array)
|
106
117
|
result.each(&block)
|
107
118
|
elsif !result
|
@@ -119,7 +130,8 @@ module JSONSchemer
|
|
119
130
|
subinstance = instance.merge(
|
120
131
|
schema: subschema,
|
121
132
|
schema_pointer: "#{instance.schema_pointer}/allOf/#{index}",
|
122
|
-
|
133
|
+
before_property_validation: false,
|
134
|
+
after_property_validation: false
|
123
135
|
)
|
124
136
|
validate_instance(subinstance, &block)
|
125
137
|
end
|
@@ -130,7 +142,8 @@ module JSONSchemer
|
|
130
142
|
subinstance = instance.merge(
|
131
143
|
schema: subschema,
|
132
144
|
schema_pointer: "#{instance.schema_pointer}/anyOf/#{index}",
|
133
|
-
|
145
|
+
before_property_validation: false,
|
146
|
+
after_property_validation: false
|
134
147
|
)
|
135
148
|
validate_instance(subinstance)
|
136
149
|
end
|
@@ -142,7 +155,8 @@ module JSONSchemer
|
|
142
155
|
subinstance = instance.merge(
|
143
156
|
schema: subschema,
|
144
157
|
schema_pointer: "#{instance.schema_pointer}/oneOf/#{index}",
|
145
|
-
|
158
|
+
before_property_validation: false,
|
159
|
+
after_property_validation: false
|
146
160
|
)
|
147
161
|
validate_instance(subinstance)
|
148
162
|
end
|
@@ -158,12 +172,13 @@ module JSONSchemer
|
|
158
172
|
subinstance = instance.merge(
|
159
173
|
schema: not_schema,
|
160
174
|
schema_pointer: "#{instance.schema_pointer}/not",
|
161
|
-
|
175
|
+
before_property_validation: false,
|
176
|
+
after_property_validation: false
|
162
177
|
)
|
163
178
|
yield error(subinstance, 'not') if valid_instance?(subinstance)
|
164
179
|
end
|
165
180
|
|
166
|
-
if if_schema && valid_instance?(instance.merge(schema: if_schema,
|
181
|
+
if if_schema && valid_instance?(instance.merge(schema: if_schema, before_property_validation: false, after_property_validation: false))
|
167
182
|
validate_instance(instance.merge(schema: then_schema, schema_pointer: "#{instance.schema_pointer}/then"), &block) unless then_schema.nil?
|
168
183
|
elsif if_schema
|
169
184
|
validate_instance(instance.merge(schema: else_schema, schema_pointer: "#{instance.schema_pointer}/else"), &block) unless else_schema.nil?
|
@@ -487,10 +502,10 @@ module JSONSchemer
|
|
487
502
|
dependencies = schema['dependencies']
|
488
503
|
property_names = schema['propertyNames']
|
489
504
|
|
490
|
-
if instance.
|
505
|
+
if instance.before_property_validation && properties
|
491
506
|
properties.each do |property, property_schema|
|
492
|
-
|
493
|
-
data
|
507
|
+
instance.before_property_validation.each do |hook|
|
508
|
+
hook.call(data, property, property_schema, schema)
|
494
509
|
end
|
495
510
|
end
|
496
511
|
end
|
@@ -565,15 +580,21 @@ module JSONSchemer
|
|
565
580
|
validate_instance(subinstance, &block)
|
566
581
|
end
|
567
582
|
end
|
583
|
+
|
584
|
+
if instance.after_property_validation && properties
|
585
|
+
properties.each do |property, property_schema|
|
586
|
+
instance.after_property_validation.each do |hook|
|
587
|
+
hook.call(data, property, property_schema, schema)
|
588
|
+
end
|
589
|
+
end
|
590
|
+
end
|
568
591
|
end
|
569
592
|
|
570
593
|
def safe_strict_decode64(data)
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
nil
|
576
|
-
end
|
594
|
+
Base64.strict_decode64(data)
|
595
|
+
rescue ArgumentError => e
|
596
|
+
raise e unless e.message == 'invalid base64'
|
597
|
+
nil
|
577
598
|
end
|
578
599
|
|
579
600
|
def ecma_262_regex(pattern)
|
@@ -615,16 +636,15 @@ module JSONSchemer
|
|
615
636
|
if schema.is_a?(Array)
|
616
637
|
schema.each_with_index { |subschema, index| resolve_ids(subschema, ids, parent_uri, "#{pointer}/#{index}") }
|
617
638
|
elsif schema.is_a?(Hash)
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
definitions.each { |key, subschema| resolve_ids(subschema, ids, uri, "#{pointer}/definitions/#{key}") }
|
639
|
+
uri = join_uri(parent_uri, schema[id_keyword])
|
640
|
+
schema.each do |key, value|
|
641
|
+
if key == id_keyword && uri != parent_uri
|
642
|
+
ids[uri.to_s] = {
|
643
|
+
schema: schema,
|
644
|
+
pointer: pointer
|
645
|
+
}
|
646
|
+
end
|
647
|
+
resolve_ids(value, ids, uri, "#{pointer}/#{key}")
|
628
648
|
end
|
629
649
|
end
|
630
650
|
ids
|
data/lib/json_schemer/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_schemer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.17
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Harsha
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-12-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -58,14 +58,14 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0.
|
61
|
+
version: '0.3'
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '0.
|
68
|
+
version: '0.3'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: hana
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -100,14 +100,14 @@ dependencies:
|
|
100
100
|
requirements:
|
101
101
|
- - "~>"
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: '
|
103
|
+
version: '2.0'
|
104
104
|
type: :runtime
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
108
|
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version: '
|
110
|
+
version: '2.0'
|
111
111
|
description:
|
112
112
|
email:
|
113
113
|
- davishmcclurg@gmail.com
|
@@ -144,17 +144,16 @@ require_paths:
|
|
144
144
|
- lib
|
145
145
|
required_ruby_version: !ruby/object:Gem::Requirement
|
146
146
|
requirements:
|
147
|
-
- - "
|
147
|
+
- - ">="
|
148
148
|
- !ruby/object:Gem::Version
|
149
|
-
version: '2.
|
149
|
+
version: '2.4'
|
150
150
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
151
151
|
requirements:
|
152
152
|
- - ">="
|
153
153
|
- !ruby/object:Gem::Version
|
154
154
|
version: '0'
|
155
155
|
requirements: []
|
156
|
-
|
157
|
-
rubygems_version: 2.7.6
|
156
|
+
rubygems_version: 3.1.2
|
158
157
|
signing_key:
|
159
158
|
specification_version: 4
|
160
159
|
summary: JSON Schema validator. Supports drafts 4, 6, and 7.
|