openc-json_schema 0.0.14 → 0.0.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +4 -1
- data/.yardopts +3 -0
- data/README.md +6 -1
- data/lib/openc/json_schema.rb +18 -2
- data/lib/openc/json_schema/validator.rb +3 -3
- data/lib/openc/json_schema/version.rb +1 -1
- data/openc-json_schema.gemspec +2 -1
- data/spec/openc_json_schema_spec.rb +92 -2
- data/spec/spec_helper.rb +8 -3
- metadata +24 -26
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6f54658d07145fbb5008c4db3ff54233455bf6bd
|
4
|
+
data.tar.gz: 3388c3350f07c0827efd9277aaa166ea06b0ab3f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 50910d269df9a4e70a74449a196dd51464b49169e8fd1dc6784f95ccc93993c75c7ee89eaf9f07c6f304bfe9bbd5dbcf4e6a97181a8eba1461af67723a78848e
|
7
|
+
data.tar.gz: 25f41b1db6d440c1ad9a1919bb45394151b888fe273d6670819cd1d61e7c74ac99e85c65d96fea448340311b4458ecd482cfc6a18d76fd4b7e59be30d17eea99
|
data/.gitignore
CHANGED
data/.yardopts
ADDED
data/README.md
CHANGED
@@ -16,4 +16,9 @@ Bump the version in `lib/openc/json_schema/version.rb` according to the [Semanti
|
|
16
16
|
git commit lib/openc/json_schema/version.rb -m 'Release new version'
|
17
17
|
rake release # requires Rubygems credentials
|
18
18
|
|
19
|
-
|
19
|
+
In [morph](https://github.com/sebbacon/morph), run:
|
20
|
+
|
21
|
+
bundle update openc-json_schema
|
22
|
+
git commit Gemfile.lock -m 'Bump openc-json_schema' && git push
|
23
|
+
|
24
|
+
Finally, [rebuild the Docker image](https://github.com/openc/morph-docker-ruby#readme) and deploy [morph](https://github.com/sebbacon/morph).
|
data/lib/openc/json_schema.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
require 'json-pointer'
|
2
2
|
require 'json-schema'
|
3
3
|
|
4
|
+
require 'json_validation'
|
5
|
+
require 'openc_json_schema_formats'
|
6
|
+
|
4
7
|
require 'openc/json_schema/date_converter'
|
5
8
|
require 'openc/json_schema/format_validators'
|
6
9
|
require 'openc/json_schema/utils'
|
@@ -11,8 +14,21 @@ module Openc
|
|
11
14
|
module JsonSchema
|
12
15
|
extend self
|
13
16
|
|
14
|
-
def validate(
|
15
|
-
|
17
|
+
def validate(schema, record)
|
18
|
+
if schema.is_a?(String)
|
19
|
+
validator = JsonValidation.load_validator(schema)
|
20
|
+
else
|
21
|
+
validator = JsonValidation.build_validator(schema)
|
22
|
+
end
|
23
|
+
fast_validation_ok = validator.validate(record)
|
24
|
+
if fast_validation_ok
|
25
|
+
nil
|
26
|
+
else
|
27
|
+
# Currently JsonValidation doesn't support error messages,
|
28
|
+
# just returns true or false; so on a failure, we have to fall
|
29
|
+
# back to the slower version
|
30
|
+
Validator.validate(schema, record)
|
31
|
+
end
|
16
32
|
end
|
17
33
|
|
18
34
|
def convert_dates(schema_path, record)
|
@@ -38,8 +38,8 @@ module Openc
|
|
38
38
|
record = JsonPointer.new(record, error[:fragment][1..-1]).value
|
39
39
|
schema = JSON::Validator.schema_for_uri(error[:schema]).schema
|
40
40
|
|
41
|
-
path = fragment_to_path(error[:fragment])
|
42
|
-
one_of = walk_schema(schema, path).each_with_index
|
41
|
+
path = fragment_to_path(error[:fragment])
|
42
|
+
one_of = walk_schema(schema, path.split('.')).each_with_index
|
43
43
|
|
44
44
|
# Try to report errors for relevant `oneOf` schemas only.
|
45
45
|
schemas_matching_type = case record
|
@@ -52,7 +52,7 @@ module Openc
|
|
52
52
|
when Array
|
53
53
|
one_of.select{|schema, _| schema['type'] == 'array'}
|
54
54
|
else
|
55
|
-
raise "Unexpected type
|
55
|
+
raise "Unexpected type #{record.class.name} for #{record.inspect} at #{path}"
|
56
56
|
end
|
57
57
|
|
58
58
|
matches = schemas_matching_type.size
|
data/openc-json_schema.gemspec
CHANGED
@@ -17,8 +17,9 @@ Gem::Specification.new do |gem|
|
|
17
17
|
|
18
18
|
gem.add_dependency "json-schema", "~> 2.6.0"
|
19
19
|
gem.add_dependency "json-pointer"
|
20
|
+
gem.add_dependency "openc_json_schema_formats"
|
21
|
+
gem.add_dependency "json_validation"
|
20
22
|
|
21
|
-
gem.add_development_dependency "bundler", "~> 1.7"
|
22
23
|
gem.add_development_dependency "coveralls"
|
23
24
|
gem.add_development_dependency "rake", "~> 10.0"
|
24
25
|
gem.add_development_dependency "rspec", "~> 3.0"
|
@@ -90,7 +90,7 @@ describe Openc::JsonSchema do
|
|
90
90
|
}
|
91
91
|
}
|
92
92
|
}
|
93
|
-
|
93
|
+
|
94
94
|
record = {'aaa' => {'a_type' => 'a1', 'a_properties' => {}}}
|
95
95
|
|
96
96
|
error = 'Missing required property: aaa.a_properties.bbb'
|
@@ -166,7 +166,7 @@ describe Openc::JsonSchema do
|
|
166
166
|
}
|
167
167
|
}
|
168
168
|
}
|
169
|
-
|
169
|
+
|
170
170
|
record = {'aaa' => {'bbb' => {}}}
|
171
171
|
|
172
172
|
error = 'No match for property: aaa'
|
@@ -305,6 +305,96 @@ describe Openc::JsonSchema do
|
|
305
305
|
expect([schema, record]).to fail_validation_with(error)
|
306
306
|
end
|
307
307
|
|
308
|
+
specify 'when date property has a timestamp' do
|
309
|
+
schema = {
|
310
|
+
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
311
|
+
'type' => 'object',
|
312
|
+
'properties' => {
|
313
|
+
'aaa' => {'type' => 'string', 'format' => 'date'}
|
314
|
+
}
|
315
|
+
}
|
316
|
+
record = {'aaa' => '2016-01-12T21:52:11Z'}
|
317
|
+
|
318
|
+
expect([schema, record]).to be_valid
|
319
|
+
end
|
320
|
+
|
321
|
+
specify 'when date property has a timestamp and a bad date' do
|
322
|
+
schema = {
|
323
|
+
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
324
|
+
'type' => 'object',
|
325
|
+
'properties' => {
|
326
|
+
'aaa' => {'type' => 'string', 'format' => 'date'}
|
327
|
+
}
|
328
|
+
}
|
329
|
+
dates = ["2016-03-13",
|
330
|
+
"2016-03-21",
|
331
|
+
"2016-03-12T14:58:51.941787",
|
332
|
+
"2016-03-15",
|
333
|
+
"2016-03-12T15:01:27.767907",
|
334
|
+
"2016-03-12T13:28:59.947478",
|
335
|
+
"2016-03-12T11:30:18.731536",
|
336
|
+
"2016-03-13",
|
337
|
+
"2016-03-13",
|
338
|
+
"2016-03-12T11:17:52.814474",
|
339
|
+
"2016-03-12T15:12:46.227805",
|
340
|
+
"2016-03-12T15:45:03.405024",
|
341
|
+
"2016-03-12T13:17:15.362714",
|
342
|
+
"2016-03-15",
|
343
|
+
"2016-03-13",
|
344
|
+
"2016-03-23",
|
345
|
+
"2016-03-12T08:20:02.088387",
|
346
|
+
"2016-03-21",
|
347
|
+
"2016-03-13",
|
348
|
+
"2016-03-21",
|
349
|
+
"2016-03-11T17:54:20.247649",
|
350
|
+
"2016-03-12",
|
351
|
+
"2016-03-12T15:07:20.572849",
|
352
|
+
"2016-03-20",
|
353
|
+
"2016-03-13",
|
354
|
+
"2016-03-12T11:24:07.573208",
|
355
|
+
"2016-03-20",
|
356
|
+
"2016-03-11T17:31:38.482306",
|
357
|
+
"2016-03-16",
|
358
|
+
"2016-03-12T15:15:13.430171",
|
359
|
+
"2016-03-11T17:07:26.781185",
|
360
|
+
"2016-03-12T13:33:24.754198",
|
361
|
+
"2016-03-15",
|
362
|
+
"2016-03-20",
|
363
|
+
"2016-03-12",
|
364
|
+
"2016-03-15",
|
365
|
+
"2016-03-12T14:04:36.597795",
|
366
|
+
"2016-03-24",
|
367
|
+
"2016-03-11T13:20:40.231104",
|
368
|
+
"2016-03-16",
|
369
|
+
"2016-03-20",
|
370
|
+
"2016-03-12",
|
371
|
+
"2016-03-23",
|
372
|
+
"2016-03-15",
|
373
|
+
"2016-03-24",
|
374
|
+
"2016-03-24",
|
375
|
+
"2016-03-23",
|
376
|
+
"2015-10-02 10:15:56 +0000",
|
377
|
+
"2015-02-28 17:32:37 +0000",
|
378
|
+
"2016-03-18 10:35:30 +0000",
|
379
|
+
"2016-03-07 09:48:23 +0000",
|
380
|
+
"2016-03-10 12:27:33 +0000",
|
381
|
+
"2015-02-28 18:04:19 +0000",
|
382
|
+
"2015-03-04 18:14:14 +0000",
|
383
|
+
"2015-03-09 10:44:52 +0000",
|
384
|
+
"2015-04-29 17:49:37 +0000",
|
385
|
+
"2015-09-04 13:35:52 +0000",
|
386
|
+
"2016-03-10 09:39:21 +0000",
|
387
|
+
"2016-03-11 12:28:38 +0000",
|
388
|
+
"2016-03-11 10:37:50 +0000",
|
389
|
+
"2015-09-18 11:48:50 +0000"]
|
390
|
+
dates.each do |d|
|
391
|
+
puts d
|
392
|
+
record = {'aaa' => d}
|
393
|
+
|
394
|
+
expect([schema, record]).to be_valid
|
395
|
+
end
|
396
|
+
end
|
397
|
+
|
308
398
|
specify 'when non-blank property of wrong format' do
|
309
399
|
schema = {
|
310
400
|
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
data/spec/spec_helper.rb
CHANGED
@@ -9,12 +9,17 @@ require 'openc/json_schema'
|
|
9
9
|
|
10
10
|
RSpec::Matchers.define(:fail_validation_with) do |expected|
|
11
11
|
match do |(schema_or_path, record)|
|
12
|
-
@actual = Openc::JsonSchema.validate(schema_or_path, record)
|
13
|
-
expect(@actual).
|
12
|
+
@actual = Openc::JsonSchema.validate(schema_or_path, record)
|
13
|
+
expect(@actual).to_not be(nil)
|
14
|
+
expect(@actual[:message]).to eq(expected)
|
14
15
|
end
|
15
16
|
|
16
17
|
failure_message do |actual|
|
17
|
-
|
18
|
+
if actual.nil?
|
19
|
+
"Expected error, but there was none"
|
20
|
+
else
|
21
|
+
"Expected error to be #{expected}, but was #{actual}"
|
22
|
+
end
|
18
23
|
end
|
19
24
|
end
|
20
25
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: openc-json_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.15
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- OpenCorporates
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-04-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json-schema
|
@@ -39,19 +39,33 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: openc_json_schema_formats
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
48
|
-
type: :
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: json_validation
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
53
67
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
68
|
+
version: '0'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: coveralls
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -102,6 +116,7 @@ extra_rdoc_files: []
|
|
102
116
|
files:
|
103
117
|
- ".gitignore"
|
104
118
|
- ".travis.yml"
|
119
|
+
- ".yardopts"
|
105
120
|
- Gemfile
|
106
121
|
- LICENSE
|
107
122
|
- README.md
|
@@ -154,22 +169,5 @@ rubygems_version: 2.4.5
|
|
154
169
|
signing_key:
|
155
170
|
specification_version: 4
|
156
171
|
summary: Utilities for validating JSON
|
157
|
-
test_files:
|
158
|
-
- spec/openc_json_schema_spec.rb
|
159
|
-
- spec/schemas/aaa.json
|
160
|
-
- spec/schemas/ccc.json
|
161
|
-
- spec/schemas/fff.json
|
162
|
-
- spec/schemas/iii.json
|
163
|
-
- spec/schemas/includes/bbb.json
|
164
|
-
- spec/schemas/includes/ddd.json
|
165
|
-
- spec/schemas/includes/eee.json
|
166
|
-
- spec/schemas/includes/ggg.json
|
167
|
-
- spec/schemas/includes/hhh.json
|
168
|
-
- spec/schemas/includes/jjj.json
|
169
|
-
- spec/schemas/includes/mmm.json
|
170
|
-
- spec/schemas/includes/zzz.json
|
171
|
-
- spec/schemas/kkk.json
|
172
|
-
- spec/schemas/lll.json
|
173
|
-
- spec/schemas/yyy.json
|
174
|
-
- spec/spec_helper.rb
|
172
|
+
test_files: []
|
175
173
|
has_rdoc:
|