-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to require an Array param, disallow an empty array, but accept 'null' as a value #1198
Comments
I'm not sure, but would this work? ARRAY_OR_NULL = lambda do |val|
if val == "null"
nil
elsif val.is_a?(Array) && !val.empty?
val
else
fail ArgumentError
end
end
# ...
requires :element, type: Array, coerce_with: ARRAY_OR_NULL do Note that |
Thank you very much for your response. I tried implementing what you suggested, but I am not sure I had all the pieces in place. I was getting an error of the type `validate': unknown validator: array_or_null (Grape::Exceptions::UnknownValidator) Based on the "Custom Validators" section in the documentation I have tried this: Created this file under a 'validations' directory: module V1 class NonEmptyArrayOrNull < Grape::Validations::Base end Then in my endpoint file I have: module V1 class BlankGenotype < Grape::API
...
.... I get the following error: Does this mean that the params parameter is not known to my custom validator class? How would I fix this? |
I am having the same issue while using custom validation, the gem version is 0.13.0 |
You simple need to upgrade to version 0.14.0. Theres no coerce_with property in 0.13.0. |
👍 @PHronek I'm closing this, please reopen if the problem is different. |
I am not sure how to re-open this issue. I am now using grape version 0.15.0, and I no longer get the NoMethodError (undefined method []' for nil:NilClass): error reported before. Using the custom validation class that I created is working as expected, but the problem is that params[attr_name] in the NonEmptyArrayOrNull class is set to an empty array, both when the maidCalls element in the json payload is set to null and when it is set to an empty array. So using this custom validator, I still can't differentiate between the following 2 son inputs: "maidCalls": null So it looks like Grape converts both of these inputs into an empty array when creating "params". Is there a way of checking for null vs [], maybe params is defined? |
Ah! but if I use this code, as originally suggested by dslh, it appears to work as I desire! In this case, it appears that val is set to whatever is sent in the json, instead of Grape converting it to an empty Array. requires :maidCalls, type: Array, coerce_with: ->(val) { if val == "null" |
I have a validation like so:
requires :element, type: Array do
The only values that I want to accept in the JSON payload sent to the API are:
a) a non-empty array
b) the string 'null'
I can use allow_blank: false to disallow an empty array, but this validation also fires if null is sent.
How can I accept null, but not an empty array?
Why do I want this validation? Because we don't want to accept empty arrays as inputs, because these we believe can be ambiguous, i.e. did the developer/client really mean to send an empty array, or is the empty array the result of poor parsing or lack of proper validation on the client side? So if in fact there are no values to be sent for the array "element", we want that element to be set explicitly to null.
The text was updated successfully, but these errors were encountered: