Grape: Boolean validation ignores values

Created on 16 Feb 2017  Â·  17Comments  Â·  Source: ruby-grape/grape

params do
  requires model_name do
      requires :params1, type: Boolean,allow_blank: false, desc: "params1",values: [true]
  end
end

When this API is called with params1 value as false should return an exception. But grape doesn't returns exception and the API is successfully executed.

bug?

All 17 comments

@sureshprasanna70 … what for exception did you expect?

@LeFnord The exception I had expected was "params1 has invalid value"

Hi @sureshprasanna70 … you are submitting a value for the param params1, with value false, so no exception is raised, cause it isn't empty 😉 For this data type, it should only be raised, if the parameter is missing, but then the validation for missing required params should be raised, in short, the usage of allow_blank makes no sense in this constellation.

@dblock suggest to close it

@LeFnord This doesn't address the issue. Let me explain the exception I was expecting. If the value passed for the said params doesn't matches anyone of the values in the values option array then grape is expected to throw an error. In this case, when params1 of the type Boolean is passed with false as value, grape should be throwing params1 is invalid, params1 does not have a valid value. This is what I was expecting. Yeah as you said allow_blank doesn't make sense here. But the above said behavior is expected.

Oh I missed the values part. That does look like a bug, @sureshprasanna70 maybe try to write a spec? I'll reopen.

ah yeah, that makes sense, sorry overlooked the values part

It currenly throws an exception, if you try to define values for Boolean type:

Grape::Exceptions::IncompatibleOptionValues: type: Virtus::Attribute::Boolean is incompatible with values: [false]
  grape-0.19.2/lib/grape/validations/params_scope.rb:384:in `validate_value_coercion'
  grape-0.19.2/lib/grape/validations/params_scope.rb:244:in `validates'
  grape-0.19.2/lib/grape/validations/params_scope.rb:147:in `validate_attributes'
  grape-0.19.2/lib/grape/dsl/parameters.rb:107:in `requires'

@ktimothy try to contribute a spec, maybe even a fix?

@dblock I found a bug in virtus, even suggested a fix, but it doesn't look like they will ever merge it and release new version of gem: last virtus release was over 2 years ago.

It looks like there're recent commits, so I wouldn't be so sure.

@dblock

@dblock Are we open to fixing this issue?. I could contribute a spec if so..

Yes, we are open to fixing this issue.

Do note https://github.com/solnic/virtus/commit/a6f8969841247462bb05c456458baa66fce29ed8. I think this means Grape should move on to a new library. I hope someone in the community picks it up! Thanks @solnic for providing a valuable project to us for all the years.

Do note solnic/virtus@a6f8969. I think this means Grape should move on to a new library. I hope someone in the community picks it up! Thanks @solnic for providing a valuable project to us for all the years.

Dry-rb maybe?

Dry-rb maybe?

Looking for PRs!

Was this page helpful?
0 / 5 - 0 ratings