Skip to content

Commit f72e3a0

Browse files
NemoOudeismt-dfrey
andauthored
fix date string format validation (#130)
Quoting from the JSON schema validation vocabulary > date: A string instance is valid against this attribute if it is a > valid representation according to the "full-date" production. and from RFC 3339 ``` date-fullyear = 4DIGIT date-month = 2DIGIT ; 01-12 date-mday = 2DIGIT ; 01-28, 01-29, 01-30, 01-31 based on ; month/year full-date = date-fullyear "-" date-month "-" date-mday ``` The current implementations allows date strings that don't comply, for example `"12-12-12"`. The regex based check fixes that. References: * OAS 3.0.0 https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#dataTypes * JSON validation vocabulary https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#section-7.3 * RFC 3339, section 5.6 https://datatracker.ietf.org/doc/html/rfc3339#section-5.6 Co-authored-by: Nemo Oudeis <dfrey@moneytree.jp>
1 parent c34766c commit f72e3a0

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/openapi_parser/schema_validator/string_validator.rb

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ def validate_uuid_format(value, schema)
7171
def validate_date_format(value, schema)
7272
return [value, nil] unless schema.format == 'date'
7373

74+
return [nil, OpenAPIParser::InvalidDateFormat.new(value, schema.object_reference)] unless value =~ /^\d{4}-\d{2}-\d{2}$/
75+
7476
begin
7577
parsed_date = Date.iso8601(value)
7678
rescue ArgumentError

spec/openapi_parser/schema_validator/string_validator_spec.rb

+23-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,29 @@
236236
it do
237237
expect { subject }.to raise_error do |e|
238238
expect(e).to be_kind_of(OpenAPIParser::InvalidDateFormat)
239-
expect(e.message).to end_with("Value: \"not_date\" is not conformant with date format")
239+
expect(e.message).to end_with("Value: \"#{value}\" is not conformant with date format")
240+
end
241+
end
242+
end
243+
context 'incomplete date string (2 digit year)' do
244+
let(:value) { '12-12-12' }
245+
let(:params) { { 'date_str' => value } }
246+
247+
it do
248+
expect { subject }.to raise_error do |e|
249+
expect(e).to be_kind_of(OpenAPIParser::InvalidDateFormat)
250+
expect(e.message).to end_with("Value: \"#{value}\" is not conformant with date format")
251+
end
252+
end
253+
end
254+
context 'invalid date string format (/ separator)' do
255+
let(:value) { '12/12/12' }
256+
let(:params) { { 'date_str' => value } }
257+
258+
it do
259+
expect { subject }.to raise_error do |e|
260+
expect(e).to be_kind_of(OpenAPIParser::InvalidDateFormat)
261+
expect(e.message).to end_with("Value: \"#{value}\" is not conformant with date format")
240262
end
241263
end
242264
end

0 commit comments

Comments
 (0)