Skip to content

Commit

Permalink
feat #582: fix date/date-time string formats
Browse files Browse the repository at this point in the history
  • Loading branch information
cboitel committed Jan 17, 2023
1 parent dab9068 commit 26029ec
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
9 changes: 4 additions & 5 deletions openapi3/schema_formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,11 @@ func init() {
// The pattern supports base64 and b./ase64url. Padding ('=') is supported.
DefineStringFormat("byte", `(^$|^[a-zA-Z0-9+/\-_]*=*$)`)

// date
DefineStringFormat("date", `^[0-9]{4}-(0[0-9]|10|11|12)-([0-2][0-9]|30|31)$`)

// date-time
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})?$`)
// defined as full-date in https://www.rfc-editor.org/rfc/rfc3339#section-5.6
DefineStringFormat("date", `^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|30|31)$`)

// defined as date-time in https://www.rfc-editor.org/rfc/rfc3339#section-5.6
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})?$`)
}

// DefineIPv4Format opts in ipv4 format validation on top of OAS 3 spec
Expand Down
3 changes: 2 additions & 1 deletion openapi3/schema_issue492_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ openapi: "3.0.1"
"name": "kin-openapi",
"time": "2001-02-03T04:05:06:789Z",
})
require.EqualError(t, err, "Error at \"/time\": string doesn't match the format \"date-time\" (regular expression \"^[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})?$\")\nSchema:\n {\n \"format\": \"date-time\",\n \"type\": \"string\"\n }\n\nValue:\n \"2001-02-03T04:05:06:789Z\"\n")
require.Error(t, err)
require.Contains(t, err.Error(), "Error at \"/time\": string doesn't match the format \"date-time\" (regular expression")
}
32 changes: 31 additions & 1 deletion openapi3/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,30 @@ var schemaExamples = []schemaExample{
"56F5Bff4-Z4B6-48E6-a10D-B6CF66A83B04",
},
},

{
Title: "STRING: format 'date'",
Schema: NewDateSchema(),
Serialization: map[string]interface{}{
"type": "string",
"format": "date",
},
AllValid: []interface{}{
"2017-12-31",
"2017-01-01",
},
AllInvalid: []interface{}{
nil,
3.14,
"2017-12-00",
"2017-12-32",
"2017-13-01",
"2017-00-31",
"99-09-09",
"2017-01-00",
"2017-01-32",
"2017-01-40",
},
},
{
Title: "STRING: format 'date-time'",
Schema: NewDateTimeSchema(),
Expand All @@ -379,6 +402,7 @@ var schemaExamples = []schemaExample{
"2017-12-31T11:59:59+11:30",
"2017-12-31T11:59:59.999+11:30",
"2017-12-31T11:59:59.999Z",
"2017-12-31T23:59:60", // leap second
},
AllInvalid: []interface{}{
nil,
Expand All @@ -387,6 +411,12 @@ var schemaExamples = []schemaExample{
"2017-12-31T11:59:59\n",
"2017-12-31T11:59:59.+11:30",
"2017-12-31T11:59:59.Z",
"2017-12-00T11:59:59.Z",
"2017-12-32T11:59:59.Z",
"2017-12-40T11:59:59.Z",
"2017-12-00T11:59:59",
"2017-12-31T11:59:60",
"99-09-09T11:59:59",
},
},

Expand Down

0 comments on commit 26029ec

Please sign in to comment.