Consider the following:
type VM struct {
Hostname string `validate:"required"`
MemoryGB int `validate:"min=1024" default:"2048"`
}
With this struct, it'd be expected that sending {"Hostname":"example.com"} would work, with the MemoryGB defaulting to 2048 hence passing the validation min="1024"
However this doesn't seem to be the case right now:
nos-terraform-app | [GIN] 2025/04/04 - 19:27:50 | 400 | 1.819052ms | 172.18.0.2 | POST "/vms"
nos-terraform-app | Error #01: binding error: Key: 'CreateVMRequest.VM.MemoryGB' Error:Field validation for 'MemoryGB' failed on the 'min' tag
I'm suspecting that it's because the default values are not correctly initialized so in this example ?
|
// validating query and path inputs if they have a validate tag |
|
initValidator() |
|
args = append(args, input) |
|
if err := validatorObj.Struct(input.Interface()); err != nil { |
|
handleError(c, BindError{message: err.Error(), validationErr: err}) |
|
return |
|
} |
Also I was wondering how powerful the default tags are, currently using https://github.com/mcuadros/go-defaults concurrently (which support nested default, like default="{}" or default="[]") so I'd like to avoid any discrepancies between both
And one last question, maybe it may sound weird, but it there a way to toggle off the validate tags in some requests ?
The use case is to implement PATCH method, where basically the struct may be only partially filled and it's correct.
I don't want to redefine the exact same struct but without the required just for one method
Thanks a lot for the help !
Consider the following:
With this struct, it'd be expected that sending
{"Hostname":"example.com"}would work, with theMemoryGBdefaulting to2048hence passing the validationmin="1024"However this doesn't seem to be the case right now:
I'm suspecting that it's because the default values are not correctly initialized so in this example ?
gadgeto/tonic/handler.go
Lines 94 to 100 in 8c9ddef
Also I was wondering how powerful the
defaulttags are, currently using https://github.com/mcuadros/go-defaults concurrently (which support nested default, likedefault="{}"ordefault="[]") so I'd like to avoid any discrepancies between bothAnd one last question, maybe it may sound weird, but it there a way to toggle off the
validatetags in some requests ?The use case is to implement
PATCHmethod, where basically the struct may be only partially filled and it's correct.I don't want to redefine the exact same struct but without the required just for one method
Thanks a lot for the help !