PATCH http request validation of single field

Viewed 69

I have a struct snippet which is something like this

type Talent struct {
    Code               string   `json:"code" gorm:"type:varchar(10);not null"`
    FirstName          string   `json:"firstName" example:"Ravi" gorm:"type:varchar(50)"`
    LastName           string   `json:"lastName" example:"Sharma" gorm:"type:varchar(50)"`
    Email              string   `json:"email" example:"john@doe.com" gorm:"type:varchar(100)"`
    Contact            string   `json:"contact" example:"1234567890" gorm:"type:varchar(15)"`
    AcademicYear       *uint8    `json:"academicYear" example:"1" gorm:"type:tinyint(2)"`
    Type               *uint8   `json:"talentType" example:"4" gorm:"type:tinyint(2)"`
    Resume             *string  `json:"resume" gorm:"type:varchar(200)"`
    ExperienceInMonths *uint    `json:"experienceInMonths"`
    Image              *string  `json:"image" gorm:"type:varchar(200)"`
    Gender             *string  `json:"gender" gorm:"type:varchar(50)"`
    DateOfBirth        *string  `json:"dateOfBirth" gorm:"type:date"`
}

I want to update one field at a time, so I am sending only one field in json to the API as a PATCH http request and in backend(Golang) I am converting the json to struct where in only that field in json will have value in the struct... all other fields will be nil.

For example I am sending firstName, now when I convert it to struct only the firstName field is filled. Now I want to validate that field.. like firstName is a compulsary field so it will have two validations.. one for checking if its empty and one for the max number of characters.

Like wise all fields have some validations. Now how do I validate only that particular field. Having switch cases is what I had considered but is there any other optimal way to do this in golang??

0 Answers
Related