json-schema 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -4,7 +4,7 @@ This library is intended to provide Ruby with an interface for validating JSON o
4
4
 
5
5
  h2. Version 2.0.0 Upgrade Notes
6
6
 
7
- Please be aware that the upgrade to version 2.0.0 will use Draft-04 *by default*, so schemas that do not declare a validator using the `$schema` keyword will use Draft-04 now instead of Draft-03. This is the reason for the major version upgrade.
7
+ Please be aware that the upgrade to version 2.0.0 will use Draft-04 *by default*, so schemas that do not declare a validator using the <code>$schema</code> keyword will use Draft-04 now instead of Draft-03. This is the reason for the major version upgrade.
8
8
 
9
9
  h2. Dependencies
10
10
 
@@ -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.0.gem
25
+ $ gem install json-schema-2.0.1.gem
26
26
  </pre>
27
27
 
28
28
 
@@ -292,10 +292,6 @@ Optionally, the JSON Schema library supports using the MultiJSON library for sel
292
292
 
293
293
  h2. Notes
294
294
 
295
- The following core schema attributes are not implemented:
296
-
297
- * default - This library aims to solely be a validator and does not modify an object it is validating.
298
-
299
295
  The 'format' attribute is only validated for the following values:
300
296
 
301
297
  * date-time
