json-schema 0.9.8 → 0.9.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -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-0.9.8.gem
21
+ $ gem install json-schema-0.9.9.gem
22
22
  </pre>
23
23
 
24
24
 
@@ -93,6 +93,26 @@ rescue JSON::Schema::ValidationError
93
93
  end
94
94
  </pre>
95
95
 
96
+ h3. Validate a JSON object against a JSON schema object, while also validating the schema itself
97
+
98
+ <pre>
99
+ require 'rubygems'
100
+ require 'json-schema'
101
+
102
+ schema = {
103
+ "type" => "object",
104
+ "properties" => {
105
+ "a" => {"type" => "integer", "required" => "true"} # This will fail schema validation!
106
+ }
107
+ }
108
+
109
+ data = {
110
+ "a" => 5
111
+ }
112
+
113
+ JSON::Validator.validate(schema, data, :validate_schema => false)
114
+ </pre>
115
+
96
116
  h3. Validate an object against a JSON Schema Draft 2 schema
97
117
 
98
118
  <pre>
@@ -3,6 +3,6 @@ $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/json-schema"
3
3
  require 'rubygems'
4
4
  require 'schema'
5
5
  require 'validator'
6
- Dir[File.join(File.dirname(__FILE__), "json-schema/attributes/*")].each {|file| require file }
7
- Dir[File.join(File.dirname(__FILE__), "json-schema/validators/*")].each {|file| require file }
8
- require 'uri/file'
6
+ Dir[File.join(File.dirname(__FILE__), "json-schema/attributes/*.rb")].each {|file| require file }
7
+ Dir[File.join(File.dirname(__FILE__), "json-schema/validators/*.rb")].each {|file| require file }
8
+ require 'uri/file'
@@ -74,7 +74,8 @@ module JSON
74
74
  @@cache_schemas = false
75
75
  @@default_opts = {
76
76
  :list => false,
77
- :version => nil
77
+ :version => nil,
78
+ :validate_schema => false
78
79
  }
79
80
  @@validators = {}
80
81
  @@default_validator = nil
@@ -85,6 +86,7 @@ module JSON
85
86
  @options = @@default_opts.clone.merge(opts)
86
87
 
87
88
  # I'm not a fan of this, but it's quick and dirty to get it working for now
89
+ version_string = "draft-03"
88
90
  if @options[:version]
89
91
  @options[:version] = case @options[:version].to_s
90
92
  when "draft3"
@@ -96,10 +98,23 @@ module JSON
96
98
  else
97
99
  raise JSON::Schema::SchemaError.new("The requested JSON schema version is not supported")
98
100
  end
101
+ version_string = @options[:version]
99
102
  u = URI.parse("http://json-schema.org/#{@options[:version]}/schema#")
100
103
  validator = JSON::Validator.validators["#{u.scheme}://#{u.host}#{u.path}"]
101
104
  @options[:version] = validator
102
105
  end
106
+
107
+ # validate the schema, if requested
108
+ if @options[:validate_schema]
109
+ begin
110
+ metaschema_file = File.join(Pathname.new(File.dirname(__FILE__)).parent.parent, "resources", "#{version_string}.json").to_s
111
+ meta_validator = JSON::Validator.new(metaschema_file, schema_data)
112
+ meta_validator.validate
113
+ rescue JSON::Schema::ValidationError, JSON::Schema::SchemaError
114
+ raise $!
115
+ end
116
+ end
117
+
103
118
  @base_schema = initialize_schema(schema_data)
104
119
  @data = initialize_data(data)
105
120
  build_schemas(@base_schema)
@@ -0,0 +1,33 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/json-schema'
3
+
4
+ class JSONSchemaValidation < Test::Unit::TestCase
5
+
6
+ def test_draft03_validation
7
+ if JSON::Validator.json_backend != nil
8
+ data = {"b" => {"a" => 5}}
9
+ schema = {
10
+ "$schema" => "http://json-schema.org/draft-03/schema#",
11
+ "type" => "object",
12
+ "properties" => {
13
+ "b" => {
14
+ "required" => true
15
+ }
16
+ }
17
+ }
18
+
19
+ assert(JSON::Validator.validate(schema,data,:validate_schema => true))
20
+
21
+ schema = {
22
+ "$schema" => "http://json-schema.org/draft-03/schema#",
23
+ "type" => "object",
24
+ "properties" => {
25
+ "b" => {
26
+ "required" => "true"
27
+ }
28
+ }
29
+ }
30
+ assert(!JSON::Validator.validate(schema,data,:validate_schema => true))
31
+ end
32
+ end
33
+ end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-schema
3
3
  version: !ruby/object:Gem::Version
4
- hash: 43
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 9
9
- - 8
10
- version: 0.9.8
8
+ - 9
9
+ version: 0.9.9
11
10
  platform: ruby
12
11
  authors:
13
12
  - Kenny Hoxworth
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2011-07-13 00:00:00 -04:00
17
+ date: 2011-07-24 00:00:00 -04:00
19
18
  default_executable:
20
19
  dependencies: []
21
20
 
@@ -67,6 +66,7 @@ files:
67
66
  - test/test_jsonschema_draft1.rb
68
67
  - test/test_jsonschema_draft2.rb
69
68
  - test/test_jsonschema_draft3.rb
69
+ - test/test_schema_validation.rb
70
70
  has_rdoc: true
71
71
  homepage: http://github.com/hoxworth/json-schema/tree/master
72
72
  licenses: []
@@ -81,7 +81,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
81
81
  requirements:
82
82
  - - ">="
83
83
  - !ruby/object:Gem::Version
84
- hash: 3
85
84
  segments:
86
85
  - 0
87
86
  version: "0"
@@ -90,7 +89,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
89
  requirements:
91
90
  - - ">="
92
91
  - !ruby/object:Gem::Version
93
- hash: 3
94
92
  segments:
95
93
  - 0
96
94
  version: "0"
@@ -107,3 +105,4 @@ test_files:
107
105
  - test/test_jsonschema_draft1.rb
108
106
  - test/test_jsonschema_draft2.rb
109
107
  - test/test_jsonschema_draft3.rb
108
+ - test/test_schema_validation.rb