Sha256: 3ef2e45d24613d81d656129f1960a7a152f727fec7dd9c70b7d6913c2caff9c8

Contents?: true

Size: 1.12 KB

Versions: 13

Compression:

Stored size: 1.12 KB

Contents

require 'spec_helper'

describe 'Validator with instance variables' do
  let(:validator_type) do
    Class.new(Grape::Validations::Base) do
      def validate_param!(_attr_name, _params)
        if @instance_variable
          raise Grape::Exceptions::Validation, params: ['params'],
                                               message: 'This should never happen'
        end
        @instance_variable = true
      end
    end
  end

  before do
    Grape::Validations.register_validator('instance_validator', validator_type)
  end

  after do
    Grape::Validations.deregister_validator('instance_validator')
  end

  let(:app) do
    Class.new(Grape::API) do
      params do
        optional :param_to_validate, instance_validator: true
        optional :another_param_to_validate, instance_validator: true
      end
      get do
        'noop'
      end
    end
  end

  it 'passes validation every time' do
    expect(validator_type).to receive(:new).exactly(4).times.and_call_original

    2.times do
      get '/', param_to_validate: 'value', another_param_to_validate: 'value'
      expect(last_response.status).to eq 200
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/grape-1.2.5/spec/grape/validations/instance_behaivour_spec.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/grape-1.2.5/spec/grape/validations/instance_behaivour_spec.rb
grape-1.2.5 spec/grape/validations/instance_behaivour_spec.rb
grape-1.2.4 spec/grape/validations/instance_behaivour_spec.rb
grape-1.2.3 spec/grape/validations/instance_behaivour_spec.rb
grape-1.2.2 spec/grape/validations/instance_behaivour_spec.rb
grape-1.2.1 spec/grape/validations/instance_behaivour_spec.rb
grape-1.2.0 spec/grape/validations/instance_behaivour_spec.rb
grape-1.1.0 spec/grape/validations/instance_behaivour_spec.rb
grape-1.0.3 spec/grape/validations/instance_behaivour_spec.rb
grape-1.0.2 spec/grape/validations/instance_behaivour_spec.rb
grape-1.0.1 spec/grape/validations/instance_behaivour_spec.rb
grape-1.0.0 spec/grape/validations/instance_behaivour_spec.rb