Skip to content

Commit

Permalink
Merge pull request #164 from sasamuku/support_iso8601_compliant_date_…
Browse files Browse the repository at this point in the history
…format

Add full-date compliant date format validation
  • Loading branch information
sasamuku authored Apr 3, 2024
2 parents 7c5ff47 + c253794 commit 823a8c6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
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
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 823a8c6

Please sign in to comment.