Skip to content

Commit b68b777

Browse files
committed
added validations in README
1 parent bc3318c commit b68b777

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

README.markdown

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,26 @@ post '/json_endpoint' do
192192
end
193193
```
194194

195+
## Validations
196+
197+
You can define validations and coercion option for your attributes:
198+
199+
```ruby
200+
params do
201+
required :id, type: Integer
202+
optional :name, type: String, regexp: /^[a-z]+$/
203+
end
204+
205+
get ':id' do
206+
# params[:id] is an Integer
207+
end
208+
```
209+
210+
The coercion is handled by the really nice Virtus gem which will convert the value if possible but
211+
in case of invalid input nothing will be done (ex: asking to coerce "ex" to Integer will return "ex" ).
212+
Proper type validation could be added later when Virtus will get a way to tell us.
213+
214+
195215
## Headers
196216

197217
Headers are available through the `env` hash object.

0 commit comments

Comments
 (0)