json_schemer 0.2.11 → 0.2.16

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: 45e4c874bfbfd416a38a47a23cf9b5c561df5da3457b11b7cb6acc0f36a8f5d6
4
- data.tar.gz: a62663e1c3f250af1b57418d666c4bbe9229212c00845dc3b5f6c34d7ab3d57c
3
+ metadata.gz: 0cc2fef0eeaaab0e1664a9fa5aa39c08ef2e1fbd8fb68d202bf1b50b7c0840ee
4
+ data.tar.gz: 438c507a7d8ffa674d34d22c647af0852eb031e02c491d274cc5d4dec5c185dd
5
5
  SHA512:
6
- metadata.gz: ff9d2a99b3f964b71d1a98a9037d5465627b913616a6c0c6160d1fe71aac784891585b92339773e677fdc9637bed0e120935ad7f4c4f283dada6f96a219b2591
7
- data.tar.gz: 78d254d698f87a46a1ba1737775178310d2cb0e9d25f095f2da7dede4bcfee58c62dcbd89899370c158638734041b59fe5f351156a713002ceeeca8552849638
6
+ metadata.gz: 84ba1ea8a43459ff6c5c835fd5f0aedaf00f8077ecd3ec0aefe88322f09ef868712702122790a56f30c33bb1ab3f2585bd7e9a57c94eb07f553d845322095ae8
7
+ data.tar.gz: 6709666d3f2cf98ba1b2f06712e90f564d33d6e861b15d9ff2cb3c109d9b3ca0816b456f22b837a18062824429c97b8d7ebeb9fbaaa75b67bb203dec8f56b310
@@ -4,19 +4,13 @@ jobs:
4
4
  ruby:
5
5
  strategy:
6
6
  matrix:
7
- ruby: [2.4.x, 2.5.x, 2.6.x]
7
+ ruby: [2.4, 2.5, 2.6, 2.7, truffleruby-head]
8
8
  runs-on: ubuntu-latest
9
9
  steps:
10
- - uses: actions/checkout@v1
11
- - uses: actions/setup-ruby@v1
10
+ - uses: actions/checkout@v2
11
+ - uses: ruby/setup-ruby@v1
12
12
  with:
13
13
  ruby-version: ${{ matrix.ruby }}
14
- - uses: actions/setup-python@v1
15
- with:
16
- python-version: 3.x
17
14
  - run: |
18
- pip install --user Flask
19
- ./JSON-Schema-Test-Suite/bin/jsonschema_suite serve &
20
- gem install bundler
21
15
  bundle install
22
16
  bundle exec rake test
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json_schemer (0.2.11)
4
+ json_schemer (0.2.16)
5
5
  ecma-re-validator (~> 0.2)
6
6
  hana (~> 1.3)
7
7
  regexp_parser (~> 1.5)
@@ -10,12 +10,12 @@ PATH
10
10
  GEM
11
11
  remote: https://rubygems.org/
12
12
  specs:
13
- ecma-re-validator (0.2.0)
13
+ ecma-re-validator (0.2.1)
14
14
  regexp_parser (~> 1.2)
15
- hana (1.3.5)
15
+ hana (1.3.6)
16
16
  minitest (5.11.3)
17
17
  rake (13.0.1)
18
- regexp_parser (1.7.0)
18
+ regexp_parser (1.8.1)
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)) }
@@ -14,6 +14,7 @@ require 'uri_template'
14
14
 
15
15
  require 'json_schemer/version'
16
16
  require 'json_schemer/format'
17
+ require 'json_schemer/errors'
17
18
  require 'json_schemer/cached_ref_resolver'
18
19
  require 'json_schemer/schema/base'
19
20
  require 'json_schemer/schema/draft4'
@@ -0,0 +1,31 @@
1
+ # Based on code from @robacarp found in issue 48:
2
+ # https://github.com/davishmcclurg/json_schemer/issues/48
3
+ #
4
+ module JSONSchemer
5
+ module Errors
6
+ class << self
7
+ def pretty(error)
8
+ data_pointer, type, schema = error.values_at('data_pointer', 'type', 'schema')
9
+ location = data_pointer.empty? ? 'root' : "property '#{data_pointer}'"
10
+
11
+ case type
12
+ when 'required'
13
+ keys = error.fetch('details').fetch('missing_keys').join(', ')
14
+ "#{location} is missing required keys: #{keys}"
15
+ when 'null', 'string', 'boolean', 'integer', 'number', 'array', 'object'
16
+ "#{location} is not of type: #{type}"
17
+ when 'pattern'
18
+ "#{location} does not match pattern: #{schema.fetch('pattern')}"
19
+ when 'format'
20
+ "#{location} does not match format: #{schema.fetch('format')}"
21
+ when 'const'
22
+ "#{location} is not: #{schema.fetch('const').inspect}"
23
+ when 'enum'
24
+ "#{location} is not one of: #{schema.fetch('enum')}"
25
+ else
26
+ "#{location} is invalid: error_type=#{type}"
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -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
@@ -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, :insert_property_defaults) do
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
- insert_property_defaults: self.insert_property_defaults
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, insert_property_defaults)
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
- @insert_property_defaults = insert_property_defaults
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, !!@insert_property_defaults))
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, !!@insert_property_defaults))
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.pointer)
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
- insert_property_defaults: false
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
- insert_property_defaults: false
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
- insert_property_defaults: false
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
- insert_property_defaults: false
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, insert_property_defaults: false))
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.insert_property_defaults && properties
505
+ if instance.before_property_validation && properties
491
506
  properties.each do |property, property_schema|
492
- if !data.key?(property) && property_schema.is_a?(Hash) && property_schema.key?('default')
493
- data[property] = property_schema.fetch('default').clone
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
- begin
572
- Base64.strict_decode64(data)
573
- rescue ArgumentError => e
574
- raise e unless e.message == 'invalid base64'
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
- id = schema[id_keyword]
619
- uri = join_uri(parent_uri, id)
620
- unless uri == parent_uri
621
- ids[uri.to_s] = {
622
- schema: schema,
623
- pointer: pointer
624
- }
625
- end
626
- if definitions = schema['definitions']
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
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module JSONSchemer
3
- VERSION = '0.2.11'
3
+ VERSION = '0.2.16'
4
4
  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.11
4
+ version: 0.2.16
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-03-14 00:00:00.000000000 Z
11
+ date: 2020-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -127,6 +127,7 @@ files:
127
127
  - json_schemer.gemspec
128
128
  - lib/json_schemer.rb
129
129
  - lib/json_schemer/cached_ref_resolver.rb
130
+ - lib/json_schemer/errors.rb
130
131
  - lib/json_schemer/format.rb
131
132
  - lib/json_schemer/schema/base.rb
132
133
  - lib/json_schemer/schema/draft4.rb
@@ -152,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
153
  - !ruby/object:Gem::Version
153
154
  version: '0'
154
155
  requirements: []
155
- rubygems_version: 3.0.3
156
+ rubygems_version: 3.1.2
156
157
  signing_key:
157
158
  specification_version: 4
158
159
  summary: JSON Schema validator. Supports drafts 4, 6, and 7.