Open AaronRobson opened 3 years ago
Hey @AaronRobson this is not the first time I've gotten this requisition for various reasons. I can see how having these functions exposed could be beneficial.
My main concern is that it would dramatically increase the surface area of this library.
WDYT if I created a separate repo with these raw exposed validations and then changed this lib to use it? then can get the best of both worlds?
Sounds like a good solution to me. Thank you for the consideration.
It's unclear to me, what was the resolution of this? I bumped into the same situation.
I have a string of comma separated country codes that is omitempty
but I also want to be able to validate it as a 2 letter country code list with iso3166_1_alpha2
. I cannot just put the baked in validator because the comma separated list is not seen as an array for dive
.
My main concern is that it would dramatically increase the surface area of this library.
This functions are not exposed as go functions but if implementation of for example isUuid was changed in a braking way, it will be braking change for the lib as well. So this means to me that validations are already part of api surface of this library.
@deankarn We can use specific baked-in tags to trigger specific baked-in validation functions associated with the tags. How validations work is already part of the API.
By exposing baked-in functions, what's increased in the surface area is that users can now use validations not only on struct fields but also on everything they are concerned about. I think that the following are all safe to be exposed:
aaaa
calls the function IsAAAA
.IsAAAA
has a specific signature (namely, func (FieldLevel) bool
)I have a similar use case for exposing baked in validators, I would like to extend them to cover additional types, e.g. github.com/shopspring/decimal.Decimal
.
In the meanwhile I have been using go's go:linkname
compile directive to have access to the private functions.
import (
_ "unsafe"
"github.com/go-playground/validator/v10"
)
//go:linkname hasMinOf github.com/go-playground/validator/v10.hasMinOf
func hasMinOf(fl validator.FieldLevel) bool
func MinOf(fl validator.FieldLevel) bool {
switch v := fl.Field().Interface().(type) {
case decimal.Decimal:
param, err := decimal.NewFromString(fl.Param())
if err != nil {
return false
}
return v.GreaterThan(param)
default:
return hasMinOf(fl)
}
}
// validatorInstance.RegisterValidation("min", MinOf)
I have a similar use case for exposing baked in validators, I would like to extend them to cover additional types, e.g. github.com/shopspring/decimal.Decimal.
Same. It's sad that this seems ultimately caused by go's paternalistic unwillingness to allow operator overloading even for strictly numeric types, but here we are.
Package version eg. v9, v10:
v10
Issue, Question or Enhancement:
Enhancement - to allow custom validators to use the baked in ones as building blocks (https://github.com/go-playground/validator/blob/d4271985b44b735c6f76abc7a06532ee997f9476/baked_in.go#L477)
If the
isUuid
etc. functions started with a capital letter (i.e.IsUuid
etc.) they'd be available to users of the library.Code sample, to showcase or reproduce:
Modelled after: https://github.com/go-playground/validator/blob/master/_examples/custom-validation/main.go