{"record":{"id":"b72583d7e8e37361","repo":"flipped-aurora/gin-vue-admin","slug":"error-b72583","errorCode":null,"errorMessage":"值不能为空","messagePattern":"值不能为空","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"server/utils/validator.go","lineNumber":155,"sourceCode":"\tfor i := 0; i < num; i++ {\n\t\ttagVal := typ.Field(i)\n\t\tval := val.Field(i)\n\t\t// 只递归进【匿名内嵌】结构体(如 global.GVA_MODEL / request.PageInfo):\n\t\t// 递归的唯一目的是够到内嵌提升上来的字段(如 IdVerify 的 ID、PageInfoVerify 的 Page)。\n\t\t// 具名关联字段(User/Dept/Meta 等)是独立业务对象,不属于当前结构体自身,不应被扫描——\n\t\t// 否则关联对象内嵌的 GVA_MODEL.ID 恒为 0,会让 IdVerify 误报\"ID值不能为空\"。\n\t\t// 需要单独校验某个具名子结构时,调用方直接 Verify(x.Sub, rule)(项目既有用法)。\n\t\tif tagVal.Anonymous && tagVal.Type.Kind() == reflect.Struct {\n\t\t\tif err = Verify(val.Interface(), roleMap); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\t\tif len(roleMap[tagVal.Name]) > 0 {\n\t\t\tfor _, v := range roleMap[tagVal.Name] {\n\t\t\t\tswitch {\n\t\t\t\tcase v == \"notEmpty\":\n\t\t\t\t\tif isBlank(val) {\n\t\t\t\t\t\treturn errors.New(tagVal.Name + \"值不能为空\")\n\t\t\t\t\t}\n\t\t\t\tcase strings.Split(v, \"=\")[0] == \"regexp\":\n\t\t\t\t\tif !regexpMatch(strings.Split(v, \"=\")[1], val.String()) {\n\t\t\t\t\t\treturn errors.New(tagVal.Name + \"格式校验不通过\")\n\t\t\t\t\t}\n\t\t\t\tcase compareMap[strings.Split(v, \"=\")[0]]:\n\t\t\t\t\tif !compareVerify(val, v) {\n\t\t\t\t\t\treturn errors.New(tagVal.Name + \"长度或值不在合法范围,\" + v)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn nil\n}\n\n//@author: [piexlmax](https://github.com/piexlmax)\n//@function: compareVerify","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/utils/validator.go#L137-L173","documentation":"Verify iterates struct fields and the rules loaded from the `mj:` tag (roleMap). When a rule contains \"notEmpty\" and isBlank(val) reports the field as blank (zero value, empty string, whitespace, or zero-length), it returns errors.New(fieldName + \"值不能为空\"), i.e. '<Field> value cannot be empty'. The library throws this so callers get a precise, field-named validation failure instead of letting an empty required value pass through.","triggerScenarios":"Calling Verify on a struct whose `mj:\"notEmpty\"`-tagged field holds its zero value: empty string, 0, nil-time, empty slice/map, or whitespace-only string. E.g. a SysUser with empty NickName passed to TestVerify/TestVerifyIdWithNamedAssociation-style Verify calls.","commonSituations":"A form/API request omitted the field but the handler still runs Verify; JSON binding left the field at its zero value because the client sent \"\" or omitted the key; a default value was never applied before validation; white-space input passed frontend checks but fails isBlank on the server.","solutions":["Populate the required field before calling Verify, or return the field's error to the client and ask them to fill it in.","Trim/normalize input and treat whitespace-only as missing on the frontend so users get earlier feedback.","If the field is legitimately optional, remove \"notEmpty\" from its `mj:` tag instead of bypassing Verify.","Bind the request with Gin binding first (binding:\"required\") so empty payloads fail at the HTTP layer with a standard message."],"exampleFix":"// before\ntype Login struct { Username string `json:\"username\" mj:\"notEmpty\"` }\nu := Login{}\nutils.Verify(u, utils.Rules{}) // -> \"Username值不能为空\"\n\n// after\nu := Login{Username: \"admin\"}\nif err := utils.Verify(u, utils.Rules{}); err != nil {\n    c.String(400, err.Error())\n}","handlingStrategy":"validation","validationCode":"v := strings.TrimSpace(req.Username)\nif v == \"\" {\n    return errors.New(\"username is required\")\n}\nreq.Username = v\nreturn utils.Verify(req, utils.Rules{})","typeGuard":null,"tryCatchPattern":"if err := utils.Verify(req, utils.Rules{}); err != nil {\n    c.JSON(400, gin.H{\"msg\": err.Error()})\n    return\n}","preventionTips":["Mirror notEmpty rules with frontend required-field validation","Trim whitespace before validating","Never call Verify on a struct before binding/populating it","Review `mj:\"notEmpty\"` tags when fields become optional"],"tags":["go","validation","reflection","gin-vue-admin"],"backgroundTag":"required-field-empty","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}