openc-json_schema 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +22 -0
- data/README.md +20 -0
- data/Rakefile +2 -0
- data/lib/openc/json_schema.rb +14 -0
- data/lib/openc/json_schema/format_validators.rb +11 -0
- data/lib/openc/json_schema/validator.rb +89 -0
- data/lib/openc/json_schema/version.rb +5 -0
- data/openc-json_schema.gemspec +25 -0
- data/spec/openc_json_schema_spec.rb +304 -0
- data/spec/schemas/aaa.json +12 -0
- data/spec/schemas/ccc.json +22 -0
- data/spec/schemas/includes/bbb.json +8 -0
- data/spec/schemas/includes/ddd.json +8 -0
- data/spec/schemas/includes/eee.json +8 -0
- data/spec/spec_helper.rb +39 -0
- data/spec/tmp/.gitkeep +0 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZjYwZDVlMmQ4MWVhNmI2MDY1OTAzOWYzMTMwY2JmOWI0MTBkN2ZlOA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZmQyZjNiMmIzZTg0N2VjNGRlNmYxOTM4ZjllNDJjMjQwMGY5MzFhMQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NGM1ZmZkNmJjN2ExMGJlMzhhMWUzMDI2MTdjNDZiZDE0NDkwNDc0MzVhMGQw
|
10
|
+
NTVmZGU5Yzc4NzdiN2MwNjkwN2NhMDFlYTA2YWEzMTY2ZGEzZjA1YmEyZjM1
|
11
|
+
NmJhOWVhNmQ3OGY1OTYwZTk4NjhkNzJkYzg1MDU2MGJkMDAyZWI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
OTdmYmMwODc0MTQ1OWYwZmQxZDdhMzQ3NzZjZDI3ZGQ1OWI4YWE3OWY4OTg5
|
14
|
+
ZWMxN2FhY2EwNmI2YjExZmY0NDkyNGQ0ZGZmMWE4OWRmNDBhNGEzZThjYjg2
|
15
|
+
YjhjODA5OGY2NzRiZDAwNmM5OWJhNzQxMDU3ODc3OGY2OWFmYmY=
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
spec/tmp/*.json
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
openc-json_schema (0.0.2)
|
5
|
+
json-schema (= 2.5.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
addressable (2.3.6)
|
11
|
+
diff-lcs (1.2.5)
|
12
|
+
json-schema (2.5.0)
|
13
|
+
addressable (~> 2.3)
|
14
|
+
rake (10.4.2)
|
15
|
+
rspec (3.1.0)
|
16
|
+
rspec-core (~> 3.1.0)
|
17
|
+
rspec-expectations (~> 3.1.0)
|
18
|
+
rspec-mocks (~> 3.1.0)
|
19
|
+
rspec-core (3.1.7)
|
20
|
+
rspec-support (~> 3.1.0)
|
21
|
+
rspec-expectations (3.1.2)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.1.0)
|
24
|
+
rspec-mocks (3.1.3)
|
25
|
+
rspec-support (~> 3.1.0)
|
26
|
+
rspec-support (3.1.2)
|
27
|
+
|
28
|
+
PLATFORMS
|
29
|
+
ruby
|
30
|
+
|
31
|
+
DEPENDENCIES
|
32
|
+
bundler (~> 1.7)
|
33
|
+
openc-json_schema!
|
34
|
+
rake (~> 10.0)
|
35
|
+
rspec (~> 3.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Peter Inglesby
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# Openc::JsonSchema
|
2
|
+
|
3
|
+
A wrapper around the json-schema gem to provide better error messages on
|
4
|
+
validation failure.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'openc-json_schema'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install openc-json_schema
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'json-schema'
|
2
|
+
|
3
|
+
require 'openc/json_schema/validator'
|
4
|
+
require 'openc/json_schema/format_validators'
|
5
|
+
|
6
|
+
module Openc
|
7
|
+
module JsonSchema
|
8
|
+
extend self
|
9
|
+
|
10
|
+
def validate(schema_or_filename, schema_dir, record)
|
11
|
+
Validator.validate(schema_or_filename, schema_dir, record)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
date_format_validator = -> value {
|
4
|
+
begin
|
5
|
+
Date.strptime(value, '%Y-%m-%d')
|
6
|
+
rescue ArgumentError
|
7
|
+
raise JSON::Schema::CustomFormatError.new('must be of format yyyy-mm-dd')
|
8
|
+
end
|
9
|
+
}
|
10
|
+
|
11
|
+
JSON::Validator.register_format_validator('date', date_format_validator)
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module Openc
|
2
|
+
module JsonSchema
|
3
|
+
module Validator
|
4
|
+
extend self
|
5
|
+
|
6
|
+
def validate(schema_or_filename, schema_dir, record)
|
7
|
+
case schema_or_filename
|
8
|
+
when Hash
|
9
|
+
schema = schema_or_filename
|
10
|
+
when String
|
11
|
+
filename = schema_or_filename
|
12
|
+
schema = JSON.parse(File.read(File.join(schema_dir, filename)))
|
13
|
+
else
|
14
|
+
raise TypeError.new("Unexpected value for schema_or_filename: #{schema_or_filename.class}")
|
15
|
+
end
|
16
|
+
|
17
|
+
# We must change directory for the relative paths in schemas to make sense.
|
18
|
+
errors = Dir.chdir(schema_dir) do
|
19
|
+
JSON::Validator.fully_validate(schema, record, :errors_as_objects => true)
|
20
|
+
end
|
21
|
+
|
22
|
+
# For now, we just handle the first error.
|
23
|
+
error = errors[0]
|
24
|
+
return if error.nil?
|
25
|
+
|
26
|
+
case error[:failed_attribute]
|
27
|
+
when 'Required'
|
28
|
+
match = error[:message].match(/required property of '(.*)'/)
|
29
|
+
missing_property = match[1]
|
30
|
+
path = fragment_to_path("#{error[:fragment]}/#{missing_property}")
|
31
|
+
|
32
|
+
{:type => :missing, :path => path}
|
33
|
+
when 'OneOf'
|
34
|
+
if error[:message].match(/did not match any/)
|
35
|
+
path_elements = fragment_to_path(error[:fragment]).split('.')
|
36
|
+
|
37
|
+
raise "Deeply nested OneOf error at: #{error[:fragment]}" unless path_elements.size == 1
|
38
|
+
|
39
|
+
record_fragment = record[path_elements[0]]
|
40
|
+
schema_fragments = schema['properties'][path_elements[0]]['oneOf']
|
41
|
+
|
42
|
+
unless schema_fragments.nil?
|
43
|
+
schema_fragments.each do |s|
|
44
|
+
s['properties'].each do |k, v|
|
45
|
+
next if v['enum'].nil?
|
46
|
+
|
47
|
+
if v['enum'].include?(record_fragment[k])
|
48
|
+
error1 = validate(s, schema_dir, record_fragment)
|
49
|
+
return error1.merge(:path => "#{path_elements[0]}.#{error1[:path]}")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
{:type => :one_of_no_matches, :path => fragment_to_path(error[:fragment])}
|
56
|
+
else
|
57
|
+
{:type => :one_of_many_matches, :path => fragment_to_path(error[:fragment])}
|
58
|
+
end
|
59
|
+
when 'MinLength'
|
60
|
+
match = error[:message].match(/minimum string length of (\d+) in/)
|
61
|
+
min_length = match[1].to_i
|
62
|
+
{:type => :too_short, :path => fragment_to_path(error[:fragment]), :length => min_length}
|
63
|
+
when 'MaxLength'
|
64
|
+
match = error[:message].match(/maximum string length of (\d+) in/)
|
65
|
+
max_length = match[1].to_i
|
66
|
+
{:type => :too_long, :path => fragment_to_path(error[:fragment]), :length => max_length}
|
67
|
+
when 'TypeV4'
|
68
|
+
match = error[:message].match(/the following types?: ([\w\s,]+) in schema/)
|
69
|
+
allowed_types = match[1].split(',').map(&:strip)
|
70
|
+
{:type => :type_mismatch, :path => fragment_to_path(error[:fragment]), :allowed_types => allowed_types}
|
71
|
+
when 'Enum'
|
72
|
+
match = error[:message].match(/the following values: ([\w\s,]+) in schema/)
|
73
|
+
allowed_values = match[1].split(',').map(&:strip)
|
74
|
+
{:type => :enum_mismatch, :path => fragment_to_path(error[:fragment]), :allowed_values => allowed_values}
|
75
|
+
else
|
76
|
+
if error[:message].match(/must be of format yyyy-mm-dd/)
|
77
|
+
{:type => :format_mismatch, :path => fragment_to_path(error[:fragment]), :expected_format => 'yyyy-mm-dd'}
|
78
|
+
else
|
79
|
+
{:type => :unknown, :path => fragment_to_path(error[:fragment]), :failed_attribute => error[:failed_attribute], :message => error[:message]}
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def fragment_to_path(fragment)
|
85
|
+
fragment.sub(/^#?\/*/, '').gsub('/', '.')
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'openc/json_schema/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "openc-json_schema"
|
8
|
+
spec.version = Openc::JsonSchema::VERSION
|
9
|
+
|
10
|
+
spec.author = "OpenCorporates"
|
11
|
+
spec.email = "info@opencorporates.com"
|
12
|
+
spec.summary = "Utilities for validating JSON"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
23
|
+
|
24
|
+
spec.add_dependency "json-schema", "2.5.0"
|
25
|
+
end
|
@@ -0,0 +1,304 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Openc::JsonSchema do
|
4
|
+
describe '.validate' do
|
5
|
+
specify 'when record is valid' do
|
6
|
+
schema = {
|
7
|
+
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
8
|
+
'type' => 'object',
|
9
|
+
'required' => ['aaa'],
|
10
|
+
}
|
11
|
+
record = {'aaa' => 'zzz'}
|
12
|
+
|
13
|
+
expect([schema, record]).to be_valid
|
14
|
+
end
|
15
|
+
|
16
|
+
specify 'when required top-level property missing' do
|
17
|
+
schema = {
|
18
|
+
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
19
|
+
'type' => 'object',
|
20
|
+
'required' => ['aaa'],
|
21
|
+
}
|
22
|
+
record = {}
|
23
|
+
|
24
|
+
expect([schema, record]).to fail_validation_with(
|
25
|
+
:type => :missing,
|
26
|
+
:path => 'aaa'
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
specify 'when required nested property missing' do
|
31
|
+
schema = {
|
32
|
+
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
33
|
+
'type' => 'object',
|
34
|
+
'required' => ['aaa'],
|
35
|
+
'properties' => {
|
36
|
+
'aaa' => {
|
37
|
+
'type' => 'object',
|
38
|
+
'required' => ['bbb'],
|
39
|
+
}
|
40
|
+
}
|
41
|
+
}
|
42
|
+
record = {'aaa' => {}}
|
43
|
+
|
44
|
+
expect([schema, record]).to fail_validation_with(
|
45
|
+
:type => :missing,
|
46
|
+
:path => 'aaa.bbb'
|
47
|
+
)
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'when none of oneOf options match' do
|
51
|
+
specify 'and we are switching on an enum field' do
|
52
|
+
schema = {
|
53
|
+
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
54
|
+
'type' => 'object',
|
55
|
+
'required' => ['aaa'],
|
56
|
+
'properties' => {
|
57
|
+
'aaa' => {
|
58
|
+
'type' => 'object',
|
59
|
+
'oneOf' => [{
|
60
|
+
'properties' => {
|
61
|
+
'a_type' => {
|
62
|
+
'enum' => ['a1']
|
63
|
+
},
|
64
|
+
'a_properties' => {
|
65
|
+
'type' => 'object',
|
66
|
+
'required' => ['bbb'],
|
67
|
+
}
|
68
|
+
}
|
69
|
+
}, {
|
70
|
+
'properties' => {
|
71
|
+
'a_type' => {
|
72
|
+
'enum' => ['a2']
|
73
|
+
},
|
74
|
+
'a_properties' => {
|
75
|
+
'type' => 'object',
|
76
|
+
'required' => ['ccc']
|
77
|
+
}
|
78
|
+
}
|
79
|
+
}]
|
80
|
+
}
|
81
|
+
}
|
82
|
+
}
|
83
|
+
|
84
|
+
record = {'aaa' => {'a_type' => 'a1', 'a_properties' => {}}}
|
85
|
+
|
86
|
+
expect([schema, record]).to fail_validation_with(
|
87
|
+
:type => :missing,
|
88
|
+
:path => 'aaa.a_properties.bbb'
|
89
|
+
)
|
90
|
+
end
|
91
|
+
|
92
|
+
specify 'and we are not switching on an enum field' do
|
93
|
+
schema = {
|
94
|
+
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
95
|
+
'type' => 'object',
|
96
|
+
'required' => ['aaa'],
|
97
|
+
'properties' => {
|
98
|
+
'aaa' => {
|
99
|
+
'type' => 'object',
|
100
|
+
'oneOf' => [{
|
101
|
+
'properties' => {
|
102
|
+
'bbb' => {
|
103
|
+
'type' => 'object',
|
104
|
+
'required' => ['ccc'],
|
105
|
+
}
|
106
|
+
}
|
107
|
+
}, {
|
108
|
+
'properties' => {
|
109
|
+
'bbb' => {
|
110
|
+
'type' => 'object',
|
111
|
+
'required' => ['ddd']
|
112
|
+
}
|
113
|
+
}
|
114
|
+
}]
|
115
|
+
}
|
116
|
+
}
|
117
|
+
}
|
118
|
+
|
119
|
+
record = {'aaa' => {'bbb' => {}}}
|
120
|
+
|
121
|
+
expect([schema, record]).to fail_validation_with(
|
122
|
+
:type => :one_of_no_matches,
|
123
|
+
:path => 'aaa'
|
124
|
+
)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
specify 'when top-level property too short' do
|
129
|
+
schema = {
|
130
|
+
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
131
|
+
'type' => 'object',
|
132
|
+
'properties' => {
|
133
|
+
'aaa' => {'minLength' => 2}
|
134
|
+
}
|
135
|
+
}
|
136
|
+
record = {'aaa' => 'x'}
|
137
|
+
|
138
|
+
expect([schema, record]).to fail_validation_with(
|
139
|
+
:type => :too_short,
|
140
|
+
:path => 'aaa',
|
141
|
+
:length => 2
|
142
|
+
)
|
143
|
+
end
|
144
|
+
|
145
|
+
specify 'when nested property too short' do
|
146
|
+
schema = {
|
147
|
+
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
148
|
+
'type' => 'object',
|
149
|
+
'properties' => {
|
150
|
+
'aaa' => {
|
151
|
+
'type' => 'object',
|
152
|
+
'properties' => {
|
153
|
+
'bbb' => {'minLength' => 2}
|
154
|
+
}
|
155
|
+
}
|
156
|
+
}
|
157
|
+
}
|
158
|
+
record = {'aaa' => {'bbb' => 'x'}}
|
159
|
+
|
160
|
+
expect([schema, record]).to fail_validation_with(
|
161
|
+
:type => :too_short,
|
162
|
+
:path => 'aaa.bbb',
|
163
|
+
:length => 2
|
164
|
+
)
|
165
|
+
end
|
166
|
+
|
167
|
+
specify 'when property too long' do
|
168
|
+
schema = {
|
169
|
+
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
170
|
+
'type' => 'object',
|
171
|
+
'properties' => {
|
172
|
+
'aaa' => {'maxLength' => 2}
|
173
|
+
}
|
174
|
+
}
|
175
|
+
record = {'aaa' => 'xxx'}
|
176
|
+
|
177
|
+
expect([schema, record]).to fail_validation_with(
|
178
|
+
:type => :too_long,
|
179
|
+
:path => 'aaa',
|
180
|
+
:length => 2
|
181
|
+
)
|
182
|
+
end
|
183
|
+
|
184
|
+
specify 'when property of wrong type and many types allowed' do
|
185
|
+
schema = {
|
186
|
+
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
187
|
+
'type' => 'object',
|
188
|
+
'properties' => {
|
189
|
+
'aaa' => {'type' => ['number', 'string']}
|
190
|
+
}
|
191
|
+
}
|
192
|
+
record = {'aaa' => ['xxx']}
|
193
|
+
|
194
|
+
expect([schema, record]).to fail_validation_with(
|
195
|
+
:type => :type_mismatch,
|
196
|
+
:path => 'aaa',
|
197
|
+
:allowed_types => ['number', 'string']
|
198
|
+
)
|
199
|
+
end
|
200
|
+
|
201
|
+
specify 'when property of wrong type and single type allowed' do
|
202
|
+
schema = {
|
203
|
+
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
204
|
+
'type' => 'object',
|
205
|
+
'properties' => {
|
206
|
+
'aaa' => {'type' => 'number'}
|
207
|
+
}
|
208
|
+
}
|
209
|
+
record = {'aaa' => 'xxx'}
|
210
|
+
|
211
|
+
expect([schema, record]).to fail_validation_with(
|
212
|
+
:type => :type_mismatch,
|
213
|
+
:path => 'aaa',
|
214
|
+
:allowed_types => ['number']
|
215
|
+
)
|
216
|
+
end
|
217
|
+
|
218
|
+
specify 'when property not in enum' do
|
219
|
+
schema = {
|
220
|
+
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
221
|
+
'type' => 'object',
|
222
|
+
'properties' => {
|
223
|
+
'aaa' => {'enum' => ['a', 'b', 'c']}
|
224
|
+
}
|
225
|
+
}
|
226
|
+
record = {'aaa' => 'z'}
|
227
|
+
|
228
|
+
expect([schema, record]).to fail_validation_with(
|
229
|
+
:type => :enum_mismatch,
|
230
|
+
:path => 'aaa',
|
231
|
+
:allowed_values => ['a', 'b', 'c']
|
232
|
+
)
|
233
|
+
end
|
234
|
+
|
235
|
+
specify 'when property of wrong format' do
|
236
|
+
schema = {
|
237
|
+
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
238
|
+
'type' => 'object',
|
239
|
+
'properties' => {
|
240
|
+
'aaa' => {'type' => 'string', 'format' => 'date'}
|
241
|
+
}
|
242
|
+
}
|
243
|
+
record = {'aaa' => 'zzz'}
|
244
|
+
|
245
|
+
expect([schema, record]).to fail_validation_with(
|
246
|
+
:type => :format_mismatch,
|
247
|
+
:path => 'aaa',
|
248
|
+
:expected_format => 'yyyy-mm-dd'
|
249
|
+
)
|
250
|
+
end
|
251
|
+
|
252
|
+
specify 'when property with format is empty' do
|
253
|
+
schema = {
|
254
|
+
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
255
|
+
'type' => 'object',
|
256
|
+
'properties' => {
|
257
|
+
'aaa' => {'type' => 'string', 'format' => 'date'}
|
258
|
+
}
|
259
|
+
}
|
260
|
+
record = {'aaa' => ''}
|
261
|
+
|
262
|
+
expect([schema, record]).to fail_validation_with(
|
263
|
+
:type => :format_mismatch,
|
264
|
+
:path => 'aaa',
|
265
|
+
:expected_format => 'yyyy-mm-dd'
|
266
|
+
)
|
267
|
+
end
|
268
|
+
|
269
|
+
context 'when schema includes $ref' do
|
270
|
+
specify 'when data is valid' do
|
271
|
+
schema_path = 'spec/schemas/aaa.json'
|
272
|
+
record = {'aaa' => 1, 'bbb' => {'BBB' => 10}}
|
273
|
+
expect([schema_path, record]).to be_valid
|
274
|
+
end
|
275
|
+
|
276
|
+
specify 'when data is invalid' do
|
277
|
+
schema_path = 'spec/schemas/aaa.json'
|
278
|
+
record = {'aaa' => 1, 'bbb' => {'BBB' => '10'}}
|
279
|
+
expect([schema_path, record]).to fail_validation_with(
|
280
|
+
:type => :type_mismatch,
|
281
|
+
:path => 'bbb.BBB',
|
282
|
+
:allowed_types => ['number']
|
283
|
+
)
|
284
|
+
end
|
285
|
+
end
|
286
|
+
|
287
|
+
specify 'when schema includes oneOfs which contain $refs' do
|
288
|
+
schema_path = 'spec/schemas/ccc.json'
|
289
|
+
record = {
|
290
|
+
'ccc' => {
|
291
|
+
'ccc_type' => 'ddd',
|
292
|
+
'ccc_properties' => {
|
293
|
+
'ddd' => 'not-a-number'
|
294
|
+
}
|
295
|
+
}
|
296
|
+
}
|
297
|
+
expect([schema_path, record]).to fail_validation_with(
|
298
|
+
:type => :type_mismatch,
|
299
|
+
:path => 'ccc.ccc_properties.ddd',
|
300
|
+
:allowed_types => ['number'],
|
301
|
+
)
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
{
|
2
|
+
"$schema": "http://json-schema.org/draft-04/schema#",
|
3
|
+
"type": "object",
|
4
|
+
"properties": {
|
5
|
+
"ccc": {
|
6
|
+
"type": "object",
|
7
|
+
"oneOf": [
|
8
|
+
{
|
9
|
+
"properties": {
|
10
|
+
"ccc_type": {"enum": ["ddd"]},
|
11
|
+
"ccc_properties": {"$ref": "includes/ddd.json"}
|
12
|
+
}
|
13
|
+
}, {
|
14
|
+
"properties": {
|
15
|
+
"ccc_type": {"enum": ["eee"]},
|
16
|
+
"ccc_properties": {"$ref": "includes/eee.json"}
|
17
|
+
}
|
18
|
+
}
|
19
|
+
]
|
20
|
+
}
|
21
|
+
}
|
22
|
+
}
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'openc/json_schema'
|
2
|
+
|
3
|
+
def get_error(schema_or_path, record)
|
4
|
+
case schema_or_path
|
5
|
+
when Hash
|
6
|
+
json_data = schema_or_path.to_json
|
7
|
+
filename = Digest::MD5.hexdigest(json_data) + '.json'
|
8
|
+
schema_dir = "spec/tmp"
|
9
|
+
File.open(File.join(schema_dir, filename), 'w') {|f| f.write(json_data)}
|
10
|
+
schema_or_filename = schema_or_path
|
11
|
+
when String
|
12
|
+
schema_or_filename = File.basename(schema_or_path)
|
13
|
+
schema_dir = File.dirname(schema_or_path)
|
14
|
+
else
|
15
|
+
raise
|
16
|
+
end
|
17
|
+
|
18
|
+
error = Openc::JsonSchema.validate(schema_or_filename, schema_dir, record)
|
19
|
+
end
|
20
|
+
|
21
|
+
RSpec::Matchers.define(:fail_validation_with) do |expected|
|
22
|
+
match do |actual|
|
23
|
+
schema_or_path, record = actual
|
24
|
+
@error = get_error(schema_or_path, record)
|
25
|
+
expect(@error).to eq(expected)
|
26
|
+
end
|
27
|
+
|
28
|
+
failure_message do |actual|
|
29
|
+
"Expected error to be #{expected}, but was #{@error}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
RSpec::Matchers.define(:be_valid) do
|
34
|
+
match do |actual|
|
35
|
+
schema_or_path, record = actual
|
36
|
+
error = get_error(schema_or_path, record)
|
37
|
+
expect(error).to eq(nil)
|
38
|
+
end
|
39
|
+
end
|
data/spec/tmp/.gitkeep
ADDED
File without changes
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: openc-json_schema
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- OpenCorporates
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: json-schema
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.5.0
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.5.0
|
69
|
+
description:
|
70
|
+
email: info@opencorporates.com
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files: []
|
74
|
+
files:
|
75
|
+
- .gitignore
|
76
|
+
- Gemfile
|
77
|
+
- Gemfile.lock
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/openc/json_schema.rb
|
82
|
+
- lib/openc/json_schema/format_validators.rb
|
83
|
+
- lib/openc/json_schema/validator.rb
|
84
|
+
- lib/openc/json_schema/version.rb
|
85
|
+
- openc-json_schema.gemspec
|
86
|
+
- spec/openc_json_schema_spec.rb
|
87
|
+
- spec/schemas/aaa.json
|
88
|
+
- spec/schemas/ccc.json
|
89
|
+
- spec/schemas/includes/bbb.json
|
90
|
+
- spec/schemas/includes/ddd.json
|
91
|
+
- spec/schemas/includes/eee.json
|
92
|
+
- spec/spec_helper.rb
|
93
|
+
- spec/tmp/.gitkeep
|
94
|
+
homepage:
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata: {}
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ! '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ! '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 2.2.2
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: Utilities for validating JSON
|
118
|
+
test_files:
|
119
|
+
- spec/openc_json_schema_spec.rb
|
120
|
+
- spec/schemas/aaa.json
|
121
|
+
- spec/schemas/ccc.json
|
122
|
+
- spec/schemas/includes/bbb.json
|
123
|
+
- spec/schemas/includes/ddd.json
|
124
|
+
- spec/schemas/includes/eee.json
|
125
|
+
- spec/spec_helper.rb
|
126
|
+
- spec/tmp/.gitkeep
|
127
|
+
has_rdoc:
|