You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The request validation middleware returns a 4xx if the request is invalid or not defined in the API description. It adds a request object to the current Rack environment at `env[OpenapiFirst::REQUEST]` with the request parameters parsed exactly as described in your API description plus access to meta information from your API description. See _[Manual use](#manual-use)_ for more details about that object.
59
82
60
83
```ruby
61
-
use OpenapiFirst::Middlewares::RequestValidation, 'openapi.yaml'
84
+
use OpenapiFirst::Middlewares::RequestValidation
62
85
63
86
# Pass `raise_error: true` to raise an error if request is invalid:
64
-
use OpenapiFirst::Middlewares::RequestValidation, 'openapi.yaml', raise_error:true
87
+
use OpenapiFirst::Middlewares::RequestValidation, raise_error:true
65
88
```
66
89
67
90
#### Error responses
@@ -152,10 +175,10 @@ This middleware raises an error by default if the response is not valid.
152
175
This can be useful in a test or staging environment, especially if you are adopting OpenAPI for an existing implementation.
153
176
154
177
```ruby
155
-
use OpenapiFirst::Middlewares::ResponseValidation, 'openapi.yaml'ifENV['RACK_ENV'] =='test'
178
+
use OpenapiFirst::Middlewares::ResponseValidationifENV['RACK_ENV'] =='test'
156
179
157
180
# Pass `raise_error: false` to not raise an error:
158
-
use OpenapiFirst::Middlewares::ResponseValidation, 'openapi.yaml', raise_error:false
181
+
use OpenapiFirst::Middlewares::ResponseValidation, raise_error:false
159
182
```
160
183
161
184
If you are adopting OpenAPI you can use these options together with [hooks](#hooks) to get notified about requests/responses that do match your API description.
@@ -166,13 +189,11 @@ You can see your OpenAPI API description as a contract that your clients can rel
166
189
167
190
Here is how to set it up:
168
191
169
-
1. Register all OpenAPI documents to track coverage for.
170
-
This should go at the top of your test helper file before loading your application code.
192
+
1. Setup the test mode
171
193
```ruby
194
+
# spec_helper.rb
172
195
require'openapi_first'
173
-
OpenapiFirst::Test.setup do |config|
174
-
config.register('openapi/openapi.yaml')
175
-
end
196
+
OpenapiFirst::Test.setup
176
197
```
177
198
2. Observe your application. You can do this in multiple ways:
178
199
-Add an `app` method to your tests (which is called by rack-test) that wraps your application with silent request / response validation.
0 commit comments