json_schemer 0.1.2 → 0.1.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: a8a7a9d5181f0da2becb05779a587291aaa9fdcdf1ce2eeb2745165f860ff4c4
4
- data.tar.gz: a287a3ce5b40ad941b114e4c95c4ad613c3372b3aa30549078ce998b9405c913
3
+ metadata.gz: 0c60e714e579a94981d17cf588f7b036ff39b95f95c6a4080b291c335e7ad4a4
4
+ data.tar.gz: b902418e60c5e98d19456aed4ae6aad09e787b4074a9b08177b9cb1fa967cb4f
5
5
  SHA512:
6
- metadata.gz: cb6bb847b36e285d969c160462d0a42d3fb0ba1cce7f479f13b6bfe759df299591f86bb05d1f6a6e545a96508c27fb1cb48e828ec431e07c3b8036392dbc20c0
7
- data.tar.gz: 53a14606a501034d0693f54e2dd56706a573d950c26fddc9950effcc9dbc4762eaf929756b1d609a9e7bf0c0a5507dd8913e5c6078fbf0ff5b68eae7b851a6e9
6
+ metadata.gz: 56f2f16354a2d1aff6565de44517b67722b8abc764dab185da0f4a46813a73599d32c7fdc1792790ad2cd1eaa08c92bda13b77ac76847da1c53b59bde0850c9b
7
+ data.tar.gz: efd9edf9ab76f389107ec9d02964ed4904655c3e5d2f2e6aec66cad668b6d034a5d61a7fea77f56b7f10d7c8a8f8ceb2635316d7987f41ac96f34c4bf1d25fc5
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json_schemer (0.1.2)
4
+ json_schemer (0.1.3)
5
5
  ecma-re-validator (~> 0.1.2)
6
6
  hana (~> 1.3.3)
7
7
  uri_template (~> 0.7.0)
@@ -23,13 +23,21 @@ module JSONSchemer
23
23
  NET_HTTP_REF_RESOLVER = proc { |uri| JSON.parse(Net::HTTP.get(uri)) }.freeze
24
24
  BOOLEANS = Set[true, false].freeze
25
25
 
26
- def initialize(schema, format: true, ref_resolver: DEFAULT_REF_RESOLVER)
26
+ def initialize(
27
+ schema,
28
+ format: true,
29
+ formats: nil,
30
+ keywords: nil,
31
+ ref_resolver: DEFAULT_REF_RESOLVER
32
+ )
27
33
  if schema.is_a?(Hash) && schema.key?('$schema') && schema['$schema'] != META_SCHEMA
28
34
  raise InvalidMetaSchema, "draft-07 is the only supported meta-schema (#{META_SCHEMA})"
29
35
  end
30
36
 
31
37
  @root = schema
32
38
  @format = format
39
+ @formats = formats
40
+ @keywords = keywords
33
41
  @ref_resolver = ref_resolver == 'net/http' ? NET_HTTP_REF_RESOLVER : ref_resolver
34
42
  end
35
43
 
@@ -70,6 +78,19 @@ module JSONSchemer
70
78
 
71
79
  validate_format(data, schema, pointer, format, &Proc.new) if format && format?
72
80
 
81
+ if keywords
82
+ keywords.each do |keyword, callable|
83
+ if schema.key?(keyword)
84
+ result = callable.call(data, schema, pointer)
85
+ if result.is_a?(Array)
86
+ result.each { |error| yield error }
87
+ elsif !result
88
+ yield error(data, schema, pointer, keyword)
89
+ end
90
+ end
91
+ end
92
+ end
93
+
73
94
  yield error(data, schema, pointer, 'enum') if enum && !enum.include?(data)
74
95
  yield error(data, schema, pointer, 'const') if schema.key?('const') && schema['const'] != data
75
96
 
@@ -106,12 +127,22 @@ module JSONSchemer
106
127
 
107
128
  private
108
129
 
109
- attr_reader :root, :ref_resolver
130
+ attr_reader :root, :formats, :keywords, :ref_resolver
110
131
 
111
132
  def format?
112
133
  !!@format
113
134
  end
114
135
 
136
+ def child(schema)
137
+ self.class.new(
138
+ schema,
139
+ format: format?,
140
+ formats: formats,
141
+ keywords: keywords,
142
+ ref_resolver: ref_resolver
143
+ )
144
+ end
145
+
115
146
  def error(data, schema, pointer, type)
