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 Sep 17, 2023
1 parent bcbbb97 commit 355d474
Show file tree
Hide file tree
Showing 9 changed files with 953 additions and 439 deletions.
5 changes: 5 additions & 0 deletions .github/docs/openapi3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,12 @@ type Schema struct{ ... }
func NewArraySchema() *Schema
func NewBoolSchema() *Schema
func NewBytesSchema() *Schema
func NewDateSchema() *Schema
func NewDateTimeSchema() *Schema
func NewFloat64Schema() *Schema
func NewHostnameSchema() *Schema
func NewIPv4Schema() *Schema
func NewIPv6Schema() *Schema
func NewInt32Schema() *Schema
func NewInt64Schema() *Schema
func NewIntegerSchema() *Schema
Expand All @@ -125,6 +129,7 @@ type SchemaValidationOption func(*schemaValidationSettings)
func EnableFormatValidation() SchemaValidationOption
func FailFast() SchemaValidationOption
func MultiErrors() SchemaValidationOption
func SetOpenAPIMinorVersion(minorVersion uint64) SchemaValidationOption
func SetSchemaErrorMessageCustomizer(f func(err *SchemaError) string) SchemaValidationOption
func VisitAsRequest() SchemaValidationOption
func VisitAsResponse() SchemaValidationOption
Expand Down
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 @@ -1628,7 +1656,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 `)
}

0 comments on commit 355d474

Please sign in to comment.