json-schema 2.0.5 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -22,7 +22,7 @@ From the git repo:
22
22
 
23
23
  <pre>
24
24
  $ gem build json-schema.gemspec
25
- $ gem install json-schema-2.0.5.gem
25
+ $ gem install json-schema-2.1.0.gem
26
26
  </pre>
27
27
 
28
28
 
@@ -149,6 +149,34 @@ errors = JSON::Validator.fully_validate(schema, data, :errors_as_objects => true
149
149
 
150
150
  </pre>
151
151
 
152
+ h3. Validate against a fragment of a supplied schema
153
+
154
+ <pre>
155
+ require 'rubygems'
156
+ require 'json-schema'
157
+
158
+ schema = {
159
+ "type" => "object",
160
+ "required" => ["a","b"],
161
+ "properties" => {
162
+ "a" => {"type" => "integer"},
163
+ "b" => {"type" => "string"},
164
+ "c" => {
165
+ "type" => "object",
166
+ "properties" => {
167
+ "z" => {"type" => "integer"}
168
+ }
169
+ }
170
+ }
171
+ }
172
+
173
+ data = {
174
+ "z" => 1
175
+ }
176
+
177
+ JSON::Validator.validate(schema, data, :fragment => "#/properties/c")
178
+ </pre>
179
+
152
180
  h3. Validate a JSON object against a JSON schema object, while also validating the schema itself
153
181
 
154
182
  <pre>
@@ -170,8 +170,39 @@ module JSON
170
170
  @@mutex.synchronize { @base_schema = initialize_schema(schema_data) }
171
171
  @data = initialize_data(data)
172
172
  @@mutex.synchronize { build_schemas(@base_schema) }
173
+
174
+ # If the :fragment option is set, try and validate against the fragment
175
+ if opts[:fragment]
176
+ @base_schema = schema_from_fragment(@base_schema, opts[:fragment])
177
+ end
173
178
  end
174
179
 
180
+ def schema_from_fragment(base_schema, fragment)
181
+ fragments = fragment.split("/")
182
+
183
+ # ensure the first element was a hash, per the fragment spec
184
+ if fragments.shift != "#"
185
+ raise JSON::Schema::SchemaError.new("Invalid fragment syntax in :fragment option")
186
+ end
187
+
188
+ fragments.each do |f|
189
+ if base_schema.is_a?(Hash)
190
+ if !base_schema.has_key?(f)
191
+ raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option")
192
+ end
193
+ base_schema = base_schema[f]
194
+ elsif base_schema.is_a?(Array)
195
+ if base_schema[f.to_i].nil?
196
+ raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option")
197
+ end
198
+ base_schema = base_schema[f.to_i]
199
+ else
200
+ raise JSON::Schema::SchemaError.new("Invalid schema encountered when resolving :fragment option")
201
+ end
202
+ end
203
+
204
+ base_schema
205
+ end
175
206
 
176
207
  # Run a simple true/false validation of data against a schema
177
208
  def validate()
@@ -0,0 +1,30 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/json-schema'
3
+
4
+ class FragmentResolution < Test::Unit::TestCase
5
+ def test_fragment_resolution
6
+ schema = {
7
+ "$schema" => "http://json-schema.org/draft-04/schema#",
8
+ "properties" => {
9
+ "a" => {
10
+ "type" => "object",
11
+ "properties" => {
12
+ "b" => {"type" => "integer" }
13
+ }
14
+ }
15
+ }
16
+ }
17
+
18
+ data = {"b" => 5}
19
+ assert(!JSON::Validator.validate(schema,data))
20
+ assert(JSON::Validator.validate(schema,data,:fragment => "#/properties/a"))
21
+
22
+ assert_raise JSON::Schema::SchemaError do
23
+ JSON::Validator.validate!(schema,data,:fragment => "/properties/a")
24
+ end
25
+
26
+ assert_raise JSON::Schema::SchemaError do
27
+ JSON::Validator.validate!(schema,data,:fragment => "#/properties/b")
28
+ end
29
+ end
30
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.5
4
+ version: 2.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-01 00:00:00.000000000 Z
12
+ date: 2013-07-03 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: hoxworth@gmail.com
@@ -70,6 +70,7 @@ files:
70
70
  - resources/draft-04.json
71
71
  - README.textile
72
72
  - LICENSE.md
73
+ - test/schemas/test_fragment_resolution.rb
73
74
  - test/test_extended_schema.rb
74
75
  - test/test_extends_and_additionalProperties.rb
75
76
  - test/test_files_v3.rb
@@ -111,11 +112,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
112
  version: '0'
112
113
  requirements: []
113
114
  rubyforge_project:
114
- rubygems_version: 1.8.24
115
+ rubygems_version: 1.8.25
115
116
  signing_key:
116
117
  specification_version: 3
117
118
  summary: Ruby JSON Schema Validator
118
119
  test_files:
120
+ - test/schemas/test_fragment_resolution.rb
119
121
  - test/test_extended_schema.rb
120
122
  - test/test_extends_and_additionalProperties.rb
121
123
  - test/test_files_v3.rb