116
147
  {
117
148
  'data' => data,
@@ -164,54 +195,59 @@ module JSONSchemer
164
195
  validate(data, ref_pointer.eval(root), pointer, pointer_uri(root, ref_pointer), &Proc.new)
165
196
  else
166
197
  ref_root = ref_resolver.call(ref_uri)
167
- ref_object = self.class.new(ref_root)
198
+ ref_object = child(ref_root)
168
199
  ref_object.validate(data, ref_pointer.eval(ref_root), pointer, pointer_uri(ref_root, ref_pointer), &Proc.new)
169
200
  end
170
201
  elsif ids.key?(ref_uri.to_s)
171
202
  validate(data, ids.fetch(ref_uri.to_s), pointer, ref_uri, &Proc.new)
172
203
  else
173
204
  ref_root = ref_resolver.call(ref_uri)
174
- ref_object = self.class.new(ref_root)
205
+ ref_object = child(ref_root)
175
206
  ref_object.validate(data, ref_object.ids.fetch(ref_uri.to_s, ref_root), pointer, ref_uri, &Proc.new)
176
207
  end
177
208
  end
178
209
 
179
210
  def validate_format(data, schema, pointer, format)
180
- valid = case format
181
- when 'date-time'
182
- valid_date_time?(data)
183
- when 'date'
184
- valid_date_time?("#{data}T04:05:06.123456789+07:00")
185
- when 'time'
186
- valid_date_time?("2001-02-03T#{data}")
187
- when 'email'
188
- data.ascii_only? && valid_email?(data)
189
- when 'idn-email'
190
- valid_email?(data)
191
- when 'hostname'
192
- data.ascii_only? && valid_hostname?(data)
193
- when 'idn-hostname'
194
- valid_hostname?(data)
195
- when 'ipv4'
196
- valid_ip?(data, :v4)
197
- when 'ipv6'
198
- valid_ip?(data, :v6)
199
- when 'uri'
200
- data.ascii_only? && valid_iri?(data)
201
- when 'uri-reference'
202
- data.ascii_only? && (valid_iri?(data) || valid_iri_reference?(data))
203
- when 'iri'
204
- valid_iri?(data)
205
- when 'iri-reference'
206
- valid_iri?(data) || valid_iri_reference?(data)
207
- when 'uri-template'
208
- valid_uri_template?(data)
209
- when 'json-pointer'
210
- valid_json_pointer?(data)
211
- when 'relative-json-pointer'
212
- valid_relative_json_pointer?(data)
213
- when 'regex'
214
- EcmaReValidator.valid?(data)
211
+ valid = if formats && formats.key?(format)
212
+ format_option = formats[format]
213
+ format_option == false || format_option.call(data, schema)
214
+ else
215
+ case format
216
+ when 'date-time'
217
+ valid_date_time?(data)
218
+ when 'date'
219
+ valid_date_time?("#{data}T04:05:06.123456789+07:00")
220
+ when 'time'
221
+ valid_date_time?("2001-02-03T#{data}")
222
+ when 'email'
223
+ data.ascii_only? && valid_email?(data)
224
+ when 'idn-email'
225
+ valid_email?(data)
226
+ when 'hostname'
227
+ data.ascii_only? && valid_hostname?(data)
228
+ when 'idn-hostname'
229
+ valid_hostname?(data)
230
+ when 'ipv4'
231
+ valid_ip?(data, :v4)
232
+ when 'ipv6'
233
+ valid_ip?(data, :v6)
234
+ when 'uri'
235
+ data.ascii_only? && valid_iri?(data)
236
+ when 'uri-reference'
237
+ data.ascii_only? && (valid_iri?(data) || valid_iri_reference?(data))
238
+ when 'iri'
239
+ valid_iri?(data)
240
+ when 'iri-reference'
241
+ valid_iri?(data) || valid_iri_reference?(data)
242
+ when 'uri-template'
243
+ valid_uri_template?(data)
244
+ when 'json-pointer'
245
+ valid_json_pointer?(data)
246
+ when 'relative-json-pointer'
247
+ valid_relative_json_pointer?(data)
248
+ when 'regex'
249
+ EcmaReValidator.valid?(data)
250
+ end
215
251
  end
216
252
  yield error(data, schema, pointer, 'format') unless valid
217
253
  end
@@ -1,3 +1,3 @@
1
1
  module JSONSchemer
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  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.1.2
4
+ version: 0.1.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: 2018-01-25 00:00:00.000000000 Z
11
+ date: 2018-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler