Skip to content

Cannot restrict boolean param #1091

Closed
@Fryie

Description

@Fryie

Basically what I want to to is this:

params do
  optional :password, allow_blank: false
  optional :generate_password, type: Boolean, values: [true]
  exactly_one_of :password, :generate_password
end

However, that won't even start up the app, because Grape / Virtus is complaining that true is not a valid value (which it truly isn't - true is not an instance of Virtus::Attribute::Boolean). Using values: [Virtus::Attribute.build(true)] runs the app, but does not seem to run the proper validation.

The only I was able to do this, was with

class Truthy < Grape::Validations::Base
  def validate_param!(attr_name, params)
    unless params[attr_name]
      raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: 'must be true'
    end
  end
end

params do
  optional :generate_password, type: Boolean, truthy: true
end

but I'm not sure why this doesn't work the way one would expect it to work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions