I am using https://github.com/go-playground/validator for a REST API server. I am using the same struct for different endpoint with different validation requirement. For example, /users/login
requires only email
and password
. However, to create a user, more information like firstName
would be required.
So I think using struct tag to validate is not very convenient. Checking the variable as described in this example seems more appropriate. However I run into two problems:
I am checking multiple at the same time. Is there a way to chain a series of validation together? Checking every single variable in a struct then check for error makes it harder to code and harder to read.
The error object is really empty. For example, checking if email is required shows only
err.Tag()
anderr.ActualTag()
asrequired
anderr.Kind()
anderr.Type()
asstring
and nothing else. Does the validator provide anything that makes this a little more convenient?