Golang

Golangバリデーション go-playground validator

Golang

https://github.com/go-playground/validator

 

まとめてくれてる記事があり助かる
https://qiita.com/RunEagler/items/ad79fc860c3689797ccc

 

やってみる

 

 

ここのサンプルを参考
https://github.com/go-playground/validator/blob/master/_examples/simple/main.go

 

import (
  "github.com/go-playground/validator/v10"
  "time"
  "fmt"
)

type CreateBirthDayRequest struct {
  UserId int  `validate:"required,min=1"`
  Date string `validate:"required,is_date"`
}

type ValidationPool struct {
  CreateBirthDayRequests []*CreateBirthDayRequest `validate:"required,dive,required"`
}


var validate *validator.Validate

func IsValid() {
	validate = validator.New()
  validate.RegisterValidation("is_date", IsDate)
	validateStruct()
}

func validateStruct() {
                 # Good Data
	request1 := &CreateBirthDayRequest{
		UserId: 1,
		Date: "2025-10-30",
	}
                 # Bad Data ... Bad Date
	request2 := &CreateBirthDayRequest{
		UserId: 1,
		Date: "baddate-2025-10-30-xxxxx",
	}
                 # Bad Data ... Bad UserId
	request3 := &CreateBirthDayRequest{
		UserId: 0,
		Date: "2025-10-30",
	}
                 # Good Data
	request4 := &CreateBirthDayRequest{
		UserId: 1,
		Date: "2025-11-01",
	}
  var array []*CreateBirthDayRequest

  array = append(array, request1)
  array = append(array, request2)
  array = append(array, request3)
  array = append(array, request4)
  pool := &ValidationPool{CreateBirthDayRequests: array}
  //pool := &ValidationPool{CreateBirthDayRequests: []*CreateBirthDayRequest{request}}
	err := validate.Struct(pool)
	if err != nil {
		if _, ok := err.(*validator.InvalidValidationError); ok {
			fmt.Println(err)
			return
		}
		for _, err := range err.(validator.ValidationErrors) {
			fmt.Println(err.Namespace())
			fmt.Println(err.Field())
			fmt.Println(err.StructNamespace())
			fmt.Println(err.StructField())
			fmt.Println(err.Tag())
			fmt.Println(err.ActualTag())
			fmt.Println(err.Kind())
			fmt.Println(err.Type())
			fmt.Println(err.Value())
			fmt.Println(err.Param())
			fmt.Println()
		}
		return
	}
}

func IsDate(fl validator.FieldLevel) bool {
  _, err := time.Parse("2006-01-02", fl.Field().String())
  if err != nil {
    return false
  }
  return true
}

 

結果

 

ValidationPool.CreateBirthDayRequests[1].Date
Date
ValidationPool.CreateBirthDayRequests[1].Date
Date
is_date
is_date
string
string
baddate-2025-10-30-xxxxx


ValidationPool.CreateBirthDayRequests[2].UserId
UserId
ValidationPool.CreateBirthDayRequests[2].UserId
UserId
required
required
int
int
0


2023/01/07 16:48:00 end valid
2023/01/07 16:48:00 start v
2023/01/07 16:48:00 {0 2025-10fdsaf-30fad}
2023/01/07 16:48:00 end v
2023/01/07 16:48:00 start valid
ValidationPool.CreateBirthDayRequests[1].Date
Date
ValidationPool.CreateBirthDayRequests[1].Date
Date
is_date
is_date
string
string
baddate-2025-10-30-xxxxx


ValidationPool.CreateBirthDayRequests[2].UserId
UserId
ValidationPool.CreateBirthDayRequests[2].UserId
UserId
required
required
int
int
0

 

 

 

 

Amazonおすすめ

iPad 9世代 2021年最新作

iPad 9世代出たから買い替え。安いぞ!🐱 初めてならiPad。Kindleを外で見るならiPad mini。ほとんどの人には通常のiPadをおすすめします><

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)