Skip to content
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

Avoid requiring non-existent fields when using Grape::Entity documentation #673

Merged
merged 1 commit into from
Jul 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Next Release
* [#626](https://github.com/intridea/grape/pull/626): Mutually exclusive params - [@oliverbarnes](https://github.com/oliverbarnes).
* [#617](https://github.com/intridea/grape/pull/617): Running tests on Ruby 2.1.1, Rubinius 2.1 and 2.2, Ruby and JRuby HEAD - [@dblock](https://github.com/dblock).
* [#397](https://github.com/intridea/grape/pull/397): Adds `Grape::Endpoint.before_each` to allow easy helper stubbing - [@mbleigh](https://github.com/mbleigh).
* [#673](https://github.com/intridea/grape/pull/673): Avoid requiring non-existent fields when using Grape::Entity documentation - [@qqshfox](https://github.com/qqshfox).
* Your contribution here.

#### Fixes
Expand Down
7 changes: 5 additions & 2 deletions lib/grape/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,13 @@ def require_required_and_optional_fields(context, opts)
optional_fields = opts[:using].keys - required_fields
end
required_fields.each do |field|
requires(field, opts[:using][field])
field_opts = opts[:using][field]
raise ArgumentError, "required field not exist: #{field}" unless field_opts
requires(field, field_opts)
end
optional_fields.each do |field|
optional(field, opts[:using][field])
field_opts = opts[:using][field]
optional(field, field_opts) if field_opts
end
end

Expand Down
35 changes: 35 additions & 0 deletions spec/grape/validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,41 @@ def define_requires_none
end
end

context 'requires :all or :none but except a non-existent field using Grape::Entity documentation' do
context 'requires :all' do
def define_requires_all
documentation = {
required_field: { type: String },
optional_field: { type: String }
}
subject.params do
requires :all, except: :non_existent_field, using: documentation
end
end

it 'adds only the entity documentation to declared params, nothing more' do
define_requires_all
expect(subject.settings[:declared_params]).to eq([:required_field, :optional_field])
end
end

context 'requires :none' do
def define_requires_none
documentation = {
required_field: { type: String },
optional_field: { type: String }
}
subject.params do
requires :none, except: :non_existent_field, using: documentation
end
end

it 'adds only the entity documentation to declared params, nothing more' do
expect { define_requires_none }.to raise_error(ArgumentError)
end
end
end

context 'required with an Array block' do
before do
subject.params do
Expand Down