-
-
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
Values validator fails fast when below root scope, violating documentation #2376
Comments
Looks like a bug, appreciate a fix! |
Looks like unexpected expected behavior #671 |
Ah, that's it! It seems like the code conflates sets of params bundled using |
@eriklovmo Are you trying to fix it? Write some specs at least that fail? |
@dblock an example of spec that fails: diff --git a/spec/grape/validations/validators/values_spec.rb b/spec/grape/validations/validators/values_spec.rb
index d8ef17c9..5d8daab9 100644
--- a/spec/grape/validations/validators/values_spec.rb
+++ b/spec/grape/validations/validators/values_spec.rb
@@ -261,6 +261,13 @@ describe Grape::Validations::Validators::ValuesValidator do
optional :name, type: String, values: %w[a b], allow_blank: true
end
get '/allow_blank'
+
+ params do
+ with(type: String) do
+ requires :type, values: ValuesModel.values
+ end
+ end
+ get 'values_wrapped_by_with_block'
end
end
@@ -730,4 +737,13 @@ describe Grape::Validations::Validators::ValuesValidator do
end
end
end
+
+ context 'when wrapped by with block' do
+ it 'rejects an invalid value' do
+ get 'values_wrapped_by_with_block'
+
+ expect(last_response.status).to eq 400
+ expect(last_response.body).to eq({ error: 'type is missing, type does not have a valid value' }.to_json)
+ end
+ end
end Fails with:
|
Possible fix ⬆️ |
I have not had time, unfortunately, but please see the fix from @numbata! @dblock |
In the documentation, it is currently written:
My interpretation of that statement is that, in order to skip subsequent validations when a specific param is found invalid, one must set
fail_fast
to true (though that interpretation might be far-reaching). However, I have found one example that behaves differently. When using thevalues
validator in a nested scope, the validation is not exhaustive:grape/lib/grape/validations/validators/values_validator.rb
Line 32 in 3f01d03
The following snippets demonstrate what this entails.
A
With example A, the validation includes the following errors when no params are provided:
"bar is missing, bar does not have a valid value, baz is missing, baz does not have a valid value"
B
With example B, the validation includes the following errors when no params are provided:
"bar is missing, baz is missing"
It would be good to either fix this behavior if possible or to update the documentation to mention this exception, either in the section about the
values
validator or in the general paragraph mentioning "fast failing", which I have cited.The text was updated successfully, but these errors were encountered: