jsonschema 2.0.1 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +5 -0
- data/Rakefile +3 -11
- data/lib/jsonschema.rb +49 -39
- metadata +21 -29
data/README.rdoc
CHANGED
data/Rakefile
CHANGED
@@ -3,9 +3,8 @@ require 'rubygems'
|
|
3
3
|
require 'rake'
|
4
4
|
require 'rake/clean'
|
5
5
|
require 'rake/testtask'
|
6
|
-
require '
|
7
|
-
require 'rake/
|
8
|
-
require 'rake/rdoctask'
|
6
|
+
require 'rubygems/package_task'
|
7
|
+
require 'rake/task'
|
9
8
|
require 'rake/contrib/rubyforgepublisher'
|
10
9
|
require 'rake/contrib/sshpublisher'
|
11
10
|
require 'fileutils'
|
@@ -58,18 +57,11 @@ spec = Gem::Specification.new do |s|
|
|
58
57
|
s.files = %w(README.rdoc Rakefile) + Dir["{bin,test,lib}/**/*"]
|
59
58
|
end
|
60
59
|
|
61
|
-
|
60
|
+
Gem::PackageTask.new(spec) do |p|
|
62
61
|
p.need_tar = true
|
63
62
|
p.gem_spec = spec
|
64
63
|
end
|
65
64
|
|
66
|
-
Rake::RDocTask.new do |rdoc|
|
67
|
-
rdoc.rdoc_dir = 'doc'
|
68
|
-
rdoc.options += $rdoc_opts
|
69
|
-
# rdoc.template = 'resh'
|
70
|
-
rdoc.rdoc_files.include("README.rdoc", "lib/**/*.rb", "ext/**/*.c")
|
71
|
-
end
|
72
|
-
|
73
65
|
desc "gem spec"
|
74
66
|
task :gemspec do
|
75
67
|
File.open("#{$github_name}.gemspec", "wb") do |f|
|
data/lib/jsonschema.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module JSON
|
4
4
|
class Schema
|
5
|
-
VERSION = '2.0.
|
5
|
+
VERSION = '2.0.2'
|
6
6
|
class ValueError < Exception;end
|
7
7
|
class Undefined;end
|
8
8
|
TypesMap = {
|
@@ -21,10 +21,19 @@ module JSON
|
|
21
21
|
@refmap = {}
|
22
22
|
end
|
23
23
|
|
24
|
+
def keys
|
25
|
+
@keys ||= []
|
26
|
+
end
|
27
|
+
|
28
|
+
def key_path
|
29
|
+
keys.reject{|k| k == 'self'}.join(" > ")
|
30
|
+
end
|
31
|
+
|
24
32
|
def check_property value, schema, key, parent
|
25
33
|
if schema
|
34
|
+
keys.push key
|
26
35
|
# if @interactive && schema['readonly']
|
27
|
-
# raise ValueError, "#{
|
36
|
+
# raise ValueError, "#{key_path} is a readonly field , it can not be changed"
|
28
37
|
# end
|
29
38
|
|
30
39
|
if schema['id']
|
@@ -37,7 +46,7 @@ module JSON
|
|
37
46
|
|
38
47
|
if value == Undefined
|
39
48
|
unless schema['optional']
|
40
|
-
raise ValueError, "#{
|
49
|
+
raise ValueError, "#{key_path}: is missing and it is not optional"
|
41
50
|
end
|
42
51
|
|
43
52
|
# default
|
@@ -61,21 +70,21 @@ module JSON
|
|
61
70
|
rescue ValueError
|
62
71
|
flag = false
|
63
72
|
end
|
64
|
-
raise ValueError, "#{
|
73
|
+
raise ValueError, "#{key_path}: disallowed value was matched" if flag
|
65
74
|
end
|
66
75
|
|
67
76
|
unless value.nil?
|
68
|
-
if value.
|
77
|
+
if value.kind_of? Array
|
69
78
|
if schema['items']
|
70
|
-
if schema['items'].
|
79
|
+
if schema['items'].kind_of?(Array)
|
71
80
|
schema['items'].each_with_index {|val, index|
|
72
81
|
check_property(undefined_check(value, index), schema['items'][index], index, value)
|
73
82
|
}
|
74
83
|
if schema.include?('additionalProperties')
|
75
84
|
additional = schema['additionalProperties']
|
76
|
-
if additional.
|
85
|
+
if additional.kind_of?(FalseClass)
|
77
86
|
if schema['items'].size < value.size
|
78
|
-
raise ValueError, "#{
|
87
|
+
raise ValueError, "#{key_path}: There are more values in the array than are allowed by the items and additionalProperties restrictions."
|
79
88
|
end
|
80
89
|
else
|
81
90
|
value.each_with_index {|val, index|
|
@@ -90,46 +99,46 @@ module JSON
|
|
90
99
|
end
|
91
100
|
end
|
92
101
|
if schema['minItems'] && value.size < schema['minItems']
|
93
|
-
raise ValueError, "#{
|
102
|
+
raise ValueError, "#{key_path}: There must be a minimum of #{schema['minItems']} in the array"
|
94
103
|
end
|
95
104
|
if schema['maxItems'] && value.size > schema['maxItems']
|
96
|
-
raise ValueError, "#{
|
105
|
+
raise ValueError, "#{key_path}: There must be a maximum of #{schema['maxItems']} in the array"
|
97
106
|
end
|
98
107
|
elsif schema['properties']
|
99
108
|
check_object(value, schema['properties'], schema['additionalProperties'])
|
100
109
|
elsif schema.include?('additionalProperties')
|
101
110
|
additional = schema['additionalProperties']
|
102
|
-
unless additional.
|
103
|
-
if additional.
|
111
|
+
unless additional.kind_of?(TrueClass)
|
112
|
+
if additional.kind_of?(Hash) || additional.kind_of?(FalseClass)
|
104
113
|
properties = {}
|
105
114
|
value.each {|k, val|
|
106
|
-
if additional.
|
107
|
-
raise ValueError, "#{
|
115
|
+
if additional.kind_of?(FalseClass)
|
116
|
+
raise ValueError, "#{key_path}: Additional properties not defined by 'properties' are not allowed in field '#{k}'"
|
108
117
|
else
|
109
118
|
check_property(val, schema['additionalProperties'], k, value)
|
110
119
|
end
|
111
120
|
}
|
112
121
|
else
|
113
|
-
raise ValueError, "#{
|
122
|
+
raise ValueError, "#{key_path}: additionalProperties schema definition for field '#{}' is not an object"
|
114
123
|
end
|
115
124
|
end
|
116
125
|
end
|
117
126
|
|
118
|
-
if value.
|
127
|
+
if value.kind_of?(String)
|
119
128
|
# pattern
|
120
129
|
if schema['pattern'] && !(value =~ Regexp.new(schema['pattern']))
|
121
|
-
raise ValueError, "#{
|
130
|
+
raise ValueError, "#{key_path}: does not match the regex pattern #{schema['pattern']}"
|
122
131
|
end
|
123
132
|
|
124
133
|
strlen = value.split(//).size
|
125
134
|
# maxLength
|
126
135
|
if schema['maxLength'] && strlen > schema['maxLength']
|
127
|
-
raise ValueError, "#{
|
136
|
+
raise ValueError, "#{key_path}: may only be #{schema['maxLength']} characters long"
|
128
137
|
end
|
129
138
|
|
130
139
|
# minLength
|
131
140
|
if schema['minLength'] && strlen < schema['minLength']
|
132
|
-
raise ValueError, "#{
|
141
|
+
raise ValueError, "#{key_path}: must be at least #{schema['minLength']} characters long"
|
133
142
|
end
|
134
143
|
end
|
135
144
|
|
@@ -140,11 +149,11 @@ module JSON
|
|
140
149
|
minimumCanEqual = schema.fetch('minimumCanEqual', Undefined)
|
141
150
|
if minimumCanEqual == Undefined || minimumCanEqual
|
142
151
|
if value < schema['minimum']
|
143
|
-
raise ValueError, "#{
|
152
|
+
raise ValueError, "#{key_path}: must have a minimum value of #{schema['minimum']}"
|
144
153
|
end
|
145
154
|
else
|
146
155
|
if value <= schema['minimum']
|
147
|
-
raise ValueError, "#{
|
156
|
+
raise ValueError, "#{key_path}: must have a minimum value of #{schema['minimum']}"
|
148
157
|
end
|
149
158
|
end
|
150
159
|
end
|
@@ -154,11 +163,11 @@ module JSON
|
|
154
163
|
maximumCanEqual = schema.fetch('maximumCanEqual', Undefined)
|
155
164
|
if maximumCanEqual == Undefined || maximumCanEqual
|
156
165
|
if value > schema['maximum']
|
157
|
-
raise ValueError, "#{
|
166
|
+
raise ValueError, "#{key_path}: must have a maximum value of #{schema['maximum']}"
|
158
167
|
end
|
159
168
|
else
|
160
169
|
if value >= schema['maximum']
|
161
|
-
raise ValueError, "#{
|
170
|
+
raise ValueError, "#{key_path}: must have a maximum value of #{schema['maximum']}"
|
162
171
|
end
|
163
172
|
end
|
164
173
|
end
|
@@ -166,7 +175,7 @@ module JSON
|
|
166
175
|
# maxDecimal
|
167
176
|
if schema['maxDecimal'] && schema['maxDecimal'].kind_of?(Numeric)
|
168
177
|
if value.to_s =~ /\.\d{#{schema['maxDecimal']+1},}/
|
169
|
-
raise ValueError, "#{
|
178
|
+
raise ValueError, "#{key_path}: may only have #{schema['maxDecimal']} digits of decimal places"
|
170
179
|
end
|
171
180
|
end
|
172
181
|
|
@@ -175,18 +184,18 @@ module JSON
|
|
175
184
|
# enum
|
176
185
|
if schema['enum']
|
177
186
|
unless(schema['enum'].detect{|enum| enum == value })
|
178
|
-
raise ValueError, "#{
|
187
|
+
raise ValueError, "#{key_path}: does not have a value in the enumeration #{schema['enum'].join(", ")}"
|
179
188
|
end
|
180
189
|
end
|
181
190
|
|
182
191
|
# description
|
183
|
-
if schema['description'] && !schema['description'].
|
184
|
-
raise ValueError, "#{
|
192
|
+
if schema['description'] && !schema['description'].kind_of?(String)
|
193
|
+
raise ValueError, "#{key_path}: The description for field '#{value}' must be a string"
|
185
194
|
end
|
186
195
|
|
187
196
|
# title
|
188
|
-
if schema['title'] && !schema['title'].
|
189
|
-
raise ValueError, "#{
|
197
|
+
if schema['title'] && !schema['title'].kind_of?(String)
|
198
|
+
raise ValueError, "#{key_path}: The title for field '#{value}' must be a string"
|
190
199
|
end
|
191
200
|
|
192
201
|
# format
|
@@ -195,12 +204,13 @@ module JSON
|
|
195
204
|
|
196
205
|
end
|
197
206
|
end
|
207
|
+
keys.pop
|
198
208
|
end
|
199
209
|
end
|
200
210
|
|
201
211
|
def check_object value, object_type_def, additional
|
202
|
-
if object_type_def.
|
203
|
-
if !value.
|
212
|
+
if object_type_def.kind_of? Hash
|
213
|
+
if !value.kind_of?(Hash) || value.kind_of?(Array)
|
204
214
|
raise ValueError, "an object is required"
|
205
215
|
end
|
206
216
|
|
@@ -212,13 +222,13 @@ module JSON
|
|
212
222
|
end
|
213
223
|
value.each {|key, val|
|
214
224
|
if key.index('__') != 0 && object_type_def && !object_type_def[key] && additional == false
|
215
|
-
raise ValueError, "#{
|
225
|
+
raise ValueError, "The property #{key_path} > #{key} is not defined in the schema and the schema does not allow additional properties"
|
216
226
|
end
|
217
227
|
requires = object_type_def && object_type_def[key] && object_type_def[key]['requires']
|
218
228
|
if requires && !value.include?(requires)
|
219
|
-
raise ValueError, "
|
229
|
+
raise ValueError, "The presence of the property #{key_path} > #{key} requires that #{requires} also be present"
|
220
230
|
end
|
221
|
-
if object_type_def && object_type_def.
|
231
|
+
if object_type_def && object_type_def.kind_of?(Hash) && !object_type_def.include?(key)
|
222
232
|
check_property(val, additional, key, value)
|
223
233
|
end
|
224
234
|
if !@interactive && val && val['$schema']
|
@@ -230,7 +240,7 @@ module JSON
|
|
230
240
|
def check_type value, type, key, parent
|
231
241
|
converted_fieldtype = convert_type(type)
|
232
242
|
if converted_fieldtype
|
233
|
-
if converted_fieldtype.
|
243
|
+
if converted_fieldtype.kind_of? Array
|
234
244
|
datavalid = false
|
235
245
|
converted_fieldtype.each do |t|
|
236
246
|
begin
|
@@ -242,13 +252,13 @@ module JSON
|
|
242
252
|
end
|
243
253
|
end
|
244
254
|
unless datavalid
|
245
|
-
raise ValueError, "#{
|
255
|
+
raise ValueError, "#{key_path}: #{value.class} value found, but a #{type} is required"
|
246
256
|
end
|
247
|
-
elsif converted_fieldtype.
|
257
|
+
elsif converted_fieldtype.kind_of? Hash
|
248
258
|
check_property(value, type, key, parent)
|
249
259
|
else
|
250
|
-
unless value.
|
251
|
-
raise ValueError, "#{
|
260
|
+
unless value.kind_of? converted_fieldtype
|
261
|
+
raise ValueError, "#{key_path}: #{value.class} value found, but a #{type} is required"
|
252
262
|
end
|
253
263
|
end
|
254
264
|
end
|
metadata
CHANGED
@@ -1,64 +1,56 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonschema
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.2
|
4
5
|
prerelease:
|
5
|
-
version: 2.0.1
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Constellation
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
date: 2011-03-02 00:00:00 +09:00
|
14
|
-
default_executable:
|
12
|
+
date: 2012-02-28 00:00:00.000000000Z
|
15
13
|
dependencies: []
|
16
|
-
|
17
14
|
description: json schema library ruby porting from http://code.google.com/p/jsonschema/
|
18
15
|
email: utatane.tea@gmail.com
|
19
16
|
executables: []
|
20
|
-
|
21
17
|
extensions: []
|
22
|
-
|
23
|
-
extra_rdoc_files:
|
18
|
+
extra_rdoc_files:
|
24
19
|
- README.rdoc
|
25
|
-
files:
|
20
|
+
files:
|
26
21
|
- README.rdoc
|
27
22
|
- Rakefile
|
28
23
|
- test/jsonschema_test.rb
|
29
24
|
- lib/jsonschema.rb
|
30
|
-
has_rdoc: true
|
31
25
|
homepage: http://github.com/Constellation/ruby-jsonchema/tree/master
|
32
26
|
licenses: []
|
33
|
-
|
34
27
|
post_install_message:
|
35
|
-
rdoc_options:
|
28
|
+
rdoc_options:
|
36
29
|
- --main
|
37
30
|
- README.rdoc
|
38
31
|
- --charset
|
39
32
|
- utf-8
|
40
33
|
- --line-numbers
|
41
34
|
- --inline-source
|
42
|
-
require_paths:
|
35
|
+
require_paths:
|
43
36
|
- lib
|
44
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
38
|
none: false
|
46
|
-
requirements:
|
47
|
-
- -
|
48
|
-
- !ruby/object:Gem::Version
|
49
|
-
version:
|
50
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
44
|
none: false
|
52
|
-
requirements:
|
53
|
-
- -
|
54
|
-
- !ruby/object:Gem::Version
|
55
|
-
version:
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
56
49
|
requirements: []
|
57
|
-
|
58
50
|
rubyforge_project: jsonschema
|
59
|
-
rubygems_version: 1.
|
51
|
+
rubygems_version: 1.8.15
|
60
52
|
signing_key:
|
61
53
|
specification_version: 3
|
62
54
|
summary: json schema library ruby porting from http://code.google.com/p/jsonschema/
|
63
|
-
test_files:
|
55
|
+
test_files:
|
64
56
|
- test/jsonschema_test.rb
|