json_schema 0.0.13 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/validate-schema +0 -1
- data/lib/commands/validate_schema.rb +29 -13
- data/lib/json_schema/parser.rb +6 -0
- data/lib/json_schema/reference_expander.rb +6 -1
- data/lib/json_schema/schema.rb +6 -0
- data/test/data_scaffold.rb +3 -0
- data/test/json_schema/parser_test.rb +5 -0
- data/test/json_schema/reference_expander_test.rb +7 -0
- metadata +2 -3
- data/schemas/heroku-hyper-schema.json +0 -96
data/bin/validate-schema
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require "json"
|
2
|
+
require "yaml"
|
1
3
|
require_relative "../json_schema"
|
2
4
|
|
3
5
|
module Commands
|
@@ -28,8 +30,9 @@ module Commands
|
|
28
30
|
return false if argv.count < 1
|
29
31
|
|
30
32
|
argv.each do |data_file|
|
31
|
-
|
32
|
-
|
33
|
+
if !(data = read_file(data_file))
|
34
|
+
return false
|
35
|
+
end
|
33
36
|
|
34
37
|
if detect
|
35
38
|
if !(schema_uri = data["$schema"])
|
@@ -57,15 +60,6 @@ module Commands
|
|
57
60
|
|
58
61
|
private
|
59
62
|
|
60
|
-
def check_file(file)
|
61
|
-
if !File.exists?(file)
|
62
|
-
@errors = ["#{file}: No such file or directory."]
|
63
|
-
false
|
64
|
-
else
|
65
|
-
true
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
63
|
def initialize_store
|
70
64
|
@store = JsonSchema::DocumentStore.new
|
71
65
|
extra_schemas.each do |extra_schema|
|
@@ -83,10 +77,12 @@ module Commands
|
|
83
77
|
end
|
84
78
|
|
85
79
|
def parse(file)
|
86
|
-
|
80
|
+
if !(schema_data = read_file(file))
|
81
|
+
return nil
|
82
|
+
end
|
87
83
|
|
88
84
|
parser = JsonSchema::Parser.new
|
89
|
-
if !(schema = parser.parse(
|
85
|
+
if !(schema = parser.parse(schema_data))
|
90
86
|
@errors = map_schema_errors(file, parser.errors)
|
91
87
|
return nil
|
92
88
|
end
|
@@ -99,5 +95,25 @@ module Commands
|
|
99
95
|
|
100
96
|
schema
|
101
97
|
end
|
98
|
+
|
99
|
+
def read_file(file)
|
100
|
+
contents = File.read(file)
|
101
|
+
if File.extname(file) == ".yaml"
|
102
|
+
YAML.load(contents)
|
103
|
+
else
|
104
|
+
JSON.load(contents)
|
105
|
+
end
|
106
|
+
rescue Errno::ENOENT
|
107
|
+
@errors = ["#{file}: No such file or directory."]
|
108
|
+
nil
|
109
|
+
rescue JSON::ParserError
|
110
|
+
# Ruby's parsing exceptions aren't too helpful, just point user to
|
111
|
+
# a better tool
|
112
|
+
@errors = ["#{file}: Invalid JSON. Try to validate using `jsonlint`."]
|
113
|
+
nil
|
114
|
+
rescue Psych::SyntaxError
|
115
|
+
@errors = ["#{file}: Invalid YAML."]
|
116
|
+
nil
|
117
|
+
end
|
102
118
|
end
|
103
119
|
end
|
data/lib/json_schema/parser.rb
CHANGED
@@ -170,6 +170,7 @@ module JsonSchema
|
|
170
170
|
link.parent = schema
|
171
171
|
|
172
172
|
link.description = l["description"]
|
173
|
+
link.enc_type = l["encType"]
|
173
174
|
link.href = l["href"]
|
174
175
|
link.method = l["method"] ? l["method"].downcase.to_sym : nil
|
175
176
|
link.rel = l["rel"]
|
@@ -179,6 +180,11 @@ module JsonSchema
|
|
179
180
|
link.schema = parse_data(l["schema"], schema, "links/#{i}/schema")
|
180
181
|
end
|
181
182
|
|
183
|
+
if l["targetSchema"]
|
184
|
+
link.target_schema =
|
185
|
+
parse_data(l["schema"], schema, "links/#{i}/targetSchema")
|
186
|
+
end
|
187
|
+
|
182
188
|
link
|
183
189
|
}
|
184
190
|
end
|
@@ -144,7 +144,6 @@ module JsonSchema
|
|
144
144
|
schema.any_of.each { |s| yielder << s }
|
145
145
|
schema.one_of.each { |s| yielder << s }
|
146
146
|
schema.definitions.each { |_, s| yielder << s }
|
147
|
-
schema.links.map { |l| l.schema }.compact.each { |s| yielder << s }
|
148
147
|
schema.pattern_properties.each { |_, s| yielder << s }
|
149
148
|
schema.properties.each { |_, s| yielder << s }
|
150
149
|
|
@@ -173,6 +172,12 @@ module JsonSchema
|
|
173
172
|
schema.dependencies.values.
|
174
173
|
select { |s| s.is_a?(Schema) }.
|
175
174
|
each { |s| yielder << s }
|
175
|
+
|
176
|
+
# schemas contained inside hyper-schema links objects
|
177
|
+
schema.links.map { |l| [l.schema, l.target_schema] }.
|
178
|
+
flatten.
|
179
|
+
compact.
|
180
|
+
each { |s| yielder << s }
|
176
181
|
end
|
177
182
|
end
|
178
183
|
|
data/lib/json_schema/schema.rb
CHANGED
@@ -283,11 +283,17 @@ module JsonSchema
|
|
283
283
|
|
284
284
|
# schema attributes
|
285
285
|
attr_accessor :description
|
286
|
+
attr_accessor :enc_type
|
286
287
|
attr_accessor :href
|
287
288
|
attr_accessor :method
|
288
289
|
attr_accessor :rel
|
289
290
|
attr_accessor :schema
|
291
|
+
attr_accessor :target_schema
|
290
292
|
attr_accessor :title
|
293
|
+
|
294
|
+
def enc_type
|
295
|
+
@enc_type || "application/json"
|
296
|
+
end
|
291
297
|
end
|
292
298
|
|
293
299
|
# Media type subobject for a hyperschema.
|
data/test/data_scaffold.rb
CHANGED
@@ -148,6 +148,7 @@ describe JsonSchema::Parser do
|
|
148
148
|
pointer("#/definitions/app").merge!(
|
149
149
|
"links" => [
|
150
150
|
"description" => "Create a new app.",
|
151
|
+
"encType" => "application/x-www-form-urlencoded",
|
151
152
|
"href" => "/apps",
|
152
153
|
"method" => "POST",
|
153
154
|
"rel" => "create",
|
@@ -157,6 +158,9 @@ describe JsonSchema::Parser do
|
|
157
158
|
"$ref" => "#/definitions/app/definitions/name"
|
158
159
|
},
|
159
160
|
}
|
161
|
+
},
|
162
|
+
"targetSchema" => {
|
163
|
+
"$ref" => "#/definitions/app"
|
160
164
|
}
|
161
165
|
]
|
162
166
|
)
|
@@ -164,6 +168,7 @@ describe JsonSchema::Parser do
|
|
164
168
|
link = schema.links[0]
|
165
169
|
assert_equal schema, link.parent
|
166
170
|
assert_equal "Create a new app.", link.description
|
171
|
+
assert_equal "application/x-www-form-urlencoded", link.enc_type
|
167
172
|
assert_equal "/apps", link.href
|
168
173
|
assert_equal :post, link.method
|
169
174
|
assert_equal "create", link.rel
|
@@ -116,6 +116,13 @@ describe JsonSchema::ReferenceExpander do
|
|
116
116
|
assert_equal ["string"], schema.type
|
117
117
|
end
|
118
118
|
|
119
|
+
it "will expand hyperschema link targetSchemas" do
|
120
|
+
expand
|
121
|
+
assert_equal [], errors
|
122
|
+
schema = @schema.properties["app"].links[0].target_schema.properties["name"]
|
123
|
+
assert_equal ["string"], schema.type
|
124
|
+
end
|
125
|
+
|
119
126
|
it "will perform multiple passes to resolve all references" do
|
120
127
|
pointer("#/properties").merge!(
|
121
128
|
"app0" => { "$ref" => "#/properties/app1" },
|
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: 0.0.
|
4
|
+
version: 0.0.14
|
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: 2014-05-
|
12
|
+
date: 2014-05-23 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email:
|
@@ -21,7 +21,6 @@ extra_rdoc_files: []
|
|
21
21
|
files:
|
22
22
|
- README.md
|
23
23
|
- bin/validate-schema
|
24
|
-
- schemas/heroku-hyper-schema.json
|
25
24
|
- schemas/hyper-schema.json
|
26
25
|
- schemas/schema.json
|
27
26
|
- lib/commands/validate_schema.rb
|
@@ -1,96 +0,0 @@
|
|
1
|
-
{
|
2
|
-
"$schema": "http://json-schema.org/draft-04/heroku-hyper-schema#",
|
3
|
-
"id": "http://json-schema.org/draft-04/heroku-hyper-schema#",
|
4
|
-
"title": "Heroku JSON Hyper-Schema",
|
5
|
-
"allOf": [
|
6
|
-
{
|
7
|
-
"$ref": "http://json-schema.org/draft-04/hyper-schema#"
|
8
|
-
}
|
9
|
-
],
|
10
|
-
"definitions": {
|
11
|
-
"entity": {
|
12
|
-
"anyOf": [
|
13
|
-
{
|
14
|
-
"$ref": "#/definitions/entityWithPatternProperties"
|
15
|
-
},
|
16
|
-
{
|
17
|
-
"$ref": "#/definitions/entityWithProperties"
|
18
|
-
}
|
19
|
-
]
|
20
|
-
},
|
21
|
-
"entityWithPatternProperties": {
|
22
|
-
"required": [
|
23
|
-
"definitions",
|
24
|
-
"description",
|
25
|
-
"id",
|
26
|
-
"links",
|
27
|
-
"patternProperties",
|
28
|
-
"title",
|
29
|
-
"type"
|
30
|
-
]
|
31
|
-
},
|
32
|
-
"entityWithProperties": {
|
33
|
-
"properties": {
|
34
|
-
"definitions": {
|
35
|
-
"additionalProperties": {
|
36
|
-
"$ref": "#/definitions/property"
|
37
|
-
},
|
38
|
-
"properties": {
|
39
|
-
"identity": {
|
40
|
-
"$ref": "#/definitions/identity"
|
41
|
-
}
|
42
|
-
},
|
43
|
-
"required": ["identity"]
|
44
|
-
}
|
45
|
-
},
|
46
|
-
"required": [
|
47
|
-
"definitions",
|
48
|
-
"description",
|
49
|
-
"id",
|
50
|
-
"links",
|
51
|
-
"properties",
|
52
|
-
"title",
|
53
|
-
"type"
|
54
|
-
]
|
55
|
-
},
|
56
|
-
"identity": {
|
57
|
-
"additionalProperties": false,
|
58
|
-
"properties": {
|
59
|
-
"anyOf": {
|
60
|
-
"additionalProperties": {
|
61
|
-
"$ref": "#/definitions/ref"
|
62
|
-
}
|
63
|
-
}
|
64
|
-
},
|
65
|
-
"required": ["anyOf"]
|
66
|
-
},
|
67
|
-
"property": {
|
68
|
-
"required": ["description", "type"]
|
69
|
-
},
|
70
|
-
"ref": {
|
71
|
-
"required": ["$ref"]
|
72
|
-
}
|
73
|
-
},
|
74
|
-
"properties": {
|
75
|
-
"definitions": {
|
76
|
-
"additionalProperties": {
|
77
|
-
"$ref": "#/definitions/entity"
|
78
|
-
}
|
79
|
-
},
|
80
|
-
"properties": {
|
81
|
-
"additionalProperties": {
|
82
|
-
"$ref": "#/definitions/ref"
|
83
|
-
}
|
84
|
-
}
|
85
|
-
},
|
86
|
-
"required": [
|
87
|
-
"$schema",
|
88
|
-
"definitions",
|
89
|
-
"description",
|
90
|
-
"id",
|
91
|
-
"links",
|
92
|
-
"properties",
|
93
|
-
"title",
|
94
|
-
"type"
|
95
|
-
]
|
96
|
-
}
|