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
Parameters can be restricted to a specific set of values with the `:values` option.
1116
1120
1117
-
Default values are eagerly evaluated. Above `:non_random_number` will evaluate to the same
1118
-
number for each call to the endpoint of this `params` block. To have the default evaluate
1119
-
lazily with each request use a lambda, like `:random_number` above.
1120
1121
1121
1122
```ruby
1122
1123
params do
@@ -1135,7 +1136,7 @@ params do
1135
1136
end
1136
1137
```
1137
1138
1138
-
Note that *both* range endpoints have to be a `#kind_of?` your `:type` option (if you don't supplied the `:type` option, it will be guessed to be equal to the class of the range's first endpoint). So the following is invalid:
1139
+
Note that *both* range endpoints have to be a `#kind_of?` your `:type` option (if you don't supply the `:type` option, it will be guessed to be equal to the class of the range's first endpoint). So the following is invalid:
1139
1140
1140
1141
```ruby
1141
1142
params do
@@ -1145,6 +1146,9 @@ end
1145
1146
```
1146
1147
1147
1148
The `:values` option can also be supplied with a `Proc`, evaluated lazily with each request.
1149
+
If the Proc has arity zero (i.e. it takes no arguments) it is expected to return either a list
1150
+
or a range which will then be used to validate the parameter.
1151
+
1148
1152
For example, given a status model you may want to restrict by hashtags that you have
1149
1153
previously defined in the `HashTag` model.
1150
1154
@@ -1154,40 +1158,34 @@ params do
1154
1158
end
1155
1159
```
1156
1160
1157
-
The values validator can also validate that the value is explicitly not within a specific
1158
-
set of values by passing ```except```. ```except``` accepts the same types of parameters as
1159
-
values (Procs, ranges, etc.).
1161
+
Alternatively, a Proc with arity one (i.e. taking one argument) can be used to explicitly validate
1162
+
each parameter value. In that case, the Proc is expected to return a truthy value if the parameter
requires :number, type:Integer, values:->(v) { v.even? && v <25 }
1164
1168
end
1165
1169
```
1166
1170
1167
-
Values and except can be combined to define a range of accepted values while not allowing
1168
-
certain values within the set. Custom error messages can be defined for both when the parameter
1169
-
passed falls within the ```except``` list or when it falls entirely outside the ```value``` list.
1171
+
While Procs are convenient for single cases, consider using [Custom Validators](#custom-validators) in cases where a validation is used more than once.
1170
1172
1171
-
```ruby
1172
-
params do
1173
-
requires :number, type:Integer, values: { value:1..20, except: [4, 13], except_message:'includes unsafe numbers', message:'is outside the range of numbers allowed' }
1174
-
end
1175
-
```
1173
+
#### `except_values`
1176
1174
1177
-
Finally, for even greater control, an explicit validation Proc may be supplied using ```proc```.
1178
-
It will be called with a single argument (the input value), and should return
1179
-
a truthy value if the value passes validation. If the input is an array, the Proc will be called
1180
-
multiple times, once for each element in the array.
1175
+
Parameters can be restricted from having a specific set of values with the `:except_values` option.
1176
+
1177
+
The `except_values` validator behaves similarly to the `values` validator in that it accepts either
1178
+
an Array, a Range, or a Proc. Unlike the `values` validator, however, `except_values` only accepts
1179
+
Procs with arity zero.
1181
1180
1182
1181
```ruby
1183
1182
params do
1184
-
requires :number, type:Integer, values: { proc:->(v) { v.even? && v <25 }, message:'is odd or greater than 25' }
While ```proc``` is convenient for single cases, consider using [Custom Validators](#custom-validators) in cases where a validation is used more than once.
1189
-
1190
-
1191
1189
#### `regexp`
1192
1190
1193
1191
Parameters can be restricted to match a specific regular expression with the `:regexp` option. If the value
0 commit comments