-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
183 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module OpenapiContracts::Validators | ||
# Validates the input parameters, eg path/url parameters | ||
class Parameters < Base | ||
include SchemaValidation | ||
|
||
private | ||
|
||
def validate | ||
operation.parameters.select(&:in_query?).each do |parameter| | ||
if request.GET.key?(parameter.name) | ||
value = request.GET[parameter.name] | ||
unless parameter.matches?(request.GET[parameter.name]) | ||
@errors << "#{value.inspect} is not a valid value for the query parameter #{parameter.name.inspect}" | ||
end | ||
elsif parameter.required? | ||
@errors << "Missing query parameter #{parameter.name.inspect}" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ Pet: | |
mapping: | ||
dog: '#/Dog' | ||
cat: '#/Cat' | ||
required: | ||
- type | ||
|
||
Cat: | ||
description: A cat | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
require 'active_support/core_ext/object/json' | ||
|
||
RSpec.describe OpenapiContracts::Validators::Parameters do | ||
subject { described_class.new(stack, env) } | ||
|
||
include_context 'when using GET /pets' | ||
|
||
let(:env) { OpenapiContracts::Env.new(operation:, request:, response:) } | ||
let(:operation) { doc.operation_for('/pets', method) } | ||
let(:stack) { ->(errors) { errors } } | ||
let(:doc) do | ||
OpenapiContracts::Doc.new( | ||
{ | ||
paths: { | ||
'/pets': { | ||
get: { | ||
parameters: [ | ||
{ | ||
in: 'query', | ||
name: 'order', | ||
required:, | ||
schema: { | ||
type: 'string', | ||
enum: %w(asc desc) | ||
} | ||
} | ||
], | ||
responses: { | ||
'200': { | ||
description: 'Ok', | ||
content: { | ||
'application/json': {} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}.as_json | ||
) | ||
end | ||
|
||
context 'when optional parameters are missing' do | ||
let(:path) { '/pets' } | ||
let(:required) { false } | ||
|
||
it 'has no errors' do | ||
expect(subject.call).to be_empty | ||
end | ||
end | ||
|
||
context 'when required parameters are missing' do | ||
let(:path) { '/pets' } | ||
let(:required) { true } | ||
|
||
it 'has errors' do | ||
expect(subject.call).to contain_exactly 'Missing query parameter "order"' | ||
end | ||
end | ||
|
||
context 'when required parameters are present' do | ||
let(:path) { '/pets?order=asc' } | ||
let(:required) { true } | ||
|
||
it 'has no errors' do | ||
expect(subject.call).to be_empty | ||
end | ||
end | ||
|
||
context 'when parameters are wrong' do | ||
let(:path) { '/pets?order=bad' } | ||
let(:required) { false } | ||
|
||
it 'has errors' do | ||
expect(subject.call).to contain_exactly '"bad" is not a valid value for the query parameter "order"' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters