Skip to content

Commit

Permalink
Add test cases for '00' months and days in date and date-time objects
Browse files Browse the repository at this point in the history
  • Loading branch information
RulerOfTheQueendom committed Dec 11, 2024
1 parent 8527e92 commit 8e65e0c
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions openapi3/datetime_schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package openapi3

import (
"testing"

"github.com/stretchr/testify/require"
)

var DateSpec = []byte(`
components:
schemas:
Server:
properties:
date:
$ref: "#/components/schemas/timestamp"
name:
type: string
type: object
timestamp:
type: string
format: date
openapi: "3.0.1"
paths: {}
info:
version: 1.1.1
title: title
`[1:])

var DateTimeSpec = []byte(`
components:
schemas:
Server:
properties:
datetime:
$ref: "#/components/schemas/timestamp"
name:
type: string
type: object
timestamp:
type: string
format: date-time
openapi: "3.0.1"
paths: {}
info:
version: 1.1.1
title: title
`[1:])

func TestDateZeroMonth(t *testing.T) {
loader := NewLoader()
doc, err := loader.LoadFromData(DateSpec)
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",
"date": "2001-00-03",
})
require.ErrorContains(t, err, `Error at "/date": string doesn't match the format "date": string doesn't match pattern "`+FormatOfStringDate+`"`)
}

func TestDateZeroDay(t *testing.T) {
loader := NewLoader()
doc, err := loader.LoadFromData(DateSpec)
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",
"date": "2001-02-00",
})
require.ErrorContains(t, err, `Error at "/date": string doesn't match the format "date": string doesn't match pattern "`+FormatOfStringDate+`"`)
}

func TestDateTimeZeroMonth(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": "2001-00-03T04:05:06.789Z",
})
require.ErrorContains(t, err, `Error at "/datetime": string doesn't match the format "date-time": string doesn't match pattern "`+FormatOfStringDateTime+`"`)
}

func TestDateTimeZeroDay(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": "2001-02-00T04:05:06.789Z",
})
require.ErrorContains(t, err, `Error at "/datetime": string doesn't match the format "date-time": string doesn't match pattern "`+FormatOfStringDateTime+`"`)
}

0 comments on commit 8e65e0c

Please sign in to comment.