Description

ShouldBindJson panic with error: Undefined validation function 'enabled' on field 'Status'"

How to reproduce

package main

import (
    "github.com/gin-gonic/gin"
)

type DemoCmd struct {
    // enum: disabled, enabled
    Status string `json:"status" binding:"oneof=disabled,enabled"`
}

func main() {
    g := gin.Default()
    g.GET("/hello/:name", func(c *gin.Context) {
                cmd := DemoCmd{}
                err := c.ShouldBindJSON(&cmd)
                c.JSON(200,  cmd)
    })
    g.Run(":9000")
}

Expectations

return error

Actual result

panic with error: Undefined validation function 'enabled' on field 'Status'"

Environment

  • go version: 1.22.6
  • gin version (or commit ref): 1.10.0
  • operating system: linux

Comment From: abuzaforfagun

I have tried to reproduce the issue in docker. It works as expected.

Comment From: AetheWu

I have tried to reproduce the issue in docker. It works as expected.

I found the cause of the problem. In my project, other libraries depend on the package github.com/go-playground/validator/v10. This bug was introduced in validator@10.22.1.