Skip to content

Commit

Permalink
feat: support nil uuid (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
zekth authored Mar 24, 2023
1 parent e53fe38 commit cc09e84
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/docs/openapi3.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ParameterInPath = "path" ...
const TypeArray = "array" ...
const FormatOfStringForUUIDOfRFC4122 = `^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$` ...
const FormatOfStringForUUIDOfRFC4122 = `^(?:[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000)$` ...
const SerializationSimple = "simple" ...
var SchemaErrorDetailsDisabled = false ...
var CircularReferenceCounter = 3
Expand Down
2 changes: 1 addition & 1 deletion openapi3/schema_formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

const (
// FormatOfStringForUUIDOfRFC4122 is an optional predefined format for UUID v1-v5 as specified by RFC4122
FormatOfStringForUUIDOfRFC4122 = `^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$`
FormatOfStringForUUIDOfRFC4122 = `^(?:[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000)$`

// FormatOfStringForEmail pattern catches only some suspiciously wrong-looking email addresses.
// Use DefineStringFormat(...) if you need something stricter.
Expand Down
49 changes: 49 additions & 0 deletions openapi3/schema_formats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package openapi3
import (
"context"
"errors"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -105,3 +106,51 @@ components:
delete(SchemaStringFormats, "ipv4")
SchemaErrorDetailsDisabled = false
}

func TestUuidFormat(t *testing.T) {

type testCase struct {
name string
value string
wantErr bool
}

DefineStringFormat("uuid", FormatOfStringForUUIDOfRFC4122)
testCases := []testCase{
{
name: "invalid",
value: "foo",
wantErr: true,
},
{
name: "uuid v1",
value: "77e66540-ca29-11ed-afa1-0242ac120002",
wantErr: false,
},
{
name: "uuid v4",
value: "00f4d301-b9f4-4366-8907-2b5a03430aa1",
wantErr: false,
},
{
name: "uuid nil",
value: "00000000-0000-0000-0000-000000000000",
wantErr: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := NewUUIDSchema().VisitJSON(tc.value)
var schemaError = &SchemaError{}
if tc.wantErr {
require.Error(t, err)
require.ErrorAs(t, err, &schemaError)

require.NotZero(t, schemaError.Reason)
require.NotContains(t, schemaError.Reason, fmt.Sprint(tc.value))
} else {
require.Nil(t, err)
}
})
}
}

0 comments on commit cc09e84

Please sign in to comment.