Skip to content

Commit 6be685d

Browse files
committed
feat #582: use OpenAPI minor version during validation if available
1 parent 26029ec commit 6be685d

File tree

8 files changed

+940
-470
lines changed

8 files changed

+940
-470
lines changed

.github/docs/openapi3.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ type Schema struct{ ... }
102102
func NewDateSchema() *Schema
103103
func NewDateTimeSchema() *Schema
104104
func NewFloat64Schema() *Schema
105+
func NewHostnameSchema() *Schema
106+
func NewIPv4Schema() *Schema
107+
func NewIPv6Schema() *Schema
105108
func NewInt32Schema() *Schema
106109
func NewInt64Schema() *Schema
107110
func NewIntegerSchema() *Schema

openapi3/schema.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,13 +611,34 @@ func NewDateTimeSchema() *Schema {
611611
}
612612
}
613613

614+
func NewHostnameSchema() *Schema {
615+
return &Schema{
616+
Type: TypeString,
617+
Format: "hostname",
618+
}
619+
}
620+
614621
func NewUUIDSchema() *Schema {
615622
return &Schema{
616623
Type: TypeString,
617624
Format: "uuid",
618625
}
619626
}
620627

628+
func NewIPv4Schema() *Schema {
629+
return &Schema{
630+
Type: TypeString,
631+
Format: "ipv4",
632+
}
633+
}
634+
635+
func NewIPv6Schema() *Schema {
636+
return &Schema{
637+
Type: TypeString,
638+
Format: "ipv6",
639+
}
640+
}
641+
621642
func NewBytesSchema() *Schema {
622643
return &Schema{
623644
Type: TypeString,

openapi3/schema_formats.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,18 @@ func init() {
177177

178178
// defined as date-time in https://www.rfc-editor.org/rfc/rfc3339#section-5.6
179179
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})?$`)
180+
181+
// defined as uuid in https://www.rfc-editor.org/rfc/rfc4122
182+
DefineStringFormatStartingWithOpenAPIMinorVersion("uuid", 1, FormatOfStringForUUIDOfRFC4122, true)
183+
184+
// defined as ipv4 in
185+
DefineStringFormatCallbackStartingWithOpenAPIMinorVersion("ipv4", 1, validateIPv4, true)
186+
187+
// defined as ipv6 in https://www.rfc-editor.org/rfc/rfc4122
188+
DefineStringFormatCallbackStartingWithOpenAPIMinorVersion("ipv6", 1, validateIPv6, true)
189+
190+
// hostname as defined in https://www.rfc-editor.org/rfc/rfc1123#section-2.1
191+
DefineStringFormatStartingWithOpenAPIMinorVersion(`hostname`, 1, `^[a-zA-Z0-9][a-zA-Z0-9-.]+[a-zA-Z0-9]$`, true)
180192
}
181193

182194
// DefineIPv4Format opts in ipv4 format validation on top of OAS 3 spec

openapi3/schema_formats_test.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ func TestIssue430(t *testing.T) {
1616
NewStringSchema().WithFormat("ipv6"),
1717
)
1818

19-
delete(SchemaStringFormats, "ipv4")
20-
delete(SchemaStringFormats, "ipv6")
21-
2219
err := schema.Validate(context.Background())
2320
require.NoError(t, err)
2421

@@ -46,11 +43,8 @@ func TestIssue430(t *testing.T) {
4643
require.Error(t, err, ErrOneOfConflict.Error())
4744
}
4845

49-
DefineIPv4Format()
50-
DefineIPv6Format()
51-
5246
for datum, isV4 := range data {
53-
err = schema.VisitJSON(datum)
47+
err = schema.VisitJSON(datum, SetOpenAPIMinorVersion(1))
5448
require.NoError(t, err)
5549
if isV4 {
5650
require.Nil(t, validateIPv4(datum), "%q should be IPv4", datum)
@@ -78,8 +72,6 @@ func TestFormatCallback_WrapError(t *testing.T) {
7872
}
7973

8074
func TestReversePathInMessageSchemaError(t *testing.T) {
81-
DefineIPv4Format()
82-
8375
SchemaErrorDetailsDisabled = true
8476

8577
const spc = `
@@ -99,11 +91,11 @@ components:
9991

10092
err = doc.Components.Schemas["Something"].Value.VisitJSON(map[string]interface{}{
10193
`ip`: `123.0.0.11111`,
102-
})
94+
}, SetOpenAPIMinorVersion(1))
10395

104-
require.EqualError(t, err, `Error at "/ip": Not an IP address`)
96+
// assert, do not require to ensure SchemaErrorDetailsDisabled can be set to false
97+
assert.ErrorContains(t, err, `Error at "/ip"`)
10598

106-
delete(SchemaStringFormats, "ipv4")
10799
SchemaErrorDetailsDisabled = false
108100
}
109101

0 commit comments

Comments
 (0)