flipped-aurora/gin-vue-admin · warning
长度或值不在合法范围,
Error message
长度或值不在合法范围,
What it means
Verify's compare rules come from compareMap and are dispatched to compareVerify(val, v). When a rule such as max=10, min=1, or eq=xxx fails, Verify returns errors.New(fieldName + "长度或值不在合法范围," + v) — '<Field> length or value not in legal range, <rule>'. The raw rule text is appended so the caller can see which comparison failed.
Source
Thrown at server/utils/validator.go:163
if tagVal.Anonymous && tagVal.Type.Kind() == reflect.Struct {
if err = Verify(val.Interface(), roleMap); err != nil {
return err
}
}
if len(roleMap[tagVal.Name]) > 0 {
for _, v := range roleMap[tagVal.Name] {
switch {
case v == "notEmpty":
if isBlank(val) {
return errors.New(tagVal.Name + "值不能为空")
}
case strings.Split(v, "=")[0] == "regexp":
if !regexpMatch(strings.Split(v, "=")[1], val.String()) {
return errors.New(tagVal.Name + "格式校验不通过")
}
case compareMap[strings.Split(v, "=")[0]]:
if !compareVerify(val, v) {
return errors.New(tagVal.Name + "长度或值不在合法范围," + v)
}
}
}
}
}
return nil
}
//@author: [piexlmax](https://github.com/piexlmax)
//@function: compareVerify
//@description: 长度和数字的校验方法 根据类型自动校验
//@param: value reflect.Value, VerifyStr string
//@return: bool
func compareVerify(value reflect.Value, VerifyStr string) bool {
switch value.Kind() {
case reflect.String:
return compare(len([]rune(value.String())), VerifyStr)View on GitHub (pinned to 3136500ef3)
Solutions
- Read the appended rule text (e.g. ",max=10") to see the exact bound violated, then adjust the submitted value to satisfy it.
- Enforce the same limits on the frontend (maxlength, min/max attributes) so users never hit the server error.
- Update the `mj:` tag bounds if business requirements legitimately changed.
- If you need a different semantic (bytes vs runes, numeric vs length), adjust compareVerify or the field type rather than weakening the rule.
Example fix
// before NickName string `json:"nickName" mj:"max=5"` // value "this-is-way-too-long" // after NickName string `json:"nickName" mj:"max=30"` // frontend maxlength="30" and value trimmed to fit
Defensive patterns
Strategy: validation
Validate before calling
if utf8.RuneCountInString(req.NickName) > 30 {
return errors.New("nickName exceeds 30 characters")
} Try / catch
if err := utils.Verify(req, utils.Rules{}); err != nil {
if strings.Contains(err.Error(), "长度或值不在合法范围") {
c.JSON(400, gin.H{"msg": err.Error()})
return
}
return err
} Prevention
- Enforce the same min/max limits on the frontend inputs
- Keep `mj:max/min` bounds in sync with DB column sizes
- Remember string rules check length, numeric kinds check value
- Clamp page-size style inputs to allowed ranges before validating
When it happens
Trigger: A field tagged e.g. mj:"max=5" receives a string longer than 5 characters or a numeric value above the bound; mj:"min=1" receives 0 or an empty slice-dependent value; any compareMap key (max/min/eq) whose compareVerify check returns false.
Common situations: Users submit over-long inputs (comments, names) that the DB column would reject; pagination/page-size fields below minimum; version drift where the tag's bound no longer matches business limits; callers forgetting the rule compares string length for string kinds and numeric value for numeric kinds.
Related errors
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/7f2c561f8c4eef00.
Report an issue: GitHub.