rack-json_schema 1.0.1 → 1.0.2
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 +4 -4
- data/CHANGELOG.md +3 -0
- data/README.md +48 -65
- data/bin/specup +2 -0
- data/lib/rack/json_schema/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13b64aeff3cae973d98aaf8f50c868a8b2223318
|
4
|
+
data.tar.gz: 30059b1a8a875156c441daf348cfe4ec245309dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a3b043a0df3ae347ac84a03ab87bc843b76479dde93ad7f479752e34609e0ff0658cd2e1e6e9ddce0c16b139ff60144a4f24f1613e00f4e9d03f6481332a1374
|
7
|
+
data.tar.gz: 960189e3f983e32e73376a518a7cedfa53e1ec9479d9829c2a8b25a3b184cced50f3d7ae8a4f4e5a636c26179e1cf8dc721b09d96531df78ce024bda60bb0772
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,14 @@
|
|
1
1
|
# Rack::JsonSchema
|
2
2
|
[JSON Schema](http://json-schema.org/) based Rack middlewares.
|
3
3
|
|
4
|
+
* [Rack::JsonSchema::RequestValidation](#rackjsonschemarequestvalidation)
|
5
|
+
* [Rack::JsonSchema::ResponseValidation](#rackjsonschemaresponsevalidation)
|
6
|
+
* [Rack::JsonSchema::Mock](#rackjsonschemamock)
|
7
|
+
* [Rack::JsonSchema::ErrorHandler](#rackjsonschemaerrorhandler)
|
8
|
+
* [Rack::JsonSchema::Docs](#rackjsonschemadocs)
|
9
|
+
* [Rack::JsonSchema::SchemaProvider](#rackjsonschemaschemaprovider)
|
10
|
+
* [specup](#specup)
|
11
|
+
|
4
12
|
## Usage
|
5
13
|
```ruby
|
6
14
|
str = File.read("schema.json")
|
@@ -23,25 +31,25 @@ Validates request and raises errors below.
|
|
23
31
|
* Rack::JsonSchema::RequestValidation::LinkNotFound
|
24
32
|
|
25
33
|
```sh
|
26
|
-
$ curl http://localhost:
|
34
|
+
$ curl http://localhost:8080/users
|
27
35
|
{
|
28
36
|
"id": "link_not_found",
|
29
37
|
"message": "Not found"
|
30
38
|
}
|
31
39
|
|
32
|
-
$ curl http://localhost:
|
40
|
+
$ curl http://localhost:8080/apps -H "Content-Type: application/json" -d "invalid-json"
|
33
41
|
{
|
34
42
|
"id": "invalid_json",
|
35
43
|
"message": "Request body wasn't valid JSON"
|
36
44
|
}
|
37
45
|
|
38
|
-
$ curl http://localhost:
|
46
|
+
$ curl http://localhost:8080/apps -H "Content-Type: text/plain" -d "{}"
|
39
47
|
{
|
40
48
|
"id": "invalid_content_type",
|
41
49
|
"message": "Invalid content type"
|
42
50
|
}
|
43
51
|
|
44
|
-
$ curl http://localhost:
|
52
|
+
$ curl http://localhost:8080/apps -H "Content-Type: application/json" -d '{"name":"x"}'
|
45
53
|
{
|
46
54
|
"id": "invalid_parameter",
|
47
55
|
"message": "Invalid request.\n#/name: failed schema #/definitions/app/links/0/schema/properties/name: Expected string to match pattern \"/^[a-z][a-z0-9-]{3,50}$/\", value was: x."
|
@@ -55,13 +63,13 @@ Validates request and raises errors below.
|
|
55
63
|
* Rack::JsonSchema::RequestValidation::InvalidResponseType
|
56
64
|
|
57
65
|
```sh
|
58
|
-
$ curl http://localhost:
|
66
|
+
$ curl http://localhost:8080/apps
|
59
67
|
{
|
60
68
|
"id": "invalid_response_content_type",
|
61
69
|
"message": "Response Content-Type wasn't for JSON"
|
62
70
|
}
|
63
71
|
|
64
|
-
$ curl http://localhost:
|
72
|
+
$ curl http://localhost:8080/apps
|
65
73
|
{
|
66
74
|
"id": "invalid_response_type",
|
67
75
|
"message": "#: failed schema #/definitions/app: Expected data to be of type \"object\"; value was: [\"message\", \"dummy\"]."
|
@@ -72,7 +80,7 @@ $ curl http://localhost:9292/apps
|
|
72
80
|
Generates dummy response from JSON schema.
|
73
81
|
|
74
82
|
```sh
|
75
|
-
$ curl http://localhost:
|
83
|
+
$ curl http://localhost:8080/apps/1
|
76
84
|
[
|
77
85
|
{
|
78
86
|
"id": "01234567-89ab-cdef-0123-456789abcdef",
|
@@ -80,34 +88,25 @@ $ curl http://localhost:9292/apps/1
|
|
80
88
|
}
|
81
89
|
]
|
82
90
|
|
83
|
-
$ curl http://localhost:
|
91
|
+
$ curl http://localhost:8080/apps/01234567-89ab-cdef-0123-456789abcdef
|
84
92
|
{
|
85
93
|
"id": "01234567-89ab-cdef-0123-456789abcdef",
|
86
94
|
"name": "example"
|
87
95
|
}
|
88
96
|
|
89
|
-
$ curl http://localhost:
|
97
|
+
$ curl http://localhost:8080/apps/1 -d '{"name":"example"}'
|
90
98
|
{
|
91
99
|
"id": "01234567-89ab-cdef-0123-456789abcdef",
|
92
100
|
"name": "example"
|
93
101
|
}
|
94
102
|
|
95
|
-
$ curl http://localhost:
|
103
|
+
$ curl http://localhost:8080/recipes
|
96
104
|
{
|
97
105
|
"id": "example_not_found",
|
98
106
|
"message": "No example found for #/definitions/recipe/id"
|
99
107
|
}
|
100
108
|
```
|
101
109
|
|
102
|
-
Note: `specup` executable command is bundled to rackup dummy API server.
|
103
|
-
|
104
|
-
```sh
|
105
|
-
$ specup schema.json
|
106
|
-
[2014-06-06 23:01:35] INFO WEBrick 1.3.1
|
107
|
-
[2014-06-06 23:01:35] INFO ruby 2.0.0 (2013-06-27) [x86_64-darwin12.5.0]
|
108
|
-
[2014-06-06 23:01:35] INFO WEBrick::HTTPServer#start: pid=24303 port=8080
|
109
|
-
```
|
110
|
-
|
111
110
|
### Rack::JsonSchema::ErrorHandler
|
112
111
|
Returns appropriate error response including following properties when RequestValidation raises error.
|
113
112
|
|
@@ -156,20 +155,26 @@ Returns API documentation as a text/plain content, rendered in GitHub flavored M
|
|
156
155
|
* API documentation is powered by [jdoc](https://github.com/r7kamura/jdoc) gem
|
157
156
|
* This middleware is also bundled in the `specup` executable command
|
158
157
|
|
158
|
+
### Rack::JsonSchema::SchemaProvider
|
159
|
+
Serves JSON Schema at `GET /schema`.
|
160
|
+
|
161
|
+
* You can give `path` option to change default path: `GET /schema`
|
162
|
+
* This middleware is also bundled in the `specup` executable command
|
163
|
+
|
164
|
+
## specup
|
165
|
+
`specup` executable command is bundled to rackup handy mock API server.
|
166
|
+
It validates requests,
|
167
|
+
and returns dummy response,
|
168
|
+
also returns auto-generated API documentation at `GET /docs`,
|
169
|
+
and JSON Schema itself at `GET /schema`.
|
170
|
+
|
159
171
|
```sh
|
160
172
|
$ specup schema.json
|
161
173
|
[2014-06-06 23:01:35] INFO WEBrick 1.3.1
|
162
174
|
[2014-06-06 23:01:35] INFO ruby 2.0.0 (2013-06-27) [x86_64-darwin12.5.0]
|
163
175
|
[2014-06-06 23:01:35] INFO WEBrick::HTTPServer#start: pid=24303 port=8080
|
164
176
|
|
165
|
-
$ curl :8080/docs
|
166
|
-
HTTP/1.1 200 OK
|
167
|
-
Content-Type: text/plain; charset=utf-8
|
168
|
-
Server: WEBrick/1.3.1 (Ruby/2.1.1/2014-02-24)
|
169
|
-
Date: Sat, 07 Jun 2014 19:58:04 GMT
|
170
|
-
Content-Length: 2175
|
171
|
-
Connection: Keep-Alive
|
172
|
-
|
177
|
+
$ curl :8080/docs
|
173
178
|
# Example API
|
174
179
|
* [App](#app)
|
175
180
|
* [GET /apps](#get-apps)
|
@@ -179,47 +184,10 @@ Connection: Keep-Alive
|
|
179
184
|
* [DELETE /apps/:id](#delete-appsid)
|
180
185
|
* [Recipe](#recipe)
|
181
186
|
* [GET /recipes](#get-recipes)
|
182
|
-
|
183
|
-
## App
|
184
|
-
An app is a program to be deployed.
|
185
|
-
|
186
|
-
### Properties
|
187
|
-
* id - unique identifier of app
|
188
|
-
* Example: `01234567-89ab-cdef-0123-456789abcdef`
|
189
|
-
* Type: string
|
190
|
-
* Format: uuid
|
191
|
-
* ReadOnly: true
|
192
|
-
* name - unique name of app
|
193
|
-
* Example: `example`
|
194
|
-
* Type: string
|
195
|
-
* Patern: `(?-mix:^[a-z][a-z0-9-]{3,50}$)`
|
196
|
-
|
197
|
-
### GET /apps
|
198
|
-
List existing apps.
|
199
|
-
|
200
187
|
...
|
201
|
-
```
|
202
|
-
|
203
|
-
### Rack::JsonSchema::SchemaProvider
|
204
|
-
Serves JSON Schema at `GET /schema`.
|
205
|
-
|
206
|
-
* You can give `path` option to change default path: `GET /schema`
|
207
|
-
* This middleware is also bundled in the `specup` executable command
|
208
|
-
|
209
|
-
```sh
|
210
|
-
$ specup schema.json
|
211
|
-
[2014-06-06 23:01:35] INFO WEBrick 1.3.1
|
212
|
-
[2014-06-06 23:01:35] INFO ruby 2.0.0 (2013-06-27) [x86_64-darwin12.5.0]
|
213
|
-
[2014-06-06 23:01:35] INFO WEBrick::HTTPServer#start: pid=24303 port=8080
|
214
188
|
|
215
|
-
$ curl :8080/schema
|
189
|
+
$ curl :8080/schema
|
216
190
|
HTTP/1.1 200 OK
|
217
|
-
Content-Type: text/plain; charset=utf-8
|
218
|
-
Server: WEBrick/1.3.1 (Ruby/2.1.1/2014-02-24)
|
219
|
-
Date: Sat, 07 Jun 2014 19:58:04 GMT
|
220
|
-
Content-Length: 2175
|
221
|
-
Connection: Keep-Alive
|
222
|
-
|
223
191
|
{
|
224
192
|
"$schema": "http://json-schema.org/draft-04/hyper-schema",
|
225
193
|
"definitions": {
|
@@ -232,3 +200,18 @@ Connection: Keep-Alive
|
|
232
200
|
}
|
233
201
|
}
|
234
202
|
}
|
203
|
+
|
204
|
+
$ curl :8080/apps/1
|
205
|
+
[
|
206
|
+
{
|
207
|
+
"id": "01234567-89ab-cdef-0123-456789abcdef",
|
208
|
+
"name": "example"
|
209
|
+
}
|
210
|
+
]
|
211
|
+
|
212
|
+
$ curl :8080/apps -H "Content-Type: application/json" -d '{"name":1}'
|
213
|
+
{
|
214
|
+
"id": "invalid_parameter",
|
215
|
+
"message": "Invalid request.\n#/name: failed schema #/definitions/app/links/0/schema/properties/name: Expected data to be of type \"string\"; value was: 1."
|
216
|
+
}
|
217
|
+
```
|
data/bin/specup
CHANGED
@@ -23,6 +23,8 @@ app = Rack::Builder.new do
|
|
23
23
|
use Rack::JsonSchema::Docs, schema: schema
|
24
24
|
use Rack::JsonSchema::SchemaProvider, schema: schema
|
25
25
|
use Rack::JsonSchema::ErrorHandler
|
26
|
+
use Rack::JsonSchema::RequestValidation, schema: schema
|
27
|
+
use Rack::JsonSchema::ResponseValidation, schema: schema
|
26
28
|
use Rack::JsonSchema::Mock, schema: schema
|
27
29
|
run ->(env) { [404, {}, ["Not found"]] }
|
28
30
|
end
|