Skip to content

Commit

Permalink
Fix hour/minute/second matches as well - also update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RulerOfTheQueendom committed Dec 11, 2024
1 parent 32f793a commit 80d23e5
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .github/docs/openapi3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ const (
FormatOfStringByte = `(^$|^[a-zA-Z0-9+/\-_]*=*$)`

// FormatOfStringDate is a RFC3339 date format regexp, for example "2017-07-21".
FormatOfStringDate = `^[0-9]{4}-(0[1-9]|10|11|12)-(0[1-9]|[1-2][0-9]|3[0-1])$`
FormatOfStringDate = `^[0-9]{4}-(0[1-9]|10|11|12)-(0[1-9]|[12][0-9]|3[01])$`

// FormatOfStringDateTime is a RFC3339 date-time format regexp, for example "2017-07-21T17:32:28Z".
FormatOfStringDateTime = `^[0-9]{4}-(0[1-9]|10|11|12)-(0[1-9]|[1-2][0-9]|3[0-1])T[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?(Z|(\+|-)[0-9]{2}:[0-9]{2})?$`
FormatOfStringDateTime = `^[0-9]{4}-(0[1-9]|10|11|12)-(0[1-9]|[12][0-9]|3[01])T([0-1][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\.[0-9]+)?(Z|(\+|-)[0-9]{2}:[0-9]{2})?$`
)
const (
SerializationSimple = "simple"
Expand Down
60 changes: 60 additions & 0 deletions openapi3/datetime_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,63 @@ func TestDateTimeZeroDay(t *testing.T) {
})
require.EqualError(t, err, `Error at "/datetime": string doesn't match the format "date-time": string doesn't match pattern "`+FormatOfStringDateTime+`"`)
}

func TestDateTimeLeapSecond(t *testing.T) {
loader := NewLoader()
doc, err := loader.LoadFromData(DateTimeSpec)
require.NoError(t, err)

err = doc.Validate(loader.Context)
require.NoError(t, err)

err = doc.Components.Schemas["Server"].Value.VisitJSON(map[string]any{
"name": "kin-openapi",
"datetime": "2016-12-31T23:59:60.000Z", // exact time of the most recent leap second
})
require.NoError(t, err)
}

func TestDateTimeHourOutOfBounds(t *testing.T) {
loader := NewLoader()
doc, err := loader.LoadFromData(DateTimeSpec)
require.NoError(t, err)

err = doc.Validate(loader.Context)
require.NoError(t, err)

err = doc.Components.Schemas["Server"].Value.VisitJSON(map[string]any{
"name": "kin-openapi",
"datetime": "2016-12-31T24:00:00.000Z",
})
require.EqualError(t, err, `Error at "/datetime": string doesn't match the format "date-time": string doesn't match pattern "`+FormatOfStringDateTime+`"`)
}

func TestDateTimeMinuteOutOfBounds(t *testing.T) {
loader := NewLoader()
doc, err := loader.LoadFromData(DateTimeSpec)
require.NoError(t, err)

err = doc.Validate(loader.Context)
require.NoError(t, err)

err = doc.Components.Schemas["Server"].Value.VisitJSON(map[string]any{
"name": "kin-openapi",
"datetime": "2016-12-31T23:60:00.000Z",
})
require.EqualError(t, err, `Error at "/datetime": string doesn't match the format "date-time": string doesn't match pattern "`+FormatOfStringDateTime+`"`)
}

func TestDateTimeSecondOutOfBounds(t *testing.T) {
loader := NewLoader()
doc, err := loader.LoadFromData(DateTimeSpec)
require.NoError(t, err)

err = doc.Validate(loader.Context)
require.NoError(t, err)

err = doc.Components.Schemas["Server"].Value.VisitJSON(map[string]any{
"name": "kin-openapi",
"datetime": "2016-12-31T23:59:61.000Z",
})
require.EqualError(t, err, `Error at "/datetime": string doesn't match the format "date-time": string doesn't match pattern "`+FormatOfStringDateTime+`"`)
}
4 changes: 2 additions & 2 deletions openapi3/schema_formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ const (
FormatOfStringByte = `(^$|^[a-zA-Z0-9+/\-_]*=*$)`

// FormatOfStringDate is a RFC3339 date format regexp, for example "2017-07-21".
FormatOfStringDate = `^[0-9]{4}-(0[1-9]|10|11|12)-(0[1-9]|[1-2][0-9]|3[0-1])$`
FormatOfStringDate = `^[0-9]{4}-(0[1-9]|10|11|12)-(0[1-9]|[12][0-9]|3[01])$`

// FormatOfStringDateTime is a RFC3339 date-time format regexp, for example "2017-07-21T17:32:28Z".
FormatOfStringDateTime = `^[0-9]{4}-(0[1-9]|10|11|12)-(0[1-9]|[1-2][0-9]|3[0-1])T[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?(Z|(\+|-)[0-9]{2}:[0-9]{2})?$`
FormatOfStringDateTime = `^[0-9]{4}-(0[1-9]|10|11|12)-(0[1-9]|[12][0-9]|3[01])T([0-1][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\.[0-9]+)?(Z|(\+|-)[0-9]{2}:[0-9]{2})?$`
)

func init() {
Expand Down

0 comments on commit 80d23e5

Please sign in to comment.