json_schemer 0.2.2 → 0.2.3

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: 73e909cd3b478fdfd80f7b4cf6f662e2b2089f9e4d36e53545250e08d6108ded
4
- data.tar.gz: ce95d5099ca09f52616555570b014c4d4501f4ef3742fb70c06f61846d061aeb
3
+ metadata.gz: ad4883bcdfb3cac627d3b20c3ab74c0ebf4a3b3ce9f95b59cc653b60f2771580
4
+ data.tar.gz: bbb0a784d78974b3a812becdb354222e145a642ef9162c6e8bd6d74578994024
5
5
  SHA512:
6
- metadata.gz: 5b6e683f487d15ac6c88bed99e18ef73e949816a980abfe2edf066e2f92831e08d8245c9685104fe6df2d51e00de9b1670909681f1785704faf8adc17ec3ca26
7
- data.tar.gz: dc9d610aade61f19a6f168d64945f0c90021548f9ead54bd8a76d9dfae2f0d2aa35c3e534fdd724b087faf8ed318704654dda63eedc531a37be0669e1f46c9e8
6
+ metadata.gz: cf54450f55764c1c50751173db9008babda7befde4012b1e434ed7087134e2e5f053cbc35977cf58b96a8a4b7176302ed03731bddeb192e8aca3f29e79622eff
7
+ data.tar.gz: 464a4611a5261cba8e5a4c1acac299eb51342b1c06da90950e9f6aa826dabf4256feed1959247262310b66c061776a4f1f262bdc0028e29205097efec328b3c9
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json_schemer (0.2.2)
4
+ json_schemer (0.2.3)
5
5
  ecma-re-validator (~> 0.2)
6
6
  hana (~> 1.3)
7
7
  regexp_parser (~> 1.5)
@@ -22,10 +22,10 @@ PLATFORMS
22
22
  ruby
23
23
 
24
24
  DEPENDENCIES
25
- bundler (~> 1.16)
25
+ bundler (~> 2.0)
26
26
  json_schemer!
27
27
  minitest (~> 5.0)
28
28
  rake (~> 10.0)
29
29
 
30
30
  BUNDLED WITH
31
- 1.17.2
31
+ 2.0.2
data/README.md CHANGED
@@ -47,6 +47,22 @@ schemer.valid?({ 'abc' => 10 })
47
47
  schemer.validate({ 'abc' => 10 }).to_a
48
48
  # => [{"data"=>10, "schema"=>{"type"=>"integer", "minimum"=>11}, "pointer"=>"#/abc", "type"=>"minimum"}]
49
49
 
50
+ # default property values
51
+
52
+ data = {}
53
+ JSONSchemer.schema(
54
+ {
55
+ 'properties' => {
56
+ 'foo' => {
57
+ 'default' => 'bar'
58
+ }
59
+ }
60
+ },
61
+ insert_property_defaults: true
62
+ ).valid?(data)
63
+ data
64
+ # => {"foo"=>"bar"}
65
+
50
66
  # schema files
51
67
 
52
68
  require 'pathname'
@@ -71,6 +87,11 @@ JSONSchemer.schema(
71
87
  # default: true
72
88
  format: true,
73
89
 
90
+ # insert default property values during validation
91
+ # true/false
92
+ # default: false
93
+ insert_property_defaults: true,
94
+
74
95
  # resolve external references
75
96
  # 'net/http'/proc/lambda/respond_to?(:call)
76
97
  # 'net/http': proc { |uri| JSON.parse(Net::HTTP.get(uri)) }
@@ -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.add_development_dependency "bundler", "~> 1.16"
23
+ spec.add_development_dependency "bundler", "~> 2.0"
24
24
  spec.add_development_dependency "rake", "~> 10.0"
25
25
  spec.add_development_dependency "minitest", "~> 5.0"
26
26
 
@@ -31,6 +31,7 @@ module JSONSchemer
31
31
  def initialize(
32
32
  schema,
33
33
  format: true,
34
+ insert_property_defaults: false,
34
35
  formats: nil,
35
36
  keywords: nil,
36
37
  ref_resolver: DEFAULT_REF_RESOLVER
@@ -38,6 +39,7 @@ module JSONSchemer
38
39
  raise InvalidSymbolKey, 'schemas must use string keys' if schema.is_a?(Hash) && !schema.first.first.is_a?(String)
39
40
  @root = schema
40
41
  @format = format
42
+ @insert_property_defaults = insert_property_defaults
41
43
  @formats = formats
42
44
  @keywords = keywords
43
45
  @ref_resolver = ref_resolver == 'net/http' ? CachedRefResolver.new(&NET_HTTP_REF_RESOLVER) : ref_resolver
@@ -93,6 +95,8 @@ module JSONSchemer
93
95
  validate_custom_format(instance, formats.fetch(format), &block)
94
96
  end
95
97
 
98
+ data = instance.data
99
+
96
100
  if keywords
97
101
  keywords.each do |keyword, callable|
98
102
  if schema.key?(keyword)
@@ -106,8 +110,6 @@ module JSONSchemer
106
110
  end
107
111
  end
108
112
 
109
- data = instance.data
110
-
111
113
  yield error(instance, 'enum') if enum && !enum.include?(data)
112
114
  yield error(instance, 'const') if schema.key?('const') && schema['const'] != data
113
115
 
@@ -177,6 +179,10 @@ module JSONSchemer
177
179
  !!@format
178
180
  end
179
181
 
182
+ def insert_property_defaults?
183
+ !!@insert_property_defaults
184
+ end
185
+
180
186
  def custom_format?(format)
181
187
  !!(formats && formats.key?(format))
182
188
  end
@@ -189,6 +195,7 @@ module JSONSchemer
189
195
  JSONSchemer.schema(
190
196
  schema,
191
197
  format: format?,
198
+ insert_property_defaults: insert_property_defaults?,
192
199
  formats: formats,
193
200
  keywords: keywords,
194
201
  ref_resolver: ref_resolver
@@ -460,6 +467,14 @@ module JSONSchemer
460
467
  dependencies = schema['dependencies']
461
468
  property_names = schema['propertyNames']
462
469
 
470
+ if insert_property_defaults?
471
+ properties.each do |property, property_schema|
472
+ if !data.key?(property) && property_schema.key?('default')
473
+ data[property] = property_schema.fetch('default').clone
474
+ end
475
+ end
476
+ end
477
+
463
478
  if dependencies
464
479
  dependencies.each do |key, value|
465
480
  next unless data.key?(key)
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module JSONSchemer
3
- VERSION = '0.2.2'
3
+ VERSION = '0.2.3'
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.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Harsha
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-10 00:00:00.000000000 Z
11
+ date: 2019-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.16'
19
+ version: '2.0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.16'
26
+ version: '2.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement