Model Validation#
Model Validation#
Huma includes a utility to make it a little easier to validate models outside of the normal HTTP request/response flow, for example on app startup to load example or default data and verify it is correct. This is just a thin wrapper around the built-in validation functionality, but abstracts away some of the boilerplate required for efficient operation and provides a simple API.
code.go
type MyExample struct {
Name string `json:"name" maxLength:"5"`
Age int `json:"age" minimum:"25"`
}
var value any
json.Unmarshal([]byte(`{"name": "abcdefg", "age": 1}`), &value)
validator := huma.ModelValidator()
errs := validator.Validate(reflect.TypeOf(MyExample{}), value)
if errs != nil {
fmt.Println("Validation error", errs)
}
Concurrency
The huma.ModelValidator
is not goroutine-safe! For more flexible validation, use the huma.Validate
function directly and provide your own registry, path buffer, validation result struct, etc.
Dive Deeper#
- Reference
huma.ModelValidator
the model validator utility
- External Links
- See Also