首頁 > 後端開發 > Golang > 主體

go中跨域自訂驗證的問題

WBOY
發布: 2024-02-11 23:18:09
轉載
939 人瀏覽過

go中跨域自訂驗證的問題

在Go語言開發中,跨域請求是一個常見的問題。跨域請求是指在瀏覽器中,透過JavaScript程式碼向不同網域下的伺服器傳送請求。由於瀏覽器的同源策略限制,跨域請求預設是不被允許的。然而,在某些場景下,我們可能需要在跨網域請求中進行自訂的驗證,以確保請求的安全性和準確性。本文將由php小編西瓜為您介紹如何在Go語言中解決跨域自訂驗證的問題,幫助您更好地應對跨域請求的挑戰。

問題內容

我正在嘗試學習 golang 自訂驗證,但遇到了很多麻煩。這是我一直在嘗試的程式碼:

package main
import (
        "reflect"
        "github.com/go-playground/validator/v10"
        "fmt"
)
type TeamMember struct {
        Country string
        Age int
        DropShip bool `validate:"is_eligible"`
}
func CustomValidation(fl validator.FieldLevel) bool {
  /*
    if(DropShip == true) {
       httpresponse = curl https://3rd-party-api.com/?country=<Country>&age=<Age>
       return httpresponse.code == 200
    }
    return false
  */

  b := fl.Parent()
  fmt.Println(reflect.TypeOf(b))
  fmt.Println(reflect.ValueOf(b))
  c := reflect.ValueOf(b).Interface()
  fmt.Println(c.(TeamMember))
  fmt.Println("============")
  return true
}


func main() {
  var validate *validator.Validate
  validate = validator.New(validator.WithRequiredStructEnabled())
  _ = validate.RegisterValidation("is_eligible", CustomValidation)
  teammember := TeamMember{"Canada", 34, true}

  validate.Struct(teammember)
}
登入後複製

您可以在程式碼註解中看到我嘗試的驗證邏輯...如果DropShip 欄位為true,那麼我需要將CountryAge提交到另一個API,以查看該團隊成員是否符合資格。 < /p>

問題是我正在努力使用 reflect 庫來存取 TeamMember 結構中的 CountryAge 欄位。 fmt.Println(c.(TeamMember)) 行使我的程式當機。

有人能給我一個如何存取其他 TeamMember 欄位的範例嗎?或者我的驗證方法是否違反了 golang 中驗證的慣用方式?

解決方法

在這種情況下,最好使用自訂結構層級驗證:

package main

import (
    "fmt"

    "github.com/go-playground/validator/v10"
)

type TeamMember struct {
    Country  string
    Age      int
    DropShip bool
}

func TeamMemberStructLevelValidation(sl validator.StructLevel) {
    teamMember := sl.Current().Interface().(TeamMember)

    if teamMember.DropShip {
        // submit the Country and Age to another API to see if this team member is eligible.
        if teamMember.Country == "Canada" && teamMember.Age == 34 {
            sl.ReportError(teamMember.Country, "country", "Country", "is_eligible", "")
            sl.ReportError(teamMember.Age, "age", "Age", "is_eligible", "")
        }
    }
}

func main() {
    validate := validator.New(validator.WithRequiredStructEnabled())
    validate.RegisterStructValidation(TeamMemberStructLevelValidation, TeamMember{})

    teamMember := TeamMember{"Canada", 34, true}
    err := validate.Struct(teamMember)

    fmt.Printf("%+v\n", err)
    // Output:
    //   Key: 'TeamMember.country' Error:Field validation for 'country' failed on the 'is_eligible' tag
    //   Key: 'TeamMember.age' Error:Field validation for 'age' failed on the 'is_eligible' tag
}
登入後複製

另請參閱套件提供的範例:https://www.php.cn/link/fe41bb826b6a3cd35fe36744936400b9

以上是go中跨域自訂驗證的問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!