Skip to content

Commit

Permalink
Add support for minItems, maxItems and uniqueItems for primitive arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
fotos committed Nov 23, 2018
1 parent 3d21cd5 commit ee3f775
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/grape-swagger/entity/attribute_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ def call(entity_options)
param[:enum] = values if values.is_a?(Array)
end

param = { type: :array, items: param } if documentation[:is_array]
if documentation[:is_array]
param = { type: :array, items: param }
param.merge!(documentation.slice(:minItems, :maxItems, :uniqueItems))
end

param
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/grape-swagger/entity/attribute_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@

it { is_expected.to include(type: :array) }
it { is_expected.to include(items: { type: 'string' }) }

context 'when it contains minItems' do
let(:entity_options) { { documentation: { type: 'string', desc: 'Colors', is_array: true, minItems: 1 } } }

it { is_expected.to include(minItems: 1) }
end

context 'when it contains minItems' do
let(:entity_options) { { documentation: { type: 'string', desc: 'Colors', is_array: true, maxItems: 1 } } }

it { is_expected.to include(maxItems: 1) }
end

context 'when it contains minItems' do
let(:entity_options) { { documentation: { type: 'string', desc: 'Colors', is_array: true, uniqueItems: true } } }

it { is_expected.to include(uniqueItems: true) }
end
end

context 'when it is not exposed as an array' do
Expand Down

0 comments on commit ee3f775

Please sign in to comment.