jsonapi-validator 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f655bd883eca1ba6402906598a55a3d0561e47f3
4
+ data.tar.gz: 4eea1ca51ce5ea131ec786fa8a0ac0e1859de1a2
5
+ SHA512:
6
+ metadata.gz: d89f222156e0d49682b4aab277d9bf0545fa42578a04acc2320e5c05466d14856fd8b4602106512ced213850325f55aaab9e07334a045c4e12cdfee741f50c29
7
+ data.tar.gz: 9a104dd06f078ecbe3788028074c777272e7863804c3edaaddab1479c82212bf1680cbb93ff740c4ec9dd687eb664bc4145d29c8b867d86e0565c99a3dd4537a
@@ -0,0 +1,9 @@
1
+ require 'json'
2
+ require 'json-schema'
3
+ require_relative 'jsonapi/schema'
4
+ require_relative 'tasks/jsonapi-validator'
5
+ require_relative 'tasks/minitest'
6
+
7
+ STANDARD_SCHEMA = JsonApi::Schema.instance
8
+
9
+ require_relative 'jsonapi/validator'
@@ -0,0 +1,16 @@
1
+ module JsonApi
2
+ class Schema
3
+
4
+ JSON_API_SCHEMA_FILE = File.join(File.dirname(__FILE__), '../../schema/schema.json')
5
+
6
+ def self.instance
7
+ if @instance.nil?
8
+ File.open(JSON_API_SCHEMA_FILE,'r') do |f|
9
+ @instance = JSON.parse(f.read)
10
+ end
11
+ end
12
+ return @instance
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ module JsonApi
2
+ class Validator
3
+
4
+ def self.json_file(file)
5
+ object = nil
6
+ File.open(file) do |f|
7
+ object = JSON.parse(f.read)
8
+ end
9
+ return json(object)
10
+ end
11
+
12
+ def self.json(object)
13
+ errors = JSON::Validator.fully_validate(STANDARD_SCHEMA,object)
14
+ return errors
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,25 @@
1
+ namespace "jsonapi" do
2
+
3
+ desc "Task which loads all the files from spec/apis folder and validates with schema"
4
+ task "validate" do
5
+ failed = false
6
+
7
+ json_spec_files = Dir["#{Rails.root}/spec/apis/**/*.json"]
8
+ json_spec_files.each do |spec|
9
+ print "\n\nValidating file '#{spec}': "
10
+ result = JsonApi::Validator.json_file(spec)
11
+ if result.nil? or result.empty?
12
+ puts " PASSED\n"
13
+ else
14
+ puts " FAILED\n"
15
+ puts result
16
+ failed = true
17
+ end
18
+
19
+ puts "----------------------------\n\n"
20
+ end
21
+
22
+ raise "Please fix the above validation errors" if failed
23
+ end
24
+
25
+ end
@@ -0,0 +1,5 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.pattern = "test/test_*.rb"
5
+ end
@@ -0,0 +1,370 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-04/schema#",
3
+ "title": "JSON API Schema",
4
+ "description": "This is a schema for responses in the JSON API format. For more, see http://jsonapi.org",
5
+ "oneOf": [
6
+ {
7
+ "$ref": "#/definitions/success"
8
+ },
9
+ {
10
+ "$ref": "#/definitions/failure"
11
+ },
12
+ {
13
+ "$ref": "#/definitions/info"
14
+ }
15
+ ],
16
+
17
+ "definitions": {
18
+ "success": {
19
+ "type": "object",
20
+ "required": [
21
+ "data"
22
+ ],
23
+ "properties": {
24
+ "data": {
25
+ "$ref": "#/definitions/data"
26
+ },
27
+ "included": {
28
+ "description": "To reduce the number of HTTP requests, servers **MAY** allow responses that include related resources along with the requested primary resources. Such responses are called \"compound documents\".",
29
+ "type": "array",
30
+ "items": {
31
+ "$ref": "#/definitions/resource"
32
+ },
33
+ "uniqueItems": true
34
+ },
35
+ "meta": {
36
+ "$ref": "#/definitions/meta"
37
+ },
38
+ "links": {
39
+ "description": "Link members related to the primary data.",
40
+ "allOf": [
41
+ {
42
+ "$ref": "#/definitions/links"
43
+ },
44
+ {
45
+ "$ref": "#/definitions/pagination"
46
+ }
47
+ ]
48
+ },
49
+ "jsonapi": {
50
+ "$ref": "#/definitions/jsonapi"
51
+ }
52
+ },
53
+ "additionalProperties": false
54
+ },
55
+ "failure": {
56
+ "type": "object",
57
+ "required": [
58
+ "errors"
59
+ ],
60
+ "properties": {
61
+ "errors": {
62
+ "type": "array",
63
+ "items": {
64
+ "$ref": "#/definitions/error"
65
+ },
66
+ "uniqueItems": true
67
+ },
68
+ "meta": {
69
+ "$ref": "#/definitions/meta"
70
+ },
71
+ "jsonapi": {
72
+ "$ref": "#/definitions/jsonapi"
73
+ }
74
+ },
75
+ "additionalProperties": false
76
+ },
77
+ "info": {
78
+ "type": "object",
79
+ "required": [
80
+ "meta"
81
+ ],
82
+ "properties": {
83
+ "meta": {
84
+ "$ref": "#/definitions/meta"
85
+ },
86
+ "links": {
87
+ "$ref": "#/definitions/links"
88
+ },
89
+ "jsonapi": {
90
+ "$ref": "#/definitions/jsonapi"
91
+ }
92
+ },
93
+ "additionalProperties": false
94
+ },
95
+
96
+ "meta": {
97
+ "description": "Non-standard meta-information that can not be represented as an attribute or relationship.",
98
+ "type": "object",
99
+ "additionalProperties": true
100
+ },
101
+ "data": {
102
+ "description": "The document's \"primary data\" is a representation of the resource or collection of resources targeted by a request.",
103
+ "oneOf": [
104
+ {
105
+ "$ref": "#/definitions/resource"
106
+ },
107
+ {
108
+ "description": "An array of resource objects, an array of resource identifier objects, or an empty array ([]), for requests that target resource collections.",
109
+ "type": "array",
110
+ "items": {
111
+ "$ref": "#/definitions/resource"
112
+ },
113
+ "uniqueItems": true
114
+ },
115
+ {
116
+ "description": "null if the request is one that might correspond to a single resource, but doesn't currently.",
117
+ "type": "null"
118
+ }
119
+ ]
120
+ },
121
+ "resource": {
122
+ "description": "\"Resource objects\" appear in a JSON API document to represent resources.",
123
+ "type": "object",
124
+ "required": [
125
+ "type",
126
+ "id"
127
+ ],
128
+ "properties": {
129
+ "type": {
130
+ "type": "string"
131
+ },
132
+ "id": {
133
+ "type": "string"
134
+ },
135
+ "attributes": {
136
+ "$ref": "#/definitions/attributes"
137
+ },
138
+ "relationships": {
139
+ "$ref": "#/definitions/relationships"
140
+ },
141
+ "links": {
142
+ "$ref": "#/definitions/links"
143
+ },
144
+ "meta": {
145
+ "$ref": "#/definitions/meta"
146
+ }
147
+ },
148
+ "additionalProperties": false
149
+ },
150
+
151
+ "links": {
152
+ "description": "A resource object **MAY** contain references to other resource objects (\"relationships\"). Relationships may be to-one or to-many. Relationships can be specified by including a member in a resource's links object.",
153
+ "type": "object",
154
+ "properties": {
155
+ "self": {
156
+ "description": "A `self` member, whose value is a URL for the relationship itself (a \"relationship URL\"). This URL allows the client to directly manipulate the relationship. For example, it would allow a client to remove an `author` from an `article` without deleting the people resource itself.",
157
+ "type": "string",
158
+ "format": "uri"
159
+ },
160
+ "related": {
161
+ "$ref": "#/definitions/link"
162
+ }
163
+ },
164
+ "additionalProperties": true
165
+ },
166
+ "link": {
167
+ "description": "A link **MUST** be represented as either: a string containing the link's URL or a link object.",
168
+ "oneOf": [
169
+ {
170
+ "description": "A string containing the link's URL.",
171
+ "type": "string",
172
+ "format": "uri"
173
+ },
174
+ {
175
+ "type": "object",
176
+ "required": [
177
+ "href"
178
+ ],
179
+ "properties": {
180
+ "href": {
181
+ "description": "A string containing the link's URL.",
182
+ "type": "string",
183
+ "format": "uri"
184
+ },
185
+ "meta": {
186
+ "$ref": "#/definitions/meta"
187
+ }
188
+ }
189
+ }
190
+ ]
191
+ },
192
+
193
+ "attributes": {
194
+ "description": "Members of the attributes object (\"attributes\") represent information about the resource object in which it's defined.",
195
+ "type": "object",
196
+ "patternProperties": {
197
+ "^(?!relationships$|links$)\\w[-\\w_]*$": {
198
+ "description": "Attributes may contain any valid JSON value."
199
+ }
200
+ },
201
+ "additionalProperties": false
202
+ },
203
+
204
+ "relationships": {
205
+ "description": "Members of the relationships object (\"relationships\") represent references from the resource object in which it's defined to other resource objects.",
206
+ "type": "object",
207
+ "patternProperties": {
208
+ "^\\w[-\\w_]*$": {
209
+ "properties": {
210
+ "links": {
211
+ "$ref": "#/definitions/links"
212
+ },
213
+ "data": {
214
+ "description": "Member, whose value represents \"resource linkage\".",
215
+ "oneOf": [
216
+ {
217
+ "$ref": "#/definitions/relationshipToOne"
218
+ },
219
+ {
220
+ "$ref": "#/definitions/relationshipToMany"
221
+ }
222
+ ]
223
+ },
224
+ "meta": {
225
+ "$ref": "#/definitions/meta"
226
+ }
227
+ },
228
+ "additionalProperties": false
229
+ }
230
+ },
231
+ "additionalProperties": false
232
+ },
233
+ "relationshipToOne": {
234
+ "description": "References to other resource objects in a to-one (\"relationship\"). Relationships can be specified by including a member in a resource's links object.",
235
+ "anyOf": [
236
+ {
237
+ "$ref": "#/definitions/empty"
238
+ },
239
+ {
240
+ "$ref": "#/definitions/linkage"
241
+ }
242
+ ]
243
+ },
244
+ "relationshipToMany": {
245
+ "description": "An array of objects each containing \"type\" and \"id\" members for to-many relationships.",
246
+ "type": "array",
247
+ "items": {
248
+ "$ref": "#/definitions/linkage"
249
+ },
250
+ "uniqueItems": true
251
+ },
252
+ "empty": {
253
+ "description": "Describes an empty to-one relationship.",
254
+ "type": "null"
255
+ },
256
+ "linkage": {
257
+ "description": "The \"type\" and \"id\" to non-empty members.",
258
+ "type": "object",
259
+ "required": [
260
+ "type",
261
+ "id"
262
+ ],
263
+ "properties": {
264
+ "type": {
265
+ "type": "string"
266
+ },
267
+ "id": {
268
+ "type": "string"
269
+ },
270
+ "meta": {
271
+ "$ref": "#/definitions/meta"
272
+ }
273
+ },
274
+ "additionalProperties": false
275
+ },
276
+ "pagination": {
277
+ "type": "object",
278
+ "properties": {
279
+ "first": {
280
+ "description": "The first page of data",
281
+ "oneOf": [
282
+ { "type": "string", "format": "uri" },
283
+ { "type": "null" }
284
+ ]
285
+ },
286
+ "last": {
287
+ "description": "The last page of data",
288
+ "oneOf": [
289
+ { "type": "string", "format": "uri" },
290
+ { "type": "null" }
291
+ ]
292
+ },
293
+ "prev": {
294
+ "description": "The previous page of data",
295
+ "oneOf": [
296
+ { "type": "string", "format": "uri" },
297
+ { "type": "null" }
298
+ ]
299
+ },
300
+ "next": {
301
+ "description": "The next page of data",
302
+ "oneOf": [
303
+ { "type": "string", "format": "uri" },
304
+ { "type": "null" }
305
+ ]
306
+ }
307
+ }
308
+ },
309
+
310
+ "jsonapi": {
311
+ "description": "An object describing the server's implementation",
312
+ "type": "object",
313
+ "properties": {
314
+ "version": {
315
+ "type": "string"
316
+ },
317
+ "meta": {
318
+ "$ref": "#/definitions/meta"
319
+ }
320
+ },
321
+ "additionalProperties": false
322
+ },
323
+
324
+ "error": {
325
+ "type": "object",
326
+ "properties": {
327
+ "id": {
328
+ "description": "A unique identifier for this particular occurrence of the problem.",
329
+ "type": "string"
330
+ },
331
+ "links": {
332
+ "$ref": "#/definitions/links"
333
+ },
334
+ "status": {
335
+ "description": "The HTTP status code applicable to this problem, expressed as a string value.",
336
+ "type": "string"
337
+ },
338
+ "code": {
339
+ "description": "An application-specific error code, expressed as a string value.",
340
+ "type": "string"
341
+ },
342
+ "title": {
343
+ "description": "A short, human-readable summary of the problem. It **SHOULD NOT** change from occurrence to occurrence of the problem, except for purposes of localization.",
344
+ "type": "string"
345
+ },
346
+ "detail": {
347
+ "description": "A human-readable explanation specific to this occurrence of the problem.",
348
+ "type": "string"
349
+ },
350
+ "source": {
351
+ "type": "object",
352
+ "properties": {
353
+ "pointer": {
354
+ "description": "A JSON Pointer [RFC6901] to the associated entity in the request document [e.g. \"/data\" for a primary data object, or \"/data/attributes/title\" for a specific attribute].",
355
+ "type": "string"
356
+ },
357
+ "parameter": {
358
+ "description": "A string indicating which query parameter caused the error.",
359
+ "type": "string"
360
+ }
361
+ }
362
+ },
363
+ "meta": {
364
+ "$ref": "#/definitions/meta"
365
+ }
366
+ },
367
+ "additionalProperties": false
368
+ }
369
+ }
370
+ }
@@ -0,0 +1,11 @@
1
+ require "minitest/autorun"
2
+ require_relative '../lib/jsonapi-validator.rb'
3
+ class TestSchema < Minitest::Test
4
+ def setup
5
+ @instance = STANDARD_SCHEMA
6
+ end
7
+
8
+ def test_schema_load
9
+ assert_equal @instance["title"],"JSON API Schema"
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ require "minitest/autorun"
2
+ require_relative '../lib/jsonapi-validator.rb'
3
+ class TestSchema < Minitest::Test
4
+ def setup
5
+ @invalid_object = {}
6
+ @valid_object = {
7
+ "data": {
8
+ "type": "articles",
9
+ "id": "1"
10
+ }
11
+ }
12
+ end
13
+
14
+ def test_invalid_json_object
15
+ assert_equal JsonApi::Validator.json(@invalid_object)[0].size > 0 ,true
16
+ end
17
+
18
+ def test_valid_json_object
19
+ assert_equal JsonApi::Validator.json(@valid_object),[]
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonapi-validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Karthik selvakumar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json-schema
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: This gem uses jsonapi.org schema to validate API schemas generated in
42
+ spec/api directory.
43
+ email: karthikselvakumar7@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/jsonapi-validator.rb
49
+ - lib/jsonapi/schema.rb
50
+ - lib/jsonapi/validator.rb
51
+ - lib/tasks/jsonapi-validator.rb
52
+ - lib/tasks/minitest.rb
53
+ - schema/schema.json
54
+ - test/test_schema.rb
55
+ - test/test_validator.rb
56
+ homepage: http://rubygems.org/gems/jsonapi-validator
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.5.1
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: jsonapi.org schema validator
80
+ test_files: []