add validation
This commit is contained in:
9
main.go
9
main.go
@@ -12,6 +12,8 @@ import (
|
|||||||
"gorm.io/gorm/logger"
|
"gorm.io/gorm/logger"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -40,6 +42,13 @@ func main() {
|
|||||||
db.AutoMigrate(&model.Event{})
|
db.AutoMigrate(&model.Event{})
|
||||||
|
|
||||||
validate := validator.New()
|
validate := validator.New()
|
||||||
|
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
|
||||||
|
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
|
||||||
|
if name == "-" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return name
|
||||||
|
})
|
||||||
|
|
||||||
var h = &handler.Handler{DB: db, Validator: validate}
|
var h = &handler.Handler{DB: db, Validator: validate}
|
||||||
|
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ type Event struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type CreateEvent struct {
|
type CreateEvent struct {
|
||||||
Start time.Time `json:"start" validate:"required"`
|
Start time.Time `json:"start" validate:"required,gt"`
|
||||||
End time.Time `json:"end" validate:"required"`
|
End time.Time `json:"end" validate:"required,gtfield=Start"`
|
||||||
Person PersonalInformation `json:"person" validate:"required"`
|
Person *PersonalInformation `json:"person" validate:"required,dive"`
|
||||||
MedicId uuid.UUID `json:"medic_id" validate:"required"`
|
MedicId uuid.UUID `json:"medic_id" validate:"required"`
|
||||||
}
|
}
|
||||||
|
|||||||
10
model/pd.go
10
model/pd.go
@@ -3,11 +3,11 @@ package model
|
|||||||
import "github.com/google/uuid"
|
import "github.com/google/uuid"
|
||||||
|
|
||||||
type PersonalInformation struct {
|
type PersonalInformation struct {
|
||||||
ID uuid.UUID `json:"id,omitempty"`
|
ID uuid.UUID `json:"id,omitempty" validate:"required_without_all=FirstName LastName Patronymic Phone"`
|
||||||
FirstName string `json:"first_name,omitempty"`
|
FirstName string `json:"first_name,omitempty" validate:"required_without=ID"`
|
||||||
LastName string `json:"last_name,omitempty"`
|
LastName string `json:"last_name,omitempty" validate:"required_without=ID"`
|
||||||
Patronymic string `json:"patronymic,omitempty"`
|
Patronymic string `json:"patronymic,omitempty" validate:"required_without=ID"`
|
||||||
Phone string `json:"phone,omitempty"`
|
Phone string `json:"phone,omitempty" validate:"required_without=ID"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PersonalInformationCreate struct {
|
type PersonalInformationCreate struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user