spec/grape/validations_spec.rb in grape-1.6.2 vs spec/grape/validations_spec.rb in grape-1.7.0

- old
+ new

@@ -1,9 +1,7 @@ # frozen_string_literal: true -require 'spec_helper' - describe Grape::Validations do subject { Class.new(Grape::API) } def app subject @@ -43,11 +41,11 @@ it 'adds to declared parameters' do subject.params do optional :some_param end - expect(declared_params).to eq([:some_param]) + expect(Grape::Validations::ParamsScope::Attr.attrs_keys(declared_params)).to eq([:some_param]) end end context 'optional using Grape::Entity documentation' do def define_optional_using @@ -63,11 +61,11 @@ end end it 'adds entity documentation to declared params' do define_optional_using - expect(declared_params).to eq(%i[field_a field_b]) + expect(Grape::Validations::ParamsScope::Attr.attrs_keys(declared_params)).to eq(%i[field_a field_b]) end it 'works when field_a and field_b are not present' do get '/optional' expect(last_response.status).to eq(200) @@ -110,11 +108,11 @@ it 'adds to declared parameters' do subject.params do requires :some_param end - expect(declared_params).to eq([:some_param]) + expect(Grape::Validations::ParamsScope::Attr.attrs_keys(declared_params)).to eq([:some_param]) end it 'works when required field is present but nil' do put '/required', { key: nil }.to_json, 'CONTENT_TYPE' => 'application/json' expect(last_response.status).to eq(200) @@ -195,11 +193,11 @@ end end it 'adds entity documentation to declared params' do define_requires_all - expect(declared_params).to eq(%i[required_field optional_field]) + expect(Grape::Validations::ParamsScope::Attr.attrs_keys(declared_params)).to eq(%i[required_field optional_field]) end it 'errors when required_field is not present' do get '/required' expect(last_response.status).to eq(400) @@ -230,11 +228,11 @@ end end it 'adds entity documentation to declared params' do define_requires_none - expect(declared_params).to eq(%i[required_field optional_field]) + expect(Grape::Validations::ParamsScope::Attr.attrs_keys(declared_params)).to eq(%i[required_field optional_field]) end it 'errors when required_field is not present' do get '/required' expect(last_response.status).to eq(400) @@ -260,11 +258,11 @@ end end it 'adds only the entity documentation to declared params, nothing more' do define_requires_all - expect(declared_params).to eq(%i[required_field optional_field]) + expect(Grape::Validations::ParamsScope::Attr.attrs_keys(declared_params)).to eq(%i[required_field optional_field]) end end context 'requires :none' do def define_requires_none @@ -326,11 +324,11 @@ subject.params do requires :items, type: Array do requires :key end end - expect(declared_params).to eq([items: [:key]]) + expect(Grape::Validations::ParamsScope::Attr.attrs_keys(declared_params)).to eq([items: [:key]]) end end # Ensure there is no leakage between declared Array types and # subsequent Hash types @@ -398,11 +396,11 @@ subject.params do requires :items, type: Array do requires :key end end - expect(declared_params).to eq([items: [:key]]) + expect(Grape::Validations::ParamsScope::Attr.attrs_keys(declared_params)).to eq([items: [:key]]) end end context 'hash with a required param with validation' do before do @@ -461,11 +459,11 @@ subject.params do group :items, type: Array do requires :key end end - expect(declared_params).to eq([items: [:key]]) + expect(Grape::Validations::ParamsScope::Attr.attrs_keys(declared_params)).to eq([items: [:key]]) end end context 'group params with nested params which has a type' do let(:invalid_items) { { items: '' } } @@ -822,11 +820,11 @@ subject.params do optional :items, type: Array do requires :key end end - expect(declared_params).to eq([items: [:key]]) + expect(Grape::Validations::ParamsScope::Attr.attrs_keys(declared_params)).to eq([items: [:key]]) end end context 'nested optional Array blocks' do before do @@ -886,11 +884,11 @@ requires :key optional(:optional_subitems, type: Array) { requires :value } requires(:required_subitems, type: Array) { requires :value } end end - expect(declared_params).to eq([items: [:key, { optional_subitems: [:value] }, { required_subitems: [:value] }]]) + expect(Grape::Validations::ParamsScope::Attr.attrs_keys(declared_params)).to eq([items: [:key, { optional_subitems: [:value] }, { required_subitems: [:value] }]]) end context <<~DESC do Issue occurs whenever: * param structure with at least three levels @@ -1426,18 +1424,18 @@ it 'by #use' do subject.params do use :pagination end - expect(declared_params).to eq %i[page per_page] + expect(Grape::Validations::ParamsScope::Attr.attrs_keys(declared_params)).to eq %i[page per_page] end it 'by #use with multiple params' do subject.params do use :pagination, :period end - expect(declared_params).to eq %i[page per_page start_date end_date] + expect(Grape::Validations::ParamsScope::Attr.attrs_keys(declared_params)).to eq %i[page per_page start_date end_date] end end context 'with block' do before do @@ -1483,21 +1481,33 @@ expect(last_response.body).to eq('{"error":"order does not have a valid value"}') end end end - context 'documentation' do - it 'can be included with a hash' do - documentation = { example: 'Joe' } - + context 'with block and keyword argument' do + before do + subject.helpers do + params :shared_params do |type:| + optional :param, default: type + end + end + subject.format :json subject.params do - requires 'first_name', documentation: documentation + use :shared_params, type: 'value' end - subject.get '/' do + subject.get '/shared_params' do + { + param: params[:param] + } end + end - expect(subject.routes.first.params['first_name'][:documentation]).to eq(documentation) + it 'works' do + get '/shared_params' + + expect(last_response.status).to eq(200) + expect(last_response.body).to eq({ param: 'value' }.to_json) end end context 'all or none' do context 'optional params' do @@ -1970,9 +1980,27 @@ 'exactly_one_of_group works!' end get '/exactly_one_of_group', drink: { foo: 'bar' }, beer: 'true' expect(last_response.status).to eq(400) + end + end + end + + describe 'require_validator' do + subject { described_class.require_validator(short_name) } + + context 'when found' do + let(:short_name) { :presence } + + it { is_expected.to be(Grape::Validations::Validators::PresenceValidator) } + end + + context 'when not found' do + let(:short_name) { :test } + + it 'raises an error' do + expect { subject }.to raise_error(Grape::Exceptions::UnknownValidator) end end end end