json_schema 0.13.6 → 0.14.0
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.
- checksums.yaml +4 -4
- data/lib/json_pointer/evaluator.rb +5 -0
- data/lib/json_schema/reference_expander.rb +5 -6
- data/lib/json_schema/schema.rb +71 -41
- data/test/json_pointer/evaluator_test.rb +8 -0
- data/test/json_schema/schema_test.rb +37 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c0bf452530b4b54246e04aae4fb36c02222c9631
|
4
|
+
data.tar.gz: e04f230e83d2da306a5c2462eda95a12f1b579cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0dd9c714bcbdb4d882c2e4a931aabacf1b57ed5cb5c0af409c18b1fcc5700d6ec821276a593175d6af8ca5a2cb91ef06c8a5e58cd40bc82056450210f2f5d883
|
7
|
+
data.tar.gz: a0aef6c2277dd70609a723aa8e23b8b955baf1f98538e98dd645c2530ddd1ddcac7da36b1ba13d258bb1f9a47f922355e5e6b76a441fada5cadafb5e46b3fb15
|
@@ -1,4 +1,9 @@
|
|
1
1
|
module JsonPointer
|
2
|
+
# Evaluates a JSON pointer within a JSON document.
|
3
|
+
#
|
4
|
+
# Note that this class is designed to evaluate references across a plain JSON
|
5
|
+
# data object _or_ an instance of `JsonSchema::Schema`, so the constructor's
|
6
|
+
# `data` argument can be of either type.
|
2
7
|
class Evaluator
|
3
8
|
def initialize(data)
|
4
9
|
@data = data
|
@@ -139,10 +139,10 @@ module JsonSchema
|
|
139
139
|
ref = ref_schema.reference
|
140
140
|
|
141
141
|
if !(new_schema = lookup_pointer(ref.uri, ref.pointer))
|
142
|
-
|
142
|
+
new_schema = JsonPointer.evaluate(resolved_schema, ref.pointer)
|
143
143
|
|
144
144
|
# couldn't resolve pointer within known schema; that's an error
|
145
|
-
if
|
145
|
+
if new_schema.nil?
|
146
146
|
message = %{Couldn't resolve pointer "#{ref.pointer}".}
|
147
147
|
@errors << SchemaError.new(resolved_schema, message, :unresolved_pointer)
|
148
148
|
return
|
@@ -153,14 +153,13 @@ module JsonSchema
|
|
153
153
|
#
|
154
154
|
# https://github.com/brandur/json_schema/issues/50
|
155
155
|
#
|
156
|
-
if new_schema
|
157
|
-
new_schema.
|
156
|
+
if new_schema.reference &&
|
157
|
+
new_new_schema = lookup_pointer(ref.uri, new_schema.reference.pointer)
|
158
|
+
new_new_schema.clones << ref_schema
|
158
159
|
else
|
159
160
|
# Parse a new schema and use the same parent node. Basically this is
|
160
161
|
# exclusively for the case of a reference that needs to be
|
161
162
|
# de-referenced again to be resolved.
|
162
|
-
# TODO: Fix to never parse.
|
163
|
-
new_schema = Parser.new.parse(data, ref_schema.parent)
|
164
163
|
build_schema_paths(ref.uri, resolved_schema)
|
165
164
|
end
|
166
165
|
else
|
data/lib/json_schema/schema.rb
CHANGED
@@ -2,13 +2,26 @@ require "json"
|
|
2
2
|
|
3
3
|
module JsonSchema
|
4
4
|
class Schema
|
5
|
-
@@
|
5
|
+
@@copyable_attrs = []
|
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 = {}
|
6
14
|
|
7
15
|
# identical to attr_accessible, but allows us to copy in values from a
|
8
16
|
# target schema to help preserve our hierarchy during reference expansion
|
9
17
|
def self.attr_copyable(attr)
|
10
18
|
attr_accessor(attr)
|
11
|
-
@@
|
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
|
12
25
|
end
|
13
26
|
|
14
27
|
def self.attr_reader_default(attr, default)
|
@@ -71,22 +84,22 @@ module JsonSchema
|
|
71
84
|
# the parent, and absolute URI's will replace it.
|
72
85
|
#
|
73
86
|
# Type: String
|
74
|
-
|
87
|
+
attr_schema :id
|
75
88
|
|
76
89
|
# Short title of the schema.
|
77
90
|
#
|
78
91
|
# Type: String
|
79
|
-
|
92
|
+
attr_schema :title
|
80
93
|
|
81
94
|
# More detailed description of the schema.
|
82
95
|
#
|
83
96
|
# Type: String
|
84
|
-
|
97
|
+
attr_schema :description
|
85
98
|
|
86
99
|
# Default JSON value for this particular schema
|
87
100
|
#
|
88
101
|
# Type: [any]
|
89
|
-
|
102
|
+
attr_schema :default
|
90
103
|
|
91
104
|
#
|
92
105
|
# Validation: Any
|
@@ -96,79 +109,79 @@ module JsonSchema
|
|
96
109
|
# set of to be valid.
|
97
110
|
#
|
98
111
|
# Type: Array[Schema]
|
99
|
-
|
112
|
+
attr_schema :all_of, :schema_name => :allOf
|
100
113
|
|
101
114
|
# A collection of subschemas of which data must validate against any schema
|
102
115
|
# in the set to be be valid.
|
103
116
|
#
|
104
117
|
# Type: Array[Schema]
|
105
|
-
|
118
|
+
attr_schema :any_of, :schema_name => :anyOf
|
106
119
|
|
107
120
|
# A collection of inlined subschemas. Standard convention is to subschemas
|
108
121
|
# here and reference them from elsewhere.
|
109
122
|
#
|
110
123
|
# Type: Hash[String => Schema]
|
111
|
-
|
124
|
+
attr_schema :definitions
|
112
125
|
|
113
126
|
# A collection of objects that must include the data for it to be valid.
|
114
127
|
#
|
115
128
|
# Type: Array
|
116
|
-
|
129
|
+
attr_schema :enum
|
117
130
|
|
118
131
|
# A collection of subschemas of which data must validate against exactly
|
119
132
|
# one of to be valid.
|
120
133
|
#
|
121
134
|
# Type: Array[Schema]
|
122
|
-
|
135
|
+
attr_schema :one_of, :schema_name => :oneOf
|
123
136
|
|
124
137
|
# A subschema which data must not validate against to be valid.
|
125
138
|
#
|
126
139
|
# Type: Schema
|
127
|
-
|
140
|
+
attr_schema :not
|
128
141
|
|
129
142
|
# An array of types that data is allowed to be. The spec allows this to be
|
130
143
|
# a string as well, but the parser will always normalize this to an array
|
131
144
|
# of strings.
|
132
145
|
#
|
133
146
|
# Type: Array[String]
|
134
|
-
|
147
|
+
attr_schema :type
|
135
148
|
|
136
149
|
# validation: array
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
150
|
+
attr_schema :additional_items, :schema_name => :additionalItems
|
151
|
+
attr_schema :items
|
152
|
+
attr_schema :max_items, :schema_name => :maxItems
|
153
|
+
attr_schema :min_items, :schema_name => :minItems
|
154
|
+
attr_schema :unique_items, :schema_name => :uniqueItems
|
142
155
|
|
143
156
|
# validation: number/integer
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
157
|
+
attr_schema :max
|
158
|
+
attr_schema :max_exclusive, :schema_name => :maxExclusive
|
159
|
+
attr_schema :min
|
160
|
+
attr_schema :min_exclusive, :schema_name => :minExclusive
|
161
|
+
attr_schema :multiple_of, :schema_name => :multipleOf
|
149
162
|
|
150
163
|
# validation: object
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
164
|
+
attr_schema :additional_properties, :schema_name => :additionalProperties
|
165
|
+
attr_schema :dependencies
|
166
|
+
attr_schema :max_properties, :schema_name => :maxProperties
|
167
|
+
attr_schema :min_properties, :schema_name => :minProperties
|
168
|
+
attr_schema :pattern_properties, :schema_name => :patternProperties
|
169
|
+
attr_schema :properties
|
170
|
+
attr_schema :required
|
158
171
|
# warning: strictProperties is technically V5 spec (but I needed it now)
|
159
|
-
|
172
|
+
attr_schema :strict_properties, :schema_name => :strictProperties
|
160
173
|
|
161
174
|
# validation: string
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
175
|
+
attr_schema :format
|
176
|
+
attr_schema :max_length, :schema_name => :maxLength
|
177
|
+
attr_schema :min_length, :schema_name => :minLength
|
178
|
+
attr_schema :pattern
|
166
179
|
|
167
180
|
# hyperschema
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
181
|
+
attr_schema :links
|
182
|
+
attr_schema :media
|
183
|
+
attr_schema :path_start, :schema_name => :pathStart
|
184
|
+
attr_schema :read_only, :schema_name => :readOnly
|
172
185
|
|
173
186
|
# Give these properties reader defaults for particular behavior so that we
|
174
187
|
# can preserve the `nil` nature of their instance variables. Knowing that
|
@@ -197,8 +210,25 @@ module JsonSchema
|
|
197
210
|
alias :read_only? :read_only
|
198
211
|
alias :unique_items? :unique_items
|
199
212
|
|
213
|
+
# Allows the values of schema attributes to be accessed with a symbol or a
|
214
|
+
# string. So for example, the value of `schema.additional_items` could be
|
215
|
+
# procured with `schema[:additionalItems]`. This only works for attributes
|
216
|
+
# that are part of the JSON schema specification; other methods on the
|
217
|
+
# class are not available (e.g. `expanded`.)
|
218
|
+
#
|
219
|
+
# This is implemented so that `JsonPointer::Evaluator` can evaluate a
|
220
|
+
# reference on an sintance of this class (as well as plain JSON data).
|
221
|
+
def [](name)
|
222
|
+
name = name.to_sym
|
223
|
+
if @@schema_attrs.key?(name)
|
224
|
+
send(@@schema_attrs[name])
|
225
|
+
else
|
226
|
+
raise NoMethodError, "Schema does not respond to ##{name}"
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
200
230
|
def copy_from(schema)
|
201
|
-
@@
|
231
|
+
@@copyable_attrs.each do |copyable|
|
202
232
|
instance_variable_set(copyable, schema.instance_variable_get(copyable))
|
203
233
|
end
|
204
234
|
end
|
@@ -229,7 +259,7 @@ module JsonSchema
|
|
229
259
|
str
|
230
260
|
else
|
231
261
|
hash = {}
|
232
|
-
@@
|
262
|
+
@@copyable_attrs.each do |copyable|
|
233
263
|
next if [:@clones, :@data, :@parent, :@uri].include?(copyable)
|
234
264
|
if value = instance_variable_get(copyable)
|
235
265
|
if value.is_a?(Array)
|
@@ -43,6 +43,14 @@ describe JsonPointer::Evaluator do
|
|
43
43
|
e.message
|
44
44
|
end
|
45
45
|
|
46
|
+
it "can evaluate on a schema object" do
|
47
|
+
schema = JsonSchema.parse!(DataScaffold.schema_sample)
|
48
|
+
evaluator = JsonPointer::Evaluator.new(schema)
|
49
|
+
res = evaluator.evaluate("#/definitions/app/definitions/contrived/allOf/0")
|
50
|
+
assert_kind_of JsonSchema::Schema, res
|
51
|
+
assert 30, res.max_length
|
52
|
+
end
|
53
|
+
|
46
54
|
def data
|
47
55
|
{
|
48
56
|
"foo" => ["bar", "baz"],
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
require "json_schema"
|
4
|
+
|
5
|
+
describe JsonSchema::Schema do
|
6
|
+
it "allows schema attribute access with #[]" do
|
7
|
+
schema = JsonSchema::Schema.new
|
8
|
+
schema.properties = { "foo" => nil }
|
9
|
+
assert_equal({ "foo" => nil }, schema[:properties])
|
10
|
+
end
|
11
|
+
|
12
|
+
it "allows schema attribute access with #[] and overridden name" do
|
13
|
+
schema = JsonSchema::Schema.new
|
14
|
+
schema.additional_properties = { "foo" => nil }
|
15
|
+
assert_equal({ "foo" => nil }, schema[:additionalProperties])
|
16
|
+
end
|
17
|
+
|
18
|
+
it "allows schema attribute access with #[] as string" do
|
19
|
+
schema = JsonSchema::Schema.new
|
20
|
+
schema.properties = { "foo" => nil }
|
21
|
+
assert_equal({ "foo" => nil }, schema["properties"])
|
22
|
+
end
|
23
|
+
|
24
|
+
it "raises if attempting to access #[] with bad method" do
|
25
|
+
schema = JsonSchema::Schema.new
|
26
|
+
assert_raises NoMethodError do
|
27
|
+
schema[:wat]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
it "raises if attempting to access #[] with non-schema attribute" do
|
32
|
+
schema = JsonSchema::Schema.new
|
33
|
+
assert_raises NoMethodError do
|
34
|
+
schema[:expanded]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
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.
|
4
|
+
version: 0.14.0
|
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-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ecma-re-validator
|
@@ -128,6 +128,7 @@ files:
|
|
128
128
|
- test/json_schema/error_test.rb
|
129
129
|
- test/json_schema/parser_test.rb
|
130
130
|
- test/json_schema/reference_expander_test.rb
|
131
|
+
- test/json_schema/schema_test.rb
|
131
132
|
- test/json_schema/validator_test.rb
|
132
133
|
- test/json_schema_test.rb
|
133
134
|
- test/test_helper.rb
|