json_schemer 0.2.21 → 0.2.23

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ed760df49bc348a5011d6baf6416462f0069b31a8c1ef99550913d30a8a8a41e
4
- data.tar.gz: c271118e1a2f8c26de0aace952e10340642536b44cd3b88f91ea22aa11c9bd0a
3
+ metadata.gz: 1c55d246b024978c9c972889597f5224da512f7452e67369e9164264be0c809e
4
+ data.tar.gz: e08104af62466e8c05c03091b1c14f01d28ea8be255a363ae13c88dbe450423c
5
5
  SHA512:
6
- metadata.gz: 737e7a7888a61e8f2807e0307c8a96653943a7b88d27bb2fa4666a806f514f89274643b96a0dd48eb4eb940cfd0fd76d9254827360f2a53808263b66e25fc932
7
- data.tar.gz: 522393b56be1b0ad983785b2b4ffe2268b11649336a151934260800b099f4160850c85dec19a44ace008cfbf5c6a208f7c53b8e04bf939ab86c8bdd75d6b873e
6
+ metadata.gz: be1e817cf98bcdcfe89c7e798475e23cb6961e0523691e950a1c3ed8b3e34357d949bc63b7096ed8cb29c966b0bd16b9aa29ef674bd26e6b302a849a09aaa7ee
7
+ data.tar.gz: 590e048ad223135c9634a3e12036cbc83f2a6773143df22f4c7f56e19f228fd56e4425a07302c0d30e4c0aa6e6a94fd255ec15ebd0e59d8a568922110a9b9d55
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json_schemer (0.2.21)
4
+ json_schemer (0.2.23)
5
5
  ecma-re-validator (~> 0.3)
6
6
  hana (~> 1.3)
7
7
  regexp_parser (~> 2.0)
@@ -15,7 +15,7 @@ GEM
15
15
  hana (1.3.7)
16
16
  minitest (5.15.0)
17
17
  rake (13.0.6)
18
- regexp_parser (2.3.1)
18
+ regexp_parser (2.6.0)
19
19
  uri_template (0.7.0)
20
20
 
21
21
  PLATFORMS
@@ -28,4 +28,4 @@ DEPENDENCIES
28
28
  rake (~> 13.0)
29
29
 
30
30
  BUNDLED WITH
31
- 2.3.11
31
+ 2.3.25
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' ? CachedRefResolver.new(&NET_HTTP_REF_RESOLVER) : ref_resolver
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 && ecma_262_regex(pattern) !~ data
419
+ yield error(instance, 'pattern') if pattern && !resolve_regexp(pattern).match?(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, ecma_262_regex(pattern), property_schema]
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
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module JSONSchemer
3
- VERSION = '0.2.21'
3
+ VERSION = '0.2.23'
4
4
  end
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/cached_ref_resolver'
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 = CachedRefResolver.new(&FILE_URI_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.21
4
+ version: 0.2.23
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-05-05 00:00:00.000000000 Z
11
+ date: 2022-11-03 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/cached_ref_resolver.rb
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