go-playground / validator

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

feat: custom error mgs with tag (generic function) #1120

Open thanhphuchuynh opened 1 year ago

thanhphuchuynh commented 1 year ago

Customizing Error Messages with Tags

In addition to specifying constraints for struct fields, we can also customize error messages using tags. To do this, we add a message tag to our struct field tags.


type PaymentInfo struct {
    CreditCardNumber string `validate:"required" errormgs:"Invalid credit card is required xxx"`
    CVV              string `validate:"required,len=3" errormgs:"CVV code must be three digits long"`
}
coveralls commented 1 year ago

Coverage Status

coverage: 73.967% (-0.005%) from 73.972% when pulling cba5d112193cafa873f9b7bf2d8677c87d52d986 on ThanhPhucHuynh:feat/errortag into bd1113d5c1901c5205fc0a7af031a11268ff13ee on go-playground:master.