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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/json_schemer.rb +75 -39
- data/lib/json_schemer/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0c60e714e579a94981d17cf588f7b036ff39b95f95c6a4080b291c335e7ad4a4
|
4
|
+
data.tar.gz: b902418e60c5e98d19456aed4ae6aad09e787b4074a9b08177b9cb1fa967cb4f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56f2f16354a2d1aff6565de44517b67722b8abc764dab185da0f4a46813a73599d32c7fdc1792790ad2cd1eaa08c92bda13b77ac76847da1c53b59bde0850c9b
|
7
|
+
data.tar.gz: efd9edf9ab76f389107ec9d02964ed4904655c3e5d2f2e6aec66cad668b6d034a5d61a7fea77f56b7f10d7c8a8f8ceb2635316d7987f41ac96f34c4bf1d25fc5
|
data/Gemfile.lock
CHANGED
data/lib/json_schemer.rb
CHANGED
@@ -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(
|
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 =
|
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 =
|
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 =
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
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
|
data/lib/json_schemer/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2018-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|