json-schema 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -18,7 +18,7 @@ From the git repo:
18
18
 
19
19
  <pre>
20
20
  $ gem build json-schema.gemspec
21
- $ gem install json-schema-1.0.4.gem
21
+ $ gem install json-schema-1.0.5.gem
22
22
  </pre>
23
23
 
24
24
 
@@ -93,6 +93,54 @@ rescue JSON::Schema::ValidationError
93
93
  end
94
94
  </pre>
95
95
 
96
+
97
+ h3. Fully validate against a schema and catch all errors
98
+
99
+ <pre>
100
+ require 'rubygems'
101
+ require 'json-schema'
102
+
103
+ schema = {
104
+ "type" => "object",
105
+ "properties" => {
106
+ "a" => {"type" => "integer", "required" => true},
107
+ "b" => {"type" => "string", "required" => "true"}
108
+ }
109
+ }
110
+
111
+ data = {
112
+ "a" => "taco"
113
+ }
114
+
115
+ errors = JSON::Validator.fully_validate(schema, data)
116
+
117
+ # ["The property '#/a' of type String did not match the following type: integer in schema 03179a21-197e-5414-9611-e9f63e8324cd#", "The property '#/' did not contain a required property of 'b' in schema 03179a21-197e-5414-9611-e9f63e8324cd#"]
118
+ </pre>
119
+
120
+ h3. Fully validate against a schema and catch all errors as objects
121
+
122
+ <pre>
123
+ require 'rubygems'
124
+ require 'json-schema'
125
+
126
+ schema = {
127
+ "type" => "object",
128
+ "properties" => {
129
+ "a" => {"type" => "integer", "required" => true},
130
+ "b" => {"type" => "string", "required" => "true"}
131
+ }
132
+ }
133
+
134
+ data = {
135
+ "a" => "taco"
136
+ }
137
+
138
+ errors = JSON::Validator.fully_validate(schema, data, :errors_as_objects => true)
139
+
140
+ # [{:message=>"The property '#/a' of type String did not match the following type: integer in schema 03179a21-197e-5414-9611-e9f63e8324cd#", :schema=>#<URI::Generic:0x103a76198 URL:03179a21-197e-5414-9611-e9f63e8324cd#>, :failed_attribute=>"Type", :fragment=>"#/a"}, {:message=>"The property '#/' did not contain a required property of 'b' in schema 03179a21-197e-5414-9611-e9f63e8324cd#", :schema=>#<URI::Generic:0x103a76198 URL:03179a21-197e-5414-9611-e9f63e8324cd#>, :failed_attribute=>"Properties", :fragment=>"#/"}]
141
+
142
+ </pre>
143
+
96
144
  h3. Validate a JSON object against a JSON schema object, while also validating the schema itself
97
145
 
98
146
  <pre>
@@ -5,8 +5,61 @@ module JSON
5
5
  schemas = current_schema.schema['extends']
6
6
  schemas = [schemas] if !schemas.is_a?(Array)
7
7
  schemas.each do |s|
8
- schema = JSON::Schema.new(s,current_schema.uri,validator)
9
- schema.validate(data, fragments, options)
8
+ if s.is_a?(Hash)
9
+ schema = JSON::Schema.new(s,current_schema.uri,validator)
10
+ schema.validate(data, fragments, options)
11
+ elsif s.is_a?(String)
12
+ temp_uri = URI.parse(s)
13
+ if temp_uri.relative?
14
+ temp_uri = current_schema.uri.clone
15
+ # Check for absolute path
16
+ path = s.split("#")[0]
17
+ if path.nil? || path == ''
18
+ temp_uri.path = current_schema.uri.path
19
+ elsif path[0,1] == "/"
20
+ temp_uri.path = Pathname.new(path).cleanpath.to_s
21
+ else
22
+ temp_uri = current_schema.uri.merge(path)
23
+ end
24
+ temp_uri.fragment = s.split("#")[1]
25
+ end
26
+ temp_uri.fragment = "" if temp_uri.fragment.nil?
27
+
28
+ # Grab the parent schema from the schema list
29
+ schema_key = temp_uri.to_s.split("#")[0] + "#"
30
+
31
+ ref_schema = JSON::Validator.schemas[schema_key]
32
+
33
+ if ref_schema
34
+ # Perform fragment resolution to retrieve the appropriate level for the schema
35
+ target_schema = ref_schema.schema
36
+ fragments = temp_uri.fragment.split("/")
37
+ fragment_path = ''
38
+ fragments.each do |fragment|
39
+ if fragment && fragment != ''
40
+ if target_schema.is_a?(Array)
41
+ target_schema = target_schema[fragment.to_i]
42
+ else
43
+ target_schema = target_schema[fragment]
44
+ end
45
+ fragment_path = fragment_path + "/#{fragment}"
46
+ if target_schema.nil?
47
+ raise SchemaError.new("The fragment '#{fragment_path}' does not exist on schema #{ref_schema.uri.to_s}")
48
+ end
49
+ end
50
+ end
51
+
52
+ # We have the schema finally, build it and validate!
53
+ schema = JSON::Schema.new(target_schema,temp_uri,validator)
54
+ schema.validate(data, fragments, options)
55
+ else
56
+ message = "The extended schema '#{temp_uri.to_s}' cannot be found"
57
+ validation_error(message, fragments, current_schema, self, options[:record_errors])
58
+ end
59
+ else
60
+ message = "The property '#{build_fragment(fragments)}' was not a valid schema"
61
+ validation_error(message, fragments, current_schema, self, options[:record_errors])
62
+ end
10
63
  end
