go-ozzo / ozzo-validation

An idiomatic Go (golang) validation package. Supports configurable and extensible validation rules (validators) using normal language constructs instead of error-prone struct tags.
MIT License
3.73k stars 224 forks source link

Validation Errors should use JSON tag values as the error keys when marshalling. #2

Closed v4n closed 8 years ago

v4n commented 8 years ago

Related to https://github.com/go-ozzo/ozzo-validation/pull/1, when marshalling validation.Errors It would be great if we can detect struct fields with the json tag and use that as the key in the map for validation.Errors.

A option for doing this is to convert validation.Errors to a struct that contains two fields Errs map[string]err & ErrsJSON map[string]string. Errs map key contains the Struct field name and ErrsJSON map key contains the Struct json tag value.

Another option would be to use a custom field tag e.g. validation that is checked for and if specified we use it as the map key for the error message. This isn't specifically meant to be used when marshalling json.

@qiangxue thoughts?

qiangxue commented 8 years ago

Very reasonable requirement. I like your second option better as it is not JSON specific.