json_schema 0.14.1 → 0.14.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/validate-schema +14 -11
- data/lib/json_schema/attributes.rb +108 -0
- data/lib/json_schema/parser.rb +7 -0
- data/lib/json_schema/reference_expander.rb +6 -4
- data/lib/json_schema/schema.rb +40 -115
- data/lib/json_schema/validator.rb +3 -2
- data/lib/json_schema.rb +1 -0
- data/test/bin_test.rb +20 -0
- data/test/json_pointer/evaluator_test.rb +1 -0
- data/test/json_schema/attribute_test.rb +98 -0
- data/test/json_schema/reference_expander_test.rb +12 -0
- data/test/test_helper.rb +13 -0
- metadata +19 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1d59f23d58a9aeed30273ccdcec6a106dbede93
|
4
|
+
data.tar.gz: d25d184e8fc7bc6345e67ebb89b9c8d21b601ad9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f33b776903af3753baa223758920ecbb77e1d48dec0973c8cf85fea4bf92c19115f78a5dc8202912ab3d4b6faf861cdf24bf34595f1a882030721364fd56c099
|
7
|
+
data.tar.gz: c0e594eb23d5faf03c6b8d14b54e24af9db99be45ae786ccb3ec33af383470b131a5b20b1fa94a2236eb8bb1391cbc2e64a7fca3b1e08fbd0cf14cd621a0e102
|
data/bin/validate-schema
CHANGED
@@ -10,7 +10,7 @@ end
|
|
10
10
|
|
11
11
|
command = Commands::ValidateSchema.new
|
12
12
|
|
13
|
-
OptionParser.new { |opts|
|
13
|
+
parser = OptionParser.new { |opts|
|
14
14
|
opts.on("-d", "--detect", "Detect schema from $schema") do
|
15
15
|
command.detect = true
|
16
16
|
|
@@ -21,16 +21,19 @@ OptionParser.new { |opts|
|
|
21
21
|
opts.on("-s", "--schema SCHEMA", "Additional schema to use for references") do |s|
|
22
22
|
command.extra_schemas << s
|
23
23
|
end
|
24
|
-
}
|
24
|
+
}
|
25
25
|
|
26
|
-
|
26
|
+
if $0 == __FILE__
|
27
|
+
parser.parse!
|
28
|
+
success = command.run(ARGV.dup)
|
27
29
|
|
28
|
-
if success
|
29
|
-
|
30
|
-
elsif !command.errors.empty?
|
31
|
-
|
32
|
-
|
33
|
-
else
|
34
|
-
|
35
|
-
|
30
|
+
if success
|
31
|
+
command.messages.each { |m| $stdout.puts(m) }
|
32
|
+
elsif !command.errors.empty?
|
33
|
+
command.errors.each { |e| $stderr.puts(e) }
|
34
|
+
exit(1)
|
35
|
+
else
|
36
|
+
print_usage!
|
37
|
+
exit(1)
|
38
|
+
end
|
36
39
|
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
module JsonSchema
|
2
|
+
# Attributes mixes in some useful attribute-related methods for use in
|
3
|
+
# defining schema classes in a spirit similar to Ruby's attr_accessor and
|
4
|
+
# friends.
|
5
|
+
module Attributes
|
6
|
+
# Provides class-level methods for the Attributes module.
|
7
|
+
module ClassMethods
|
8
|
+
# Attributes that should be copied between classes when invoking
|
9
|
+
# Attributes#copy_from.
|
10
|
+
#
|
11
|
+
# Hash contains instance variable names mapped to a default value for the
|
12
|
+
# field.
|
13
|
+
attr_reader :copyable_attrs
|
14
|
+
|
15
|
+
# Attributes that are part of the JSON schema and hyper-schema
|
16
|
+
# specifications. These are allowed to be accessed with the [] operator.
|
17
|
+
#
|
18
|
+
# Hash contains the access key mapped to the name of the method that should
|
19
|
+
# be invoked to retrieve a value. For example, `type` maps to `type` and
|
20
|
+
# `additionalItems` maps to `additional_items`.
|
21
|
+
attr_reader :schema_attrs
|
22
|
+
|
23
|
+
# identical to attr_accessible, but allows us to copy in values from a
|
24
|
+
# target schema to help preserve our hierarchy during reference expansion
|
25
|
+
def attr_copyable(attr, options = {})
|
26
|
+
attr_accessor(attr)
|
27
|
+
|
28
|
+
# Usually the default being assigned here is nil.
|
29
|
+
self.copyable_attrs["@#{attr}".to_sym] = options[:default]
|
30
|
+
|
31
|
+
if default = options[:default]
|
32
|
+
# remove the reader already created by attr_accessor
|
33
|
+
remove_method(attr)
|
34
|
+
|
35
|
+
define_method(attr) do
|
36
|
+
val = instance_variable_get(:"@#{attr}")
|
37
|
+
if !val.nil?
|
38
|
+
val
|
39
|
+
else
|
40
|
+
if [Array, Hash, Set].include?(default.class)
|
41
|
+
default.dup
|
42
|
+
else
|
43
|
+
default
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def attr_schema(attr, options = {})
|
51
|
+
attr_copyable(attr, :default => options[:default])
|
52
|
+
self.schema_attrs[options[:schema_name] || attr] = attr
|
53
|
+
end
|
54
|
+
|
55
|
+
# Directive indicating that attributes should be inherited from a parent
|
56
|
+
# class.
|
57
|
+
#
|
58
|
+
# Must appear as first statement in class that mixes in (or whose parent
|
59
|
+
# mixes in) the Attributes module.
|
60
|
+
def inherit_attrs
|
61
|
+
@copyable_attrs = self.superclass.instance_variable_get(:@copyable_attrs).dup
|
62
|
+
@schema_attrs = self.superclass.instance_variable_get(:@schema_attrs).dup
|
63
|
+
end
|
64
|
+
|
65
|
+
# Initializes some class instance variables required to make other
|
66
|
+
# methods in the Attributes module work. Run automatically when the
|
67
|
+
# module is mixed into another class.
|
68
|
+
def initialize_attrs
|
69
|
+
@copyable_attrs = {}
|
70
|
+
@schema_attrs = {}
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.included(klass)
|
75
|
+
klass.extend(ClassMethods)
|
76
|
+
klass.send(:initialize_attrs)
|
77
|
+
end
|
78
|
+
|
79
|
+
# Allows the values of schema attributes to be accessed with a symbol or a
|
80
|
+
# string. So for example, the value of `schema.additional_items` could be
|
81
|
+
# procured with `schema[:additionalItems]`. This only works for attributes
|
82
|
+
# that are part of the JSON schema specification; other methods on the
|
83
|
+
# class are not available (e.g. `expanded`.)
|
84
|
+
#
|
85
|
+
# This is implemented so that `JsonPointer::Evaluator` can evaluate a
|
86
|
+
# reference on an sintance of this class (as well as plain JSON data).
|
87
|
+
def [](name)
|
88
|
+
name = name.to_sym
|
89
|
+
if self.class.schema_attrs.key?(name)
|
90
|
+
send(self.class.schema_attrs[name])
|
91
|
+
else
|
92
|
+
raise NoMethodError, "Schema does not respond to ##{name}"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def copy_from(schema)
|
97
|
+
self.class.copyable_attrs.each do |copyable, _|
|
98
|
+
instance_variable_set(copyable, schema.instance_variable_get(copyable))
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def initialize_attrs
|
103
|
+
self.class.copyable_attrs.each do |attr, _|
|
104
|
+
instance_variable_set(attr, nil)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
data/lib/json_schema/parser.rb
CHANGED
@@ -186,6 +186,13 @@ module JsonSchema
|
|
186
186
|
link = Schema::Link.new
|
187
187
|
link.parent = schema
|
188
188
|
|
189
|
+
link.data = l
|
190
|
+
|
191
|
+
# any parsed schema is automatically expanded
|
192
|
+
link.expanded = true
|
193
|
+
|
194
|
+
link.uri = nil
|
195
|
+
|
189
196
|
link.description = l["description"]
|
190
197
|
link.enc_type = l["encType"]
|
191
198
|
link.href = l["href"]
|
@@ -254,10 +254,12 @@ module JsonSchema
|
|
254
254
|
each { |s| yielder << s }
|
255
255
|
|
256
256
|
# schemas contained inside hyper-schema links objects
|
257
|
-
schema.links
|
258
|
-
|
259
|
-
|
260
|
-
|
257
|
+
if schema.links
|
258
|
+
schema.links.map { |l| [l.schema, l.target_schema] }.
|
259
|
+
flatten.
|
260
|
+
compact.
|
261
|
+
each { |s| yielder << s }
|
262
|
+
end
|
261
263
|
end
|
262
264
|
end
|
263
265
|
|
data/lib/json_schema/schema.rb
CHANGED
@@ -2,45 +2,18 @@ require "json"
|
|
2
2
|
|
3
3
|
module JsonSchema
|
4
4
|
class Schema
|
5
|
-
|
6
|
-
|
7
|
-
# Attributes that are part of the JSON schema and hyper-schema
|
8
|
-
# specifications. These are allowed to be accessed with the [] operator.
|
9
|
-
#
|
10
|
-
# Hash contains the access key mapped to the name of the method that should
|
11
|
-
# be invoked to retrieve a value. For example, `type` maps to `type` and
|
12
|
-
# `additionalItems` maps to `additional_items`.
|
13
|
-
@@schema_attrs = {}
|
14
|
-
|
15
|
-
# identical to attr_accessible, but allows us to copy in values from a
|
16
|
-
# target schema to help preserve our hierarchy during reference expansion
|
17
|
-
def self.attr_copyable(attr)
|
18
|
-
attr_accessor(attr)
|
19
|
-
@@copyable_attrs << "@#{attr}".to_sym
|
20
|
-
end
|
21
|
-
|
22
|
-
def self.attr_schema(attr, options = {})
|
23
|
-
attr_copyable(attr)
|
24
|
-
@@schema_attrs[options[:schema_name] || attr] = attr
|
25
|
-
end
|
26
|
-
|
27
|
-
def self.attr_reader_default(attr, default)
|
28
|
-
# remove the reader already created by attr_accessor
|
29
|
-
remove_method(attr)
|
30
|
-
|
31
|
-
class_eval("def #{attr} ; !@#{attr}.nil? ? @#{attr} : #{default} ; end")
|
32
|
-
end
|
5
|
+
include Attributes
|
33
6
|
|
34
7
|
def initialize
|
8
|
+
# nil out all our fields so that it's possible to instantiate a schema
|
9
|
+
# instance without going through the parser and validate against it
|
10
|
+
# without Ruby throwing warnings about uninitialized instance variables.
|
11
|
+
initialize_attrs
|
12
|
+
|
13
|
+
# Don't put this in as an attribute default. We require that this precise
|
14
|
+
# pointer gets copied between all clones of any given schema so that they
|
15
|
+
# all share exactly the same set.
|
35
16
|
@clones = Set.new
|
36
|
-
|
37
|
-
# nil out all our schema fields so that it's possible to instantiate a
|
38
|
-
# schema instance without going through the parser and validate against
|
39
|
-
# it without Ruby throwing warnings about uninitialized instance
|
40
|
-
# variables.
|
41
|
-
@@schema_attrs.each do |_, a|
|
42
|
-
send(:"#{a}=", nil)
|
43
|
-
end
|
44
17
|
end
|
45
18
|
|
46
19
|
# Fragment of a JSON Pointer that can help us build a pointer back to this
|
@@ -73,6 +46,9 @@ module JsonSchema
|
|
73
46
|
# initialized after the original. Used for JSON Reference expansion. The
|
74
47
|
# only copy not present in this set is the original Schema object.
|
75
48
|
#
|
49
|
+
# Note that this doesn't have a default option because we rely on the fact
|
50
|
+
# that the set is the *same object* between all clones of any given schema.
|
51
|
+
#
|
76
52
|
# Type: Set[Schema]
|
77
53
|
attr_copyable :clones
|
78
54
|
|
@@ -94,12 +70,13 @@ module JsonSchema
|
|
94
70
|
# Type: String
|
95
71
|
attr_schema :id
|
96
72
|
|
97
|
-
# Short title of the schema.
|
73
|
+
# Short title of the schema (or the hyper-schema link if this is one).
|
98
74
|
#
|
99
75
|
# Type: String
|
100
76
|
attr_schema :title
|
101
77
|
|
102
|
-
# More detailed description of the schema
|
78
|
+
# More detailed description of the schema (or the hyper-schema link if this
|
79
|
+
# is one).
|
103
80
|
#
|
104
81
|
# Type: String
|
105
82
|
attr_schema :description
|
@@ -117,19 +94,19 @@ module JsonSchema
|
|
117
94
|
# set of to be valid.
|
118
95
|
#
|
119
96
|
# Type: Array[Schema]
|
120
|
-
attr_schema :all_of, :schema_name => :allOf
|
97
|
+
attr_schema :all_of, :default => [], :schema_name => :allOf
|
121
98
|
|
122
99
|
# A collection of subschemas of which data must validate against any schema
|
123
100
|
# in the set to be be valid.
|
124
101
|
#
|
125
102
|
# Type: Array[Schema]
|
126
|
-
attr_schema :any_of, :schema_name => :anyOf
|
103
|
+
attr_schema :any_of, :default => [], :schema_name => :anyOf
|
127
104
|
|
128
105
|
# A collection of inlined subschemas. Standard convention is to subschemas
|
129
106
|
# here and reference them from elsewhere.
|
130
107
|
#
|
131
108
|
# Type: Hash[String => Schema]
|
132
|
-
attr_schema :definitions
|
109
|
+
attr_schema :definitions, :default => {}
|
133
110
|
|
134
111
|
# A collection of objects that must include the data for it to be valid.
|
135
112
|
#
|
@@ -140,7 +117,7 @@ module JsonSchema
|
|
140
117
|
# one of to be valid.
|
141
118
|
#
|
142
119
|
# Type: Array[Schema]
|
143
|
-
attr_schema :one_of, :schema_name => :oneOf
|
120
|
+
attr_schema :one_of, :default => [], :schema_name => :oneOf
|
144
121
|
|
145
122
|
# A subschema which data must not validate against to be valid.
|
146
123
|
#
|
@@ -152,10 +129,10 @@ module JsonSchema
|
|
152
129
|
# of strings.
|
153
130
|
#
|
154
131
|
# Type: Array[String]
|
155
|
-
attr_schema :type
|
132
|
+
attr_schema :type, :default => []
|
156
133
|
|
157
134
|
# validation: array
|
158
|
-
attr_schema :additional_items, :schema_name => :additionalItems
|
135
|
+
attr_schema :additional_items, :default => true, :schema_name => :additionalItems
|
159
136
|
attr_schema :items
|
160
137
|
attr_schema :max_items, :schema_name => :maxItems
|
161
138
|
attr_schema :min_items, :schema_name => :minItems
|
@@ -163,21 +140,21 @@ module JsonSchema
|
|
163
140
|
|
164
141
|
# validation: number/integer
|
165
142
|
attr_schema :max
|
166
|
-
attr_schema :max_exclusive, :schema_name => :maxExclusive
|
143
|
+
attr_schema :max_exclusive, :default => false, :schema_name => :maxExclusive
|
167
144
|
attr_schema :min
|
168
|
-
attr_schema :min_exclusive, :schema_name => :minExclusive
|
145
|
+
attr_schema :min_exclusive, :default => false, :schema_name => :minExclusive
|
169
146
|
attr_schema :multiple_of, :schema_name => :multipleOf
|
170
147
|
|
171
148
|
# validation: object
|
172
|
-
attr_schema :additional_properties, :schema_name => :additionalProperties
|
173
|
-
attr_schema :dependencies
|
149
|
+
attr_schema :additional_properties, :default => true, :schema_name => :additionalProperties
|
150
|
+
attr_schema :dependencies, :default => {}
|
174
151
|
attr_schema :max_properties, :schema_name => :maxProperties
|
175
152
|
attr_schema :min_properties, :schema_name => :minProperties
|
176
|
-
attr_schema :pattern_properties, :schema_name => :patternProperties
|
177
|
-
attr_schema :properties
|
153
|
+
attr_schema :pattern_properties, :default => {}, :schema_name => :patternProperties
|
154
|
+
attr_schema :properties, :default => {}
|
178
155
|
attr_schema :required
|
179
156
|
# warning: strictProperties is technically V5 spec (but I needed it now)
|
180
|
-
attr_schema :strict_properties, :schema_name => :strictProperties
|
157
|
+
attr_schema :strict_properties, :default => false, :schema_name => :strictProperties
|
181
158
|
|
182
159
|
# validation: string
|
183
160
|
attr_schema :format
|
@@ -186,29 +163,19 @@ module JsonSchema
|
|
186
163
|
attr_schema :pattern
|
187
164
|
|
188
165
|
# hyperschema
|
189
|
-
attr_schema :links
|
166
|
+
attr_schema :links, :default => []
|
190
167
|
attr_schema :media
|
191
168
|
attr_schema :path_start, :schema_name => :pathStart
|
192
169
|
attr_schema :read_only, :schema_name => :readOnly
|
193
170
|
|
194
|
-
#
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
attr_reader_default :definitions, {}
|
203
|
-
attr_reader_default :dependencies, {}
|
204
|
-
attr_reader_default :links, []
|
205
|
-
attr_reader_default :one_of, []
|
206
|
-
attr_reader_default :max_exclusive, false
|
207
|
-
attr_reader_default :min_exclusive, false
|
208
|
-
attr_reader_default :pattern_properties, {}
|
209
|
-
attr_reader_default :properties, {}
|
210
|
-
attr_reader_default :strict_properties, false
|
211
|
-
attr_reader_default :type, []
|
171
|
+
# hyperschema link attributes
|
172
|
+
attr_schema :enc_type, :default => "application/json"
|
173
|
+
attr_schema :href
|
174
|
+
attr_schema :media_type, :default => "application/json"
|
175
|
+
attr_schema :method
|
176
|
+
attr_schema :rel
|
177
|
+
attr_schema :schema
|
178
|
+
attr_schema :target_schema
|
212
179
|
|
213
180
|
# allow booleans to be access with question mark
|
214
181
|
alias :additional_items? :additional_items
|
@@ -218,29 +185,6 @@ module JsonSchema
|
|
218
185
|
alias :read_only? :read_only
|
219
186
|
alias :unique_items? :unique_items
|
220
187
|
|
221
|
-
# Allows the values of schema attributes to be accessed with a symbol or a
|
222
|
-
# string. So for example, the value of `schema.additional_items` could be
|
223
|
-
# procured with `schema[:additionalItems]`. This only works for attributes
|
224
|
-
# that are part of the JSON schema specification; other methods on the
|
225
|
-
# class are not available (e.g. `expanded`.)
|
226
|
-
#
|
227
|
-
# This is implemented so that `JsonPointer::Evaluator` can evaluate a
|
228
|
-
# reference on an sintance of this class (as well as plain JSON data).
|
229
|
-
def [](name)
|
230
|
-
name = name.to_sym
|
231
|
-
if @@schema_attrs.key?(name)
|
232
|
-
send(@@schema_attrs[name])
|
233
|
-
else
|
234
|
-
raise NoMethodError, "Schema does not respond to ##{name}"
|
235
|
-
end
|
236
|
-
end
|
237
|
-
|
238
|
-
def copy_from(schema)
|
239
|
-
@@copyable_attrs.each do |copyable|
|
240
|
-
instance_variable_set(copyable, schema.instance_variable_get(copyable))
|
241
|
-
end
|
242
|
-
end
|
243
|
-
|
244
188
|
def expand_references(options = {})
|
245
189
|
expander = ReferenceExpander.new
|
246
190
|
if expander.expand(self, options)
|
@@ -267,7 +211,7 @@ module JsonSchema
|
|
267
211
|
str
|
268
212
|
else
|
269
213
|
hash = {}
|
270
|
-
|
214
|
+
self.class.copyable_attrs.each do |copyable, _|
|
271
215
|
next if [:@clones, :@data, :@parent, :@uri].include?(copyable)
|
272
216
|
if value = instance_variable_get(copyable)
|
273
217
|
if value.is_a?(Array)
|
@@ -319,27 +263,8 @@ module JsonSchema
|
|
319
263
|
end
|
320
264
|
|
321
265
|
# Link subobject for a hyperschema.
|
322
|
-
class Link
|
323
|
-
|
324
|
-
|
325
|
-
# schema attributes
|
326
|
-
attr_accessor :description
|
327
|
-
attr_writer :enc_type
|
328
|
-
attr_accessor :href
|
329
|
-
attr_writer :media_type
|
330
|
-
attr_accessor :method
|
331
|
-
attr_accessor :rel
|
332
|
-
attr_accessor :schema
|
333
|
-
attr_accessor :target_schema
|
334
|
-
attr_accessor :title
|
335
|
-
|
336
|
-
def enc_type
|
337
|
-
@enc_type || "application/json"
|
338
|
-
end
|
339
|
-
|
340
|
-
def media_type
|
341
|
-
@media_type || "application/json"
|
342
|
-
end
|
266
|
+
class Link < Schema
|
267
|
+
inherit_attrs
|
343
268
|
end
|
344
269
|
|
345
270
|
# Media type subobject for a hyperschema.
|
@@ -409,8 +409,9 @@ module JsonSchema
|
|
409
409
|
|
410
410
|
num_valid = schema.one_of.count do |subschema|
|
411
411
|
current_sub_errors = []
|
412
|
+
valid = validate_data(subschema, data, current_sub_errors, path)
|
412
413
|
sub_errors << current_sub_errors
|
413
|
-
|
414
|
+
valid
|
414
415
|
end
|
415
416
|
|
416
417
|
return true if num_valid == 1
|
@@ -498,7 +499,7 @@ module JsonSchema
|
|
498
499
|
end
|
499
500
|
|
500
501
|
def validate_type(schema, data, errors, path)
|
501
|
-
return true if schema.type.empty?
|
502
|
+
return true if !schema.type || schema.type.empty?
|
502
503
|
valid_types = schema.type.map { |t| TYPE_MAP[t] }.flatten.compact
|
503
504
|
if valid_types.any? { |t| data.is_a?(t) }
|
504
505
|
true
|
data/lib/json_schema.rb
CHANGED
data/test/bin_test.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
#
|
4
|
+
# The purpose of this sets of tests is just to include our Ruby executables
|
5
|
+
# where possible so that we can get very basic sanity checks on their syntax
|
6
|
+
# (which is something that of course Ruby can't do by default).
|
7
|
+
#
|
8
|
+
# We can do this without actually executing them because they're gated by `if
|
9
|
+
# $0 == __FILE__` statements.
|
10
|
+
#
|
11
|
+
|
12
|
+
describe "executables in bin/" do
|
13
|
+
before do
|
14
|
+
@bin_dir = File.expand_path("../../bin", __FILE__)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "has roughly valid Ruby structure for validate-schema" do
|
18
|
+
load File.join(@bin_dir, "validate-schema")
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
require "json_schema"
|
4
|
+
|
5
|
+
describe JsonSchema::Attributes do
|
6
|
+
it "defines copyable attributes" do
|
7
|
+
obj = TestAttributes.new
|
8
|
+
obj.copyable = "foo"
|
9
|
+
assert_equal "foo", obj.copyable
|
10
|
+
assert_includes obj.class.copyable_attrs, :@copyable
|
11
|
+
end
|
12
|
+
|
13
|
+
it "defines schema attributes" do
|
14
|
+
obj = TestAttributes.new
|
15
|
+
obj.schema = "foo"
|
16
|
+
assert_equal "foo", obj.schema
|
17
|
+
assert_equal({:schema => :schema, :named => :schema_named},
|
18
|
+
obj.class.schema_attrs)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "defines attributes with default readers" do
|
22
|
+
obj = TestAttributes.new
|
23
|
+
assert_equal [], obj.copyable_default
|
24
|
+
|
25
|
+
assert_equal "application/json", obj.copyable_default_with_string
|
26
|
+
|
27
|
+
hash = obj.copyable_default_with_object
|
28
|
+
assert_equal({}, hash)
|
29
|
+
hash[:x] = 123
|
30
|
+
|
31
|
+
# This is a check to make sure that the new object is not the same object
|
32
|
+
# as the one that we just mutated above. When assigning defaults the module
|
33
|
+
# should dup any common data strcutures that it puts in here.
|
34
|
+
obj = TestAttributes.new
|
35
|
+
hash = obj.copyable_default_with_object
|
36
|
+
assert_equal({}, hash)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "inherits attributes when so instructed" do
|
40
|
+
obj = TestAttributesDescendant.new
|
41
|
+
assert_includes obj.class.copyable_attrs, :@copyable
|
42
|
+
end
|
43
|
+
|
44
|
+
it "allows schema attributes to be indexed but not others" do
|
45
|
+
obj = TestAttributes.new
|
46
|
+
|
47
|
+
obj.copyable = "non-schema"
|
48
|
+
obj.schema = "schema"
|
49
|
+
|
50
|
+
assert_raises NoMethodError do
|
51
|
+
assert_equal nil, obj[:copyable]
|
52
|
+
end
|
53
|
+
|
54
|
+
assert_equal "schema", obj[:schema]
|
55
|
+
end
|
56
|
+
|
57
|
+
it "copies attributes with #copy_from" do
|
58
|
+
obj = TestAttributes.new
|
59
|
+
|
60
|
+
obj.copyable = "copyable"
|
61
|
+
obj.schema = "schema"
|
62
|
+
|
63
|
+
obj2 = TestAttributes.new
|
64
|
+
obj2.copy_from(obj)
|
65
|
+
|
66
|
+
assert_equal "copyable", obj2.copyable
|
67
|
+
assert_equal "schema", obj2.schema
|
68
|
+
end
|
69
|
+
|
70
|
+
it "initializes attributes with #initialize_attrs" do
|
71
|
+
obj = TestAttributes.new
|
72
|
+
|
73
|
+
# should produce a nil value *without* a Ruby warning
|
74
|
+
assert_equal nil, obj.copyable
|
75
|
+
assert_equal nil, obj.schema
|
76
|
+
end
|
77
|
+
|
78
|
+
class TestAttributes
|
79
|
+
include JsonSchema::Attributes
|
80
|
+
|
81
|
+
def initialize
|
82
|
+
initialize_attrs
|
83
|
+
end
|
84
|
+
|
85
|
+
attr_copyable :copyable
|
86
|
+
|
87
|
+
attr_schema :schema
|
88
|
+
attr_schema :schema_named, :schema_name => :named
|
89
|
+
|
90
|
+
attr_copyable :copyable_default, :default => []
|
91
|
+
attr_copyable :copyable_default_with_string, :default => "application/json"
|
92
|
+
attr_copyable :copyable_default_with_object, :default => {}
|
93
|
+
end
|
94
|
+
|
95
|
+
class TestAttributesDescendant < TestAttributes
|
96
|
+
inherit_attrs
|
97
|
+
end
|
98
|
+
end
|
@@ -265,6 +265,18 @@ describe JsonSchema::ReferenceExpander do
|
|
265
265
|
assert schema.expanded?
|
266
266
|
end
|
267
267
|
|
268
|
+
it "expands a reference to a link" do
|
269
|
+
pointer("#/properties").merge!(
|
270
|
+
"link" => { "$ref" => "#/links/0" }
|
271
|
+
)
|
272
|
+
assert expand
|
273
|
+
|
274
|
+
referenced = @schema.links[0]
|
275
|
+
reference = @schema.properties["link"]
|
276
|
+
|
277
|
+
assert_equal reference.href, referenced.href
|
278
|
+
end
|
279
|
+
|
268
280
|
def error_messages
|
269
281
|
@expander.errors.map { |e| e.message }
|
270
282
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
if RUBY_VERSION >= '2.0.0'
|
2
|
+
require 'simplecov'
|
3
|
+
SimpleCov.start do
|
4
|
+
# We do our utmost to test our executables by modularizing them into
|
5
|
+
# testable pieces, but testing them to completion is nearly impossible as
|
6
|
+
# far as I can tell, so include them in our tests but don't calculate
|
7
|
+
# coverage.
|
8
|
+
add_filter "/bin/"
|
9
|
+
|
10
|
+
add_filter "/test/"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
1
14
|
require "minitest"
|
2
15
|
require "minitest/autorun"
|
3
16
|
#require "pry-rescue/minitest"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.14.
|
4
|
+
version: 0.14.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandur
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-10-
|
11
|
+
date: 2016-10-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ecma-re-validator
|
@@ -94,6 +94,20 @@ dependencies:
|
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
97
111
|
description:
|
98
112
|
email:
|
99
113
|
- brandur@mutelight.org
|
@@ -110,6 +124,7 @@ files:
|
|
110
124
|
- lib/json_pointer/evaluator.rb
|
111
125
|
- lib/json_reference.rb
|
112
126
|
- lib/json_schema.rb
|
127
|
+
- lib/json_schema/attributes.rb
|
113
128
|
- lib/json_schema/configuration.rb
|
114
129
|
- lib/json_schema/document_store.rb
|
115
130
|
- lib/json_schema/error.rb
|
@@ -120,10 +135,12 @@ files:
|
|
120
135
|
- schemas/hyper-schema.json
|
121
136
|
- schemas/open-api-schema.json
|
122
137
|
- schemas/schema.json
|
138
|
+
- test/bin_test.rb
|
123
139
|
- test/commands/validate_schema_test.rb
|
124
140
|
- test/data_scaffold.rb
|
125
141
|
- test/json_pointer/evaluator_test.rb
|
126
142
|
- test/json_reference/reference_test.rb
|
143
|
+
- test/json_schema/attribute_test.rb
|
127
144
|
- test/json_schema/document_store_test.rb
|
128
145
|
- test/json_schema/error_test.rb
|
129
146
|
- test/json_schema/parser_test.rb
|