@@ -1,174 +1,150 @@
1
1
  {
2
- "$schema": "http://json-schema.org/draft-03/schema#",
3
- "id": "http://json-schema.org/draft-03/schema#",
4
- "type": "object",
5
-
6
- "properties": {
7
- "type": {
8
- "type": [ "string", "array" ],
9
- "items": {
10
- "type": [ "string", { "$ref": "#" } ]
11
- },
12
- "uniqueItems": true,
13
- "default": "any"
14
- },
15
-
16
- "properties": {
17
- "type": "object",
18
- "additionalProperties": { "$ref": "#" },
19
- "default": {}
2
+ "id": "http://json-schema.org/draft-04/schema#",
3
+ "$schema": "http://json-schema.org/draft-04/schema#",
4
+ "description": "Core schema meta-schema",
5
+ "definitions": {
6
+ "schemaArray": {
7
+ "type": "array",
8
+ "minItems": 1,
9
+ "items": { "$ref": "#" }
20
10
  },
21
-
22
- "patternProperties": {
23
- "type": "object",
24
- "additionalProperties": { "$ref": "#" },
25
- "default": {}
11
+ "positiveInteger": {
12
+ "type": "integer",
13
+ "minimum": 0
26
14
  },
27
-
28
- "additionalProperties": {
29
- "type": [ { "$ref": "#" }, "boolean" ],
30
- "default": {}
15
+ "positiveIntegerDefault0": {
16
+ "allOf": [ { "$ref": "#/definitions/positiveInteger" }, { "default": 0 } ]
31
17
  },
32
-
33
- "items": {
34
- "type": [ { "$ref": "#" }, "array" ],
35
- "items": { "$ref": "#" },
36
- "default": {}
18
+ "simpleTypes": {
19
+ "enum": [ "array", "boolean", "integer", "null", "number", "object", "string" ]
37
20
  },
38
-
39
- "additionalItems": {
40
- "type": [ { "$ref": "#" }, "boolean" ],
41
- "default": {}
21
+ "stringArray": {
22
+ "type": "array",
23
+ "items": { "type": "string" },
24
+ "minItems": 1,
25
+ "uniqueItems": true
26
+ }
27
+ },
28
+ "type": "object",
29
+ "properties": {
30
+ "id": {
31
+ "type": "string",
32
+ "format": "uri"
42
33
  },
43
-
44
- "required": {
45
- "type": "boolean",
46
- "default": false
34
+ "$schema": {
35
+ "type": "string",
36
+ "format": "uri"
47
37
  },
48
-
49
- "dependencies": {
50
- "type": "object",
51
- "additionalProperties": {
52
- "type": [ "string", "array", { "$ref": "#" } ],
53
- "items": {
54
- "type": "string"
55
- }
56
- },
57
- "default": {}
38
+ "title": {
39
+ "type": "string"
58
40
  },
59
-
60
- "minimum": {
61
- "type": "number"
41
+ "description": {
42
+ "type": "string"
43
+ },
44
+ "default": {},
45
+ "multipleOf": {
46
+ "type": "number",
47
+ "minimum": 0,
48
+ "exclusiveMinimum": true
62
49
  },
63
-
64
50
  "maximum": {
65
51
  "type": "number"
66
52
  },
67
-
68
- "exclusiveMinimum": {
69
- "type": "boolean",
70
- "default": false
71
- },
72
-
73
53
  "exclusiveMaximum": {
74
54
  "type": "boolean",
75
55
  "default": false
76
56
  },
77
-
78
- "minItems": {
79
- "type": "integer",
80
- "minimum": 0,
81
- "default": 0
82
- },
83
-
84
- "maxItems": {
85
- "type": "integer",
86
- "minimum": 0
57
+ "minimum": {
58
+ "type": "number"
87
59
  },
88
-
89
- "uniqueItems": {
60
+ "exclusiveMinimum": {
90
61
  "type": "boolean",
91
62
  "default": false
92
63
  },
93
-
64
+ "maxLength": { "$ref": "#/definitions/positiveInteger" },
65
+ "minLength": { "$ref": "#/definitions/positiveIntegerDefault0" },
94
66
  "pattern": {
95
67
  "type": "string",
96
68
  "format": "regex"
97
69
  },
98
-
99
- "minLength": {
100
- "type": "integer",
101
- "minimum": 0,
102
- "default": 0
103
- },
104
-
105
- "maxLength": {
106
- "type": "integer"
107
- },
108
-
109
- "enum": {
110
- "type": "array",
111
- "minItems": 1,
112
- "uniqueItems": true
70
+ "additionalItems": {
71
+ "anyOf": [
72
+ { "type": "boolean" },
73
+ { "$ref": "#" }
74
+ ],
75
+ "default": {}
113
76
  },
114
-
115
- "default": {
116
- "type": "any"
77
+ "items": {
78
+ "anyOf": [
79
+ { "$ref": "#" },
80
+ { "$ref": "#/definitions/schemaArray" }
81
+ ],
82
+ "default": {}
117
83
  },
118
-
119
- "title": {
120
- "type": "string"
84
+ "maxItems": { "$ref": "#/definitions/positiveInteger" },
85
+ "minItems": { "$ref": "#/definitions/positiveIntegerDefault0" },
86
+ "uniqueItems": {
87
+ "type": "boolean",
88
+ "default": false
121
89
  },
122
-
123
- "description": {
124
- "type": "string"
90
+ "maxProperties": { "$ref": "#/definitions/positiveInteger" },
91
+ "minProperties": { "$ref": "#/definitions/positiveIntegerDefault0" },
92
+ "required": { "$ref": "#/definitions/stringArray" },
93
+ "additionalProperties": {
94
+ "anyOf": [
95
+ { "type": "boolean" },
96
+ { "$ref": "#" }
97
+ ],
98
+ "default": {}
125
99
  },
126
-
127
- "format": {
128
- "type": "string"
100
+ "definitions": {
101
+ "type": "object",
102
+ "additionalProperties": { "$ref": "#" },
103
+ "default": {}
129
104
  },
130
-
131
- "divisibleBy": {
132
- "type": "number",
133
- "minimum": 0,
134
- "exclusiveMinimum": true,
135
- "default": 1
136
- },
137
-
138
- "disallow": {
139
- "type": [ "string", "array" ],
140
- "items": {
141
- "type": [ "string", { "$ref": "#" } ]
142
- },
143
- "uniqueItems": true
105
+ "properties": {
106
+ "type": "object",
107
+ "additionalProperties": { "$ref": "#" },
108
+ "default": {}
144
109
  },
145
-
146
- "extends": {
147
- "type": [ { "$ref": "#" }, "array" ],
148
- "items": { "$ref": "#" },
110
+ "patternProperties": {
111
+ "type": "object",
112
+ "additionalProperties": { "$ref": "#" },
149
113
  "default": {}
150
114
  },
151
-
152
- "id": {
153
- "type": "string",
154
- "format": "uri"
115
+ "dependencies": {
116
+ "type": "object",
117
+ "additionalProperties": {
118
+ "anyOf": [
119
+ { "$ref": "#" },
120
+ { "$ref": "#/definitions/stringArray" }
121
+ ]
122
+ }
155
123
  },
156
-
157
- "$ref": {
158
- "type": "string",
159
- "format": "uri"
124
+ "enum": {
125
+ "type": "array",
126
+ "minItems": 1,
127
+ "uniqueItems": true
160
128
  },
161
-
162
- "$schema": {
163
- "type": "string",
164
- "format": "uri"
165
- }
129
+ "type": {
130
+ "anyOf": [
131
+ { "$ref": "#/definitions/simpleTypes" },
132
+ {
133
+ "type": "array",
134
+ "items": { "$ref": "#/definitions/simpleTypes" },
135
+ "minItems": 1,
136
+ "uniqueItems": true
137
+ }
138
+ ]
139
+ },
140
+ "allOf": { "$ref": "#/definitions/schemaArray" },
141
+ "anyOf": { "$ref": "#/definitions/schemaArray" },
142
+ "oneOf": { "$ref": "#/definitions/schemaArray" },
143
+ "not": { "$ref": "#" }
166
144
  },
167
-
168
145
  "dependencies": {
169
- "exclusiveMinimum": "minimum",
170
- "exclusiveMaximum": "maximum"
146
+ "exclusiveMaximum": [ "maximum" ],
147
+ "exclusiveMinimum": [ "minimum" ]
171
148
  },
172
-
173
149
  "default": {}
174
150
  }
@@ -2,7 +2,7 @@ require 'test/unit'
2
2
  require File.dirname(__FILE__) + '/../lib/json-schema'
3
3
 
4
4
  class JSONSchemaValidation < Test::Unit::TestCase
5
- def valid_schema
5
+ def valid_schema_v3
6
6
  {
7
7
  "$schema" => "http://json-schema.org/draft-03/schema#",
8
8
  "type" => "object",
@@ -14,7 +14,7 @@ class JSONSchemaValidation < Test::Unit::TestCase
14
14
  }
15
15
  end
16
16
 
17
- def invalid_schema
17
+ def invalid_schema_v3
18
18
  {
19
19
  "$schema" => "http://json-schema.org/draft-03/schema#",
20
20
  "type" => "object",
@@ -26,17 +26,53 @@ class JSONSchemaValidation < Test::Unit::TestCase
26
26
  }
27
27
  end
28
28
 
29
+ def valid_schema_v4
30
+ {
31
+ "$schema" => "http://json-schema.org/draft-04/schema#",
32
+ "type" => "object",
33
+ "required" => ["b"],
34
+ "properties" => {
35
+ }
36
+ }
37
+ end
38
+
39
+ def invalid_schema_v4
40
+ {
41
+ "$schema" => "http://json-schema.org/draft-04/schema#",
42
+ "type" => "object",
43
+ "required" => "b",
44
+ "properties" => {
45
+ }
46
+ }
47
+ end
48
+
29
49
  def test_draft03_validation
30
50
  data = {"b" => {"a" => 5}}
31
- assert(JSON::Validator.validate(valid_schema,data,:validate_schema => true))
32
- assert(!JSON::Validator.validate(invalid_schema,data,:validate_schema => true))
51
+ assert(JSON::Validator.validate(valid_schema_v3,data,:validate_schema => true, :version => :draft3))
52
+ assert(!JSON::Validator.validate(invalid_schema_v3,data,:validate_schema => true, :version => :draft3))
53
+ end
54
+
55
+ def test_validate_just_schema_draft03
56
+ errors = JSON::Validator.fully_validate_schema(valid_schema_v3, :version => :draft3)
57
+ assert_equal [], errors
58
+
59
+ errors = JSON::Validator.fully_validate_schema(invalid_schema_v3, :version => :draft3)
60
+ assert_equal 1, errors.size
61
+ assert_match /the property .*required.*did not match/i, errors.first
62
+ end
63
+
64
+
65
+ def test_draft04_validation
66
+ data = {"b" => {"a" => 5}}
67
+ assert(JSON::Validator.validate(valid_schema_v4,data,:validate_schema => true, :version => :draft4))
68
+ assert(!JSON::Validator.validate(invalid_schema_v4,data,:validate_schema => true, :version => :draft4))
33
69
  end
34
70
 
35
- def test_validate_just_schema
36
- errors = JSON::Validator.fully_validate_schema(valid_schema)
71
+ def test_validate_just_schema_draft04
72
+ errors = JSON::Validator.fully_validate_schema(valid_schema_v4, :version => :draft4)
37
73
  assert_equal [], errors
38
74
 
39
- errors = JSON::Validator.fully_validate_schema(invalid_schema)
75
+ errors = JSON::Validator.fully_validate_schema(invalid_schema_v4, :version => :draft4)
40
76
  assert_equal 1, errors.size
41
77
  assert_match /the property .*required.*did not match/i, errors.first
42
78
  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.0
4
+ version: 2.0.1
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-06-23 00:00:00.000000000 Z
12
+ date: 2013-06-25 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: hoxworth@gmail.com