{"record":{"id":"667d70b0bf6bc28e","repo":"flipped-aurora/gin-vue-admin","slug":"error-667d70","errorCode":null,"errorMessage":"格式校验不通过","messagePattern":"格式校验不通过","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"server/utils/validator.go","lineNumber":159,"sourceCode":"\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\n//@description: 长度和数字的校验方法 根据类型自动校验\n//@param: value reflect.Value, VerifyStr string\n//@return: bool\n","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/utils/validator.go#L141-L177","documentation":"Verify supports a `mj:` tag rule of the form regexp=<pattern>. When such a rule matches, it calls regexpMatch(pattern, val.String()); if the field's string value does not match the regular expression, it returns errors.New(fieldName + \"格式校验不通过\"), i.e. '<Field> failed format validation'. Note it uses val.String(), so for non-string kinds (int, float) the value is rendered via fmt/String semantics and may never match the intended pattern.","triggerScenarios":"A struct field tagged mj:\"regexp=^1[3-9][0-9]{9}$\" (or any pattern) receives a value that does not match the regex — e.g. a phone number with letters, an email without '@', or an integer field whose String() form does not fit a string-oriented pattern.","commonSituations":"Users submit IDs, phone numbers, or codes with wrong format; the regex pattern in the tag was written for a different format than the frontend enforces; the field is numeric but the regexp was designed for strings so val.String() yields an unexpected representation.","solutions":["Inspect the field's `mj:\"regexp=...\"` tag and test the actual value against the pattern (go test with regexp.MustCompile(pattern).MatchString).","Fix the client to send values in the format the regex expects, or align frontend validation with the server regex.","If the pattern itself is wrong/outdated, update the `mj:` tag to the correct regular expression.","For non-string fields, convert or re-type the field so val.String() returns a value the pattern can match (or drop the regexp rule for numeric fields)."],"exampleFix":"// before\nPhone string `json:\"phone\" mj:\"regexp=^1[3-9][0-9]{9}$\"` // value \"12ab\"\n\n// after\n// send a well-formed value or correct the pattern\nPhone string `json:\"phone\" mj:\"regexp=^\\\\d{11}$\"` // value \"13800138000\"","handlingStrategy":"validation","validationCode":"var emailRe = regexp.MustCompile(`^[\\w.+-]+@[\\w-]+\\.[\\w.]+$`)\nif !emailRe.MatchString(req.Email) {\n    return errors.New(\"email format invalid\")\n}","typeGuard":null,"tryCatchPattern":"if err := utils.Verify(req, utils.Rules{}); err != nil {\n    if strings.HasSuffix(err.Error(), \"格式校验不通过\") {\n        c.JSON(400, gin.H{\"msg\": \"输入格式不正确: \" + err.Error()})\n        return\n    }\n    return err\n}","preventionTips":["Keep frontend regexes identical to the `mj:regexp=` tags","Test every regexp tag with go tests using boundary values","Avoid regexp rules on non-string fields","Anchor patterns (^...$) to prevent accidental partial matches"],"tags":["go","validation","regex","gin-vue-admin"],"backgroundTag":"regex-validation-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}