Skip to content

Commit

Permalink
Merge branch 'master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
willnet authored Apr 18, 2024
2 parents 81feac6 + 4e8b27d commit a8f6648
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Unreleased

## 2.1.0 (2024-04-03)
### Fixed
* Add full-date compliant date format validation #164

## 2.0.0 (2023-10-15)

### Added

* Support for uniqueItems in array #154
Expand Down
6 changes: 5 additions & 1 deletion lib/openapi_parser/schema_validator/string_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,15 @@ def validate_date_format(value, schema)
return [value, nil] unless schema.format == 'date'

begin
Date.strptime(value, "%Y-%m-%d")
parsed_date = Date.iso8601(value)
rescue ArgumentError
return [nil, OpenAPIParser::InvalidDateFormat.new(value, schema.object_reference)]
end

unless parsed_date.strftime('%Y-%m-%d') == value
return [nil, OpenAPIParser::InvalidDateFormat.new(value, schema.object_reference)]
end

return [value, nil]
end

Expand Down
2 changes: 1 addition & 1 deletion lib/openapi_parser/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module OpenAPIParser
VERSION = '2.0.0'.freeze
VERSION = '2.1.0'.freeze
end
50 changes: 49 additions & 1 deletion spec/openapi_parser/schema_validator/string_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
end

context 'invalid' do
context 'error pattern' do
context 'arbitrary string' do
let(:value) { 'not_date' }
let(:params) { { 'date_str' => value } }

Expand All @@ -225,6 +225,54 @@
end
end
end

context 'date with YY-MM-DD' do
let(:value) { '21-02-12' }
let(:params) { { 'date_str' => value } }

it do
expect { subject }.to raise_error do |e|
expect(e).to be_kind_of(OpenAPIParser::InvalidDateFormat)
expect(e.message).to end_with("Value: \"21-02-12\" is not conformant with date format")
end
end
end

context 'date with YYYYMMDD' do
let(:value) { '20210212' }
let(:params) { { 'date_str' => value } }

it do
expect { subject }.to raise_error do |e|
expect(e).to be_kind_of(OpenAPIParser::InvalidDateFormat)
expect(e.message).to end_with("Value: \"20210212\" is not conformant with date format")
end
end
end

context 'date with YYMMDD' do
let(:value) { '210212' }
let(:params) { { 'date_str' => value } }

it do
expect { subject }.to raise_error do |e|
expect(e).to be_kind_of(OpenAPIParser::InvalidDateFormat)
expect(e.message).to end_with("Value: \"210212\" is not conformant with date format")
end
end
end

context 'datetime' do
let(:value) { '2021-02-12T12:59:00.000+09:00' }
let(:params) { { 'date_str' => value } }

it do
expect { subject }.to raise_error do |e|
expect(e).to be_kind_of(OpenAPIParser::InvalidDateFormat)
expect(e.message).to end_with("Value: \"2021-02-12T12:59:00.000+09:00\" is not conformant with date format")
end
end
end
end
end

Expand Down

0 comments on commit a8f6648

Please sign in to comment.