go-playground / validator

:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
MIT License
16.64k stars 1.32k forks source link

Feat: tag to select child field #1235

Open MysteriousPotato opened 7 months ago

MysteriousPotato commented 7 months ago

Fixes Or Enhances

This PR adds a feature to allow selecting struct fields or map values for further validation.

This is useful when trying to apply validation tags to a wrapped value without having to use RegisterCustomTypeFunc with all possible types. When using the new sql.Null for example.

Usage

The usage would be similar to that of the dive tag in the sense that validation tags can be specified for the whole field or the selected field depending on whether they are placed before or after the select tag.

type MyStruct struct {
   Field sql.Null[uint] `validate:"omitempty,select=V,gt=10"`
}

Limitations

Make sure that you've checked the boxes below before you submit PR:

@go-playground/validator-maintainers

coveralls commented 7 months ago

Coverage Status

coverage: 74.217% (+0.009%) from 74.208% when pulling 7eb58afab34a368220d0a7ecbbc9c74e983d6c0b on MysteriousPotato:main into a0f74b0fb2a7ae1750c0f0b0a49550d8b6e2e708 on go-playground:master.