Closed fourcels closed 4 months ago
The change in the validate.go
file involves updating the comparison logic within a loop that checks for the presence of a value in a list of enums. The enhancement ensures that the enum value is converted to a string before performing the comparison, improving the accuracy and reliability of the validation process.
File | Change Summary |
---|---|
validate.go |
Modified comparison logic within the loop to convert the enum value to a string before comparison. |
In the code where enums play, A string conversion saves the day. Validation now more true, Ensures the values pass on through. 🎉 Hooray for changes, hip-hip-hooray! 🎉
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 92.84%. Comparing base (
158b076
) to head (355b230
).
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
@danielgtaylor i think this issue relates to #438. A good solution on #438 solves the problem here and avoids the use of a fmt.Sprint which allocates and seems a bit hacky
@danielgtaylor is there any chances to get support for enum types?
type VehicleType string
const (
VehicleTypeCar VehicleType = "car"
VehicleTypeTruck VehicleType = "truck"
VehicleTypeSpecial VehicleType = "special"
)
func (*VehicleType) Schema(r huma.Registry) *huma.Schema {
schema := huma.SchemaFromType(r, reflect.TypeFor[string]()) // always complains that the validation failed
schema.Enum = []any{VehicleTypeCar, VehicleTypeTruck, VehicleTypeSpecial}
return schema
}
func (*VehicleType) Schema(r huma.Registry) *huma.Schema {
schema := huma.SchemaFromType(r, reflect.TypeFor[VehicleType]()) // panics
schema.Enum = []any{VehicleTypeCar, VehicleTypeTruck, VehicleTypeSpecial}
return schema
}
Actually enums are quite common in API schemas
@codercms have a try
func (*VehicleType) Schema(r huma.Registry) *huma.Schema {
schema := huma.SchemaFromType(r, reflect.TypeFor[string]()) // always complains that the validation failed
schema.Enum = []any{string(VehicleTypeCar), string(VehicleTypeTruck), string(VehicleTypeSpecial)}
return schema
}
@fourcels it works, thanks :)
Summary by CodeRabbit