How to implement a PATCH request for an object with 10+ fields with specific validation

157 Views Asked by At

I am using gorm v1 and gorilla/mux (REST API)

let's say I have

type User struct{

  A string

  B int

  C int

  .

  .

  Z string

}

Fields from A-H are mandatory and have a few other validations on them like

min/max size

no spaces

etc

Now, If I have a request
PATCH: api/v1/user/1

Body: {C:10}

How do I figure out if C was given as body in my API endpoint so that I can validate C (or any given field) should I be doing this with a lot of if's and switch cases?

1

There are 1 best solutions below

0
vishal sharma On

You can do this via go-playground/validator. If you don't wanna use external library, then you'll have to use reflect package, that's what essentially the validator package does as well.