11
64
  end
12
65
  end
@@ -228,6 +228,9 @@ module JSON
228
228
  if parent_schema.schema["$ref"]
229
229
  load_ref_schema(parent_schema, parent_schema.schema["$ref"])
230
230
  end
231
+ if parent_schema.schema["extends"] && parent_schema.schema["extends"].is_a?(String)
232
+ load_ref_schema(parent_schema, parent_schema.schema["extends"])
233
+ end
231
234
 
232
235
  # Check for schemas in union types
233
236
  ["type", "disallow"].each do |key|
@@ -271,12 +274,14 @@ module JSON
271
274
 
272
275
  # Either load a reference schema or create a new schema
273
276
  def handle_schema(parent_schema, obj)
274
- schema_uri = parent_schema.uri.clone
275
- schema = JSON::Schema.new(obj,schema_uri,@options[:version])
276
- if obj['id']
277
- Validator.add_schema(schema)
277
+ if obj.is_a?(Hash)
278
+ schema_uri = parent_schema.uri.clone
279
+ schema = JSON::Schema.new(obj,schema_uri,@options[:version])
280
+ if obj['id']
281
+ Validator.add_schema(schema)
282
+ end
283
+ build_schemas(schema)
278
284
  end
279
- build_schemas(schema)
280
285
  end
281
286
 
282
287
 
metadata CHANGED
@@ -1,33 +1,24 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: json-schema
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 0
8
- - 4
9
- version: 1.0.4
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.5
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Kenny Hoxworth
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2012-02-14 00:00:00 -05:00
18
- default_executable:
12
+ date: 2012-02-21 00:00:00.000000000 Z
19
13
  dependencies: []
20
-
21
14
  description:
22
15
  email: hoxworth@gmail.com
23
16
  executables: []
24
-
25
17
  extensions: []
26
-
27
- extra_rdoc_files:
18
+ extra_rdoc_files:
28
19
  - README.textile
29
20
  - LICENSE.md
30
- files:
21
+ files:
31
22
  - lib/json-schema/attributes/additionalitems.rb
32
23
  - lib/json-schema/attributes/additionalproperties.rb
33
24
  - lib/json-schema/attributes/dependencies.rb
@@ -66,37 +57,42 @@ files:
66
57
  - resources/draft-03.json
67
58
  - README.textile
68
59
  - LICENSE.md
69
- has_rdoc: true
60
+ - test/test_extended_schema.rb
61
+ - test/test_files.rb
62
+ - test/test_full_validation.rb
63
+ - test/test_jsonschema_draft1.rb
64
+ - test/test_jsonschema_draft2.rb
65
+ - test/test_jsonschema_draft3.rb
66
+ - test/test_schema_validation.rb
67
+ - test/data/bad_data_1.json
68
+ - test/data/good_data_1.json
69
+ - test/schemas/good_schema_1.json
70
+ - test/schemas/good_schema_2.json
70
71
  homepage: http://github.com/hoxworth/json-schema/tree/master
71
72
  licenses: []
72
-
73
73
  post_install_message:
74
74
  rdoc_options: []
75
-
76
- require_paths:
75
+ require_paths:
77
76
  - lib
78
- required_ruby_version: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- segments:
83
- - 0
84
- version: "0"
85
- required_rubygems_version: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- segments:
90
- - 0
91
- version: "0"
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
92
89
  requirements: []
93
-
94
90
  rubyforge_project:
95
- rubygems_version: 1.3.6
91
+ rubygems_version: 1.8.10
96
92
  signing_key:
97
93
  specification_version: 3
98
94
  summary: Ruby JSON Schema Validator
99
- test_files:
95
+ test_files:
100
96
  - test/test_extended_schema.rb
101
97
  - test/test_files.rb
102
98
  - test/test_full_validation.rb