Skip to content

Commit

Permalink
feat #582: use OpenAPI minor version during validation when available
Browse files Browse the repository at this point in the history
  • Loading branch information
cboitel committed Jul 31, 2023
1 parent 808300a commit eaa7245
Show file tree
Hide file tree
Showing 8 changed files with 948 additions and 439 deletions.
2 changes: 2 additions & 0 deletions openapi3/issue735_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func TestIssue735(t *testing.T) {
DefineStringFormat("email", FormatOfStringForEmail)
DefineIPv4Format()
DefineIPv6Format()
// restore modified string formats used during this tests
defer RestoreDefaultStringFormats()

testCases := []testCase{
{
Expand Down
30 changes: 29 additions & 1 deletion openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,20 +579,48 @@ func NewStringSchema() *Schema {
}
}

func NewDateSchema() *Schema {
return &Schema{
Type: TypeString,
Format: "date",
}
}

func NewDateTimeSchema() *Schema {
return &Schema{
Type: TypeString,
Format: "date-time",
}
}

func NewHostnameSchema() *Schema {
return &Schema{
Type: TypeString,
Format: "hostname",
}
}

func NewUUIDSchema() *Schema {
return &Schema{
Type: TypeString,
Format: "uuid",
}
}

func NewIPv4Schema() *Schema {
return &Schema{
Type: TypeString,
Format: "ipv4",
}
}

func NewIPv6Schema() *Schema {
return &Schema{
Type: TypeString,
Format: "ipv6",
}
}

func NewBytesSchema() *Schema {
return &Schema{
Type: TypeString,
Expand Down Expand Up @@ -1624,7 +1652,7 @@ func (schema *Schema) visitJSONString(settings *schemaValidationSettings, value
var formatStrErr string
var formatErr error
if format := schema.Format; format != "" {
if f, ok := SchemaStringFormats[format]; ok {
if f := getSchemaStringFormats(format, settings.openapiMinorVersion); f != nil {
switch {
case f.regexp != nil && f.callback == nil:
if cp := f.regexp; !cp.MatchString(value) {
Expand Down
2 changes: 1 addition & 1 deletion openapi3/schema_issue492_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ info:
"name": "kin-openapi",
"time": "2001-02-03T04:05:06:789Z",
})
require.ErrorContains(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})?$")`)
require.ErrorContains(t, err, `Error at "/time": string doesn't match the format "date-time" (regular expression `)
}
Loading

0 comments on commit eaa7245

Please sign in to comment.