validator icon indicating copy to clipboard operation
validator copied to clipboard

How to implement group validator?

Open nogolang opened this issue 4 years ago • 1 comments

How to implement group validator?

I have the following structure

type User struct {
	UserId        int64          `binding:"required,number"`
	Name         string         `json:"name" form:"name" binding:"gte=3"`
}

Now I need to implement findUserById , I have to verify the ID exist, so i use required. But when I implement saveUser, I have to verify that the ID doesn't exist because my database is a self-increment ID. They have a conflict

image

In Java spring boot, they can be distinguished by a custom group

nogolang avatar Sep 30 '21 08:09 nogolang

The honest answer would be to break your Find + Save User struct up into two separate ones. I highly recommend doing this regardless otherwise you'll eventually end up with abstraction leakage between the two use case resulting in less maintainable and more brittle code.

I can't see adding this sort of grouping functionality support.

Hope this helps answer the question.

deankarn avatar Oct 23 '21 22:10 deankarn