Skip to content

Commit 3929307

Browse files
committed
feat #582: fix date/date-time string formats
1 parent 0cbef63 commit 3929307

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

openapi3/schema_formats.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,11 @@ func init() {
172172
// The pattern supports base64 and b./ase64url. Padding ('=') is supported.
173173
DefineStringFormat("byte", `(^$|^[a-zA-Z0-9+/\-_]*=*$)`)
174174

175-
// date
176-
DefineStringFormat("date", `^[0-9]{4}-(0[0-9]|10|11|12)-([0-2][0-9]|30|31)$`)
177-
178-
// date-time
179-
DefineStringFormat("date-time", `^[0-9]{4}-(0[0-9]|10|11|12)-([0-2][0-9]|30|31)T[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?(Z|(\+|-)[0-9]{2}:[0-9]{2})?$`)
175+
// defined as full-date in https://www.rfc-editor.org/rfc/rfc3339#section-5.6
176+
DefineStringFormat("date", `^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|30|31)$`)
180177

178+
// defined as date-time in https://www.rfc-editor.org/rfc/rfc3339#section-5.6
179+
DefineStringFormat("date-time", `^[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T(23:59:60|(([01][0-9]|2[0-3])(:[0-5][0-9]){2}))(\.[0-9]+)?(Z|(\+|-)[0-9]{2}:[0-9]{2})?$`)
181180
}
182181

183182
// DefineIPv4Format opts in ipv4 format validation on top of OAS 3 spec

openapi3/schema_test.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,30 @@ var schemaExamples = []schemaExample{
364364
"56F5Bff4-Z4B6-48E6-a10D-B6CF66A83B04",
365365
},
366366
},
367-
367+
{
368+
Title: "STRING: format 'date'",
369+
Schema: NewDateSchema(),
370+
Serialization: map[string]interface{}{
371+
"type": "string",
372+
"format": "date",
373+
},
374+
AllValid: []interface{}{
375+
"2017-12-31",
376+
"2017-01-01",
377+
},
378+
AllInvalid: []interface{}{
379+
nil,
380+
3.14,
381+
"2017-12-00",
382+
"2017-12-32",
383+
"2017-13-01",
384+
"2017-00-31",
385+
"99-09-09",
386+
"2017-01-00",
387+
"2017-01-32",
388+
"2017-01-40",
389+
},
390+
},
368391
{
369392
Title: "STRING: format 'date-time'",
370393
Schema: NewDateTimeSchema(),
@@ -379,6 +402,7 @@ var schemaExamples = []schemaExample{
379402
"2017-12-31T11:59:59+11:30",
380403
"2017-12-31T11:59:59.999+11:30",
381404
"2017-12-31T11:59:59.999Z",
405+
"2017-12-31T23:59:60", // leap second
382406
},
383407
AllInvalid: []interface{}{
384408
nil,
@@ -387,6 +411,12 @@ var schemaExamples = []schemaExample{
387411
"2017-12-31T11:59:59\n",
388412
"2017-12-31T11:59:59.+11:30",
389413
"2017-12-31T11:59:59.Z",
414+
"2017-12-00T11:59:59.Z",
415+
"2017-12-32T11:59:59.Z",
416+
"2017-12-40T11:59:59.Z",
417+
"2017-12-00T11:59:59",
418+
"2017-12-31T11:59:60",
419+
"99-09-09T11:59:59",
390420
},
391421
},
392422

0 commit comments

Comments
 (0)