json_schemer 0.2.21 → 0.2.22
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +8 -1
- data/lib/json_schemer/cached_resolver.rb +16 -0
- data/lib/json_schemer/schema/base.rb +28 -15
- data/lib/json_schemer/version.rb +1 -1
- data/lib/json_schemer.rb +3 -2
- metadata +3 -3
- data/lib/json_schemer/cached_ref_resolver.rb +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc9ff5876e71054ee601dc4464c99560e86557796ef792e36512e1f08725a065
|
4
|
+
data.tar.gz: 5ee26787348f3aecb1a591c38c7bccc7aaf4166fd14d1299999d426ecaa15893
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b5dc77e4a136d9dd7ab774e530880188c3125647535707d67b9b09e615e538bfe826249fd53644d3dc452e8387667bc74a3e24ec21f847145bc4b92de15371d
|
7
|
+
data.tar.gz: 160ead197e1f0a61090e936a526e4753156b185e4049db6d5ca7efb9c8bbf4430ebd9a0eff952299fc5bdb0b93a063745d9be3e919bee80ce93c05864a9655b7
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -110,7 +110,14 @@ JSONSchemer.schema(
|
|
110
110
|
# 'net/http'/proc/lambda/respond_to?(:call)
|
111
111
|
# 'net/http': proc { |uri| JSON.parse(Net::HTTP.get(uri)) }
|
112
112
|
# default: proc { |uri| raise UnknownRef, uri.to_s }
|
113
|
-
ref_resolver: 'net/http'
|
113
|
+
ref_resolver: 'net/http',
|
114
|
+
|
115
|
+
# use different method to match regexes
|
116
|
+
# 'ecma'/'ruby'/proc/lambda/respond_to?(:call)
|
117
|
+
# default: 'ecma'
|
118
|
+
regexp_resolver: proc do |pattern|
|
119
|
+
RE2::Regexp.new(pattern)
|
120
|
+
end
|
114
121
|
)
|
115
122
|
```
|
116
123
|
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module JSONSchemer
|
3
|
+
class CachedResolver
|
4
|
+
def initialize(&resolver)
|
5
|
+
@resolver = resolver
|
6
|
+
@cache = {}
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(*args)
|
10
|
+
@cache[args] = @resolver.call(*args) unless @cache.key?(args)
|
11
|
+
@cache[args]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class CachedRefResolver < CachedResolver; end
|
16
|
+
end
|
@@ -30,6 +30,14 @@ module JSONSchemer
|
|
30
30
|
:eol => '\z'
|
31
31
|
}.freeze
|
32
32
|
|
33
|
+
ECMA_262_REGEXP_RESOLVER = proc do |pattern|
|
34
|
+
Regexp.new(
|
35
|
+
Regexp::Scanner.scan(pattern).map do |type, token, text|
|
36
|
+
type == :anchor ? RUBY_REGEX_ANCHORS_TO_ECMA_262.fetch(token, text) : text
|
37
|
+
end.join
|
38
|
+
)
|
39
|
+
end
|
40
|
+
|
33
41
|
INSERT_DEFAULT_PROPERTY = proc do |data, property, property_schema, _parent|
|
34
42
|
if !data.key?(property) && property_schema.is_a?(Hash) && property_schema.key?('default')
|
35
43
|
data[property] = property_schema.fetch('default').clone
|
@@ -44,7 +52,8 @@ module JSONSchemer
|
|
44
52
|
after_property_validation: nil,
|
45
53
|
formats: nil,
|
46
54
|
keywords: nil,
|
47
|
-
ref_resolver: DEFAULT_REF_RESOLVER
|
55
|
+
ref_resolver: DEFAULT_REF_RESOLVER,
|
56
|
+
regexp_resolver: 'ecma'
|
48
57
|
)
|
49
58
|
raise InvalidSymbolKey, 'schemas must use string keys' if schema.is_a?(Hash) && !schema.empty? && !schema.first.first.is_a?(String)
|
50
59
|
@root = schema
|
@@ -54,7 +63,15 @@ module JSONSchemer
|
|
54
63
|
@after_property_validation = [*after_property_validation]
|
55
64
|
@formats = formats
|
56
65
|
@keywords = keywords
|
57
|
-
@ref_resolver = ref_resolver == 'net/http' ?
|
66
|
+
@ref_resolver = ref_resolver == 'net/http' ? CachedResolver.new(&NET_HTTP_REF_RESOLVER) : ref_resolver
|
67
|
+
@regexp_resolver = case regexp_resolver
|
68
|
+
when 'ecma'
|
69
|
+
CachedResolver.new(&ECMA_262_REGEXP_RESOLVER)
|
70
|
+
when 'ruby'
|
71
|
+
CachedResolver.new(&Regexp.method(:new))
|
72
|
+
else
|
73
|
+
regexp_resolver
|
74
|
+
end
|
58
75
|
end
|
59
76
|
|
60
77
|
def valid?(data)
|
@@ -204,7 +221,7 @@ module JSONSchemer
|
|
204
221
|
|
205
222
|
private
|
206
223
|
|
207
|
-
attr_reader :root, :formats, :keywords, :ref_resolver
|
224
|
+
attr_reader :root, :formats, :keywords, :ref_resolver, :regexp_resolver
|
208
225
|
|
209
226
|
def id_keyword
|
210
227
|
ID_KEYWORD
|
@@ -228,7 +245,8 @@ module JSONSchemer
|
|
228
245
|
format: format?,
|
229
246
|
formats: formats,
|
230
247
|
keywords: keywords,
|
231
|
-
ref_resolver: ref_resolver
|
248
|
+
ref_resolver: ref_resolver,
|
249
|
+
regexp_resolver: regexp_resolver
|
232
250
|
)
|
233
251
|
end
|
234
252
|
|
@@ -398,7 +416,7 @@ module JSONSchemer
|
|
398
416
|
|
399
417
|
yield error(instance, 'maxLength') if max_length && data.size > max_length
|
400
418
|
yield error(instance, 'minLength') if min_length && data.size < min_length
|
401
|
-
yield error(instance, 'pattern') if pattern &&
|
419
|
+
yield error(instance, 'pattern') if pattern && resolve_regexp(pattern) !~ data
|
402
420
|
yield error(instance, 'format') if format? && spec_format?(format) && !valid_spec_format?(data, format)
|
403
421
|
|
404
422
|
if content_encoding || content_media_type
|
@@ -551,7 +569,7 @@ module JSONSchemer
|
|
551
569
|
|
552
570
|
if pattern_properties
|
553
571
|
regex_pattern_properties ||= pattern_properties.map do |pattern, property_schema|
|
554
|
-
[pattern,
|
572
|
+
[pattern, resolve_regexp(pattern), property_schema]
|
555
573
|
end
|
556
574
|
regex_pattern_properties.each do |pattern, regex, property_schema|
|
557
575
|
if regex.match?(key)
|
@@ -596,15 +614,6 @@ module JSONSchemer
|
|
596
614
|
nil
|
597
615
|
end
|
598
616
|
|
599
|
-
def ecma_262_regex(pattern)
|
600
|
-
@ecma_262_regex ||= {}
|
601
|
-
@ecma_262_regex[pattern] ||= Regexp.new(
|
602
|
-
Regexp::Scanner.scan(pattern).map do |type, token, text|
|
603
|
-
type == :anchor ? RUBY_REGEX_ANCHORS_TO_ECMA_262.fetch(token, text) : text
|
604
|
-
end.join
|
605
|
-
)
|
606
|
-
end
|
607
|
-
|
608
617
|
def join_uri(a, b)
|
609
618
|
b = URI.parse(b) if b
|
610
619
|
if a && b && a.relative? && b.relative?
|
@@ -652,6 +661,10 @@ module JSONSchemer
|
|
652
661
|
def resolve_ref(uri)
|
653
662
|
ref_resolver.call(uri) || raise(InvalidRefResolution, uri.to_s)
|
654
663
|
end
|
664
|
+
|
665
|
+
def resolve_regexp(pattern)
|
666
|
+
regexp_resolver.call(pattern) || raise(InvalidRegexpResolution, pattern)
|
667
|
+
end
|
655
668
|
end
|
656
669
|
end
|
657
670
|
end
|
data/lib/json_schemer/version.rb
CHANGED
data/lib/json_schemer.rb
CHANGED
@@ -17,7 +17,7 @@ require 'uri_template'
|
|
17
17
|
require 'json_schemer/version'
|
18
18
|
require 'json_schemer/format'
|
19
19
|
require 'json_schemer/errors'
|
20
|
-
require 'json_schemer/
|
20
|
+
require 'json_schemer/cached_resolver'
|
21
21
|
require 'json_schemer/schema/base'
|
22
22
|
require 'json_schemer/schema/draft4'
|
23
23
|
require 'json_schemer/schema/draft6'
|
@@ -27,6 +27,7 @@ module JSONSchemer
|
|
27
27
|
class UnsupportedMetaSchema < StandardError; end
|
28
28
|
class UnknownRef < StandardError; end
|
29
29
|
class InvalidRefResolution < StandardError; end
|
30
|
+
class InvalidRegexpResolution < StandardError; end
|
30
31
|
class InvalidFileURI < StandardError; end
|
31
32
|
class InvalidSymbolKey < StandardError; end
|
32
33
|
|
@@ -59,7 +60,7 @@ module JSONSchemer
|
|
59
60
|
if options.key?(:ref_resolver)
|
60
61
|
schema = FILE_URI_REF_RESOLVER.call(uri)
|
61
62
|
else
|
62
|
-
ref_resolver =
|
63
|
+
ref_resolver = CachedResolver.new(&FILE_URI_REF_RESOLVER)
|
63
64
|
schema = ref_resolver.call(uri)
|
64
65
|
options[:ref_resolver] = ref_resolver
|
65
66
|
end
|
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.22
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Harsha
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-10-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -129,7 +129,7 @@ files:
|
|
129
129
|
- exe/json_schemer
|
130
130
|
- json_schemer.gemspec
|
131
131
|
- lib/json_schemer.rb
|
132
|
-
- lib/json_schemer/
|
132
|
+
- lib/json_schemer/cached_resolver.rb
|
133
133
|
- lib/json_schemer/errors.rb
|
134
134
|
- lib/json_schemer/format.rb
|
135
135
|
- lib/json_schemer/schema/base.rb
|
@@ -1,14 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
module JSONSchemer
|
3
|
-
class CachedRefResolver
|
4
|
-
def initialize(&ref_resolver)
|
5
|
-
@ref_resolver = ref_resolver
|
6
|
-
@cache = {}
|
7
|
-
end
|
8
|
-
|
9
|
-
def call(uri)
|
10
|
-
@cache[uri] = @ref_resolver.call(uri) unless @cache.key?(uri)
|
11
|
-
@cache[uri]
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|