Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"time"

"github.com/danielgtaylor/huma/v2/validation"
"github.com/google/uuid"
)

// ErrSchemaInvalid is sent when there is a problem building the schema.
Expand Down Expand Up @@ -45,6 +46,7 @@ var (
ipAddrType = reflect.TypeOf(netip.Addr{})
urlType = reflect.TypeOf(url.URL{})
rawMessageType = reflect.TypeOf(json.RawMessage{})
uuidType = reflect.TypeOf(uuid.UUID{})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO users should be able to inform which uuid library they are using, as a google library might not be their first option. @NicoleStrel What do you think?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah @NicoleStrel this adds a dependency on the Google UUID library and we are trying to keep dependencies to a minimum. I'd be open to a PR which doesn't require additional dependencies.

For now you can just set format:"uuid" like this: https://go.dev/play/p/swjPaRjVQcx

)

func deref(t reflect.Type) reflect.Type {
Expand Down Expand Up @@ -713,7 +715,7 @@ func schemaFromType(r Registry, t reflect.Type) *Schema {
return custom
}

// Handle special cases for known stdlib types.
// Handle special cases for known types.
switch t {
case timeType:
return &Schema{Type: TypeString, Nullable: isPointer, Format: "date-time"}
Expand All @@ -723,6 +725,8 @@ func schemaFromType(r Registry, t reflect.Type) *Schema {
return &Schema{Type: TypeString, Nullable: isPointer, Format: "ipv4"}
case ipAddrType:
return &Schema{Type: TypeString, Nullable: isPointer, Format: "ipv4"}
case uuidType:
return &Schema{Type: TypeString, Nullable: isPointer, Format: "uuid"}
case rawMessageType:
return &Schema{}
}
Expand Down
6 changes: 6 additions & 0 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -192,6 +193,11 @@ func TestSchema(t *testing.T) {
input: netip.AddrFrom4([4]byte{127, 0, 0, 1}),
expected: `{"type": "string", "format": "ipv4"}`,
},
{
name: "uuid",
input: uuid.UUID{},
expected: `{"type": "string", "format": "uuid"}`,
},
{
name: "json.RawMessage",
input: &json.RawMessage{},
Expand Down