Skip to content
导航栏

Yao 模型字段校验

有以下的校验方法列表: yao/yao-app-sources/gou/validation.go

go
	"typeof":      // 校验数值类型 string, integer, float, number, datetime, timestamp,bool
	"min":        // 最小值
	"max":        // 最大值
	"enum":       // 枚举型
	"pattern":    // 正则匹配
	"minLength":  // 最小长度
	"maxLength":  // 最大长度
	"email":      // 邮箱地址
	"mobile":     // 手机号

类型判断,参数是允许的数据类型:string, integer, float, number, datetime, timestamp

json
{
  "validations": [
    {
      "method": "typeof",
      "args": ["string"],
      "message": "::{{input}} Error, {{label}} should be string"
    }
  ]
}

枚举判断,参数是可能的枚举列表

json
{
  "validations": [
    {
      "method": "enum",
      "args": ["enabled", "disabled"],
      "message": "::{{input}} Error, {{label}} should be enabled/disabled"
    }
  ]
}

邮箱地址/电话号码,不需要参数

json
{
  "validations": [
    {
      "method": "email",
      "args": [],
      "message": "{{input}} should be email"
    },
    {
      "method": "mobile",
      "args": [],
      "message": "{{input}} should be mobile"
    }
  ]
}

最小值,最大值判断

json
{
  "validations": [
    {
      "method": "min",
      "args": [0],
      "message": "{{label}}应大于等于0"
    },
    {
      "method": "max",
      "args": [10],
      "message": "{{label}}应小于等于10"
    }
  ]
}

最小长度,最大长度判断

json
{
  "validations": [
    {
      "method": "minLength",
      "args": [6],
      "message": "{{label}} Error"
    },
    {
      "method": "maxLength",
      "args": [18],
      "message": "{{label}} Error"
    }
  ]
}

正则判断,参数是正则表达式,可以替代邮件地址,电话号码的校验

json
[
  {
    "method": "pattern",
    "args": ["[0-9]+"],
    "message": "{{label}}应该至少包含一个数字"
  },
  {
    "method": "pattern",
    "args": ["[A-Z]+"],
    "message": "{{label}}应该至少包含一个大写字母"
  },
  {
    "method": "pattern",
    "args": ["[a-z]+"],
    "message": "{{label}}应该至少包含一个小写字母"
  },
  {
    "method": "pattern",
    "args": ["[@#$&*]+"],
    "message": "{{label}}应该至少包含一个符号"
  },
  {
    "method": "pattern",
    "args": ["^(\\d{18})|(\\d{14}X)$"],
    "message": "{{label}}身份证格式错误"
  },
  {
    "method": "pattern",
    "args": ["^[0-9A-Za-z@#$&*]{8}$"],
    "message": " {{label}}应该由8位,大小写字母、数字和符号构成"
  },
  {
    "method": "pattern",
    "args": ["^[0-9A-Za-z@#$&*]{32}$"],
    "message": "{{label}}应该由32位,大小写字母、数字和符号构成"
  }
]

用户名检验

json
{
  "label": "姓名",
  "name": "name",
  "type": "string",
  "length": 80,
  "comment": "姓名",
  "index": true,
  "validations": [
    {
      "method": "typeof",
      "args": ["string"],
      "message": "{{input}}类型错误, {{label}}应该为字符串"
    },
    {
      "method": "minLength",
      "args": [2],
      "message": "{{label}}至少需要2个字"
    },
    {
      "method": "maxLength",
      "args": [40],
      "message": "{{label}}不能超过20个字"
    }
  ]
}

密码检验

json
{
  "label": "登录密码",
  "name": "password",
  "type": "string",
  "length": 256,
  "comment": "登录密码",
  "crypt": "PASSWORD",
  "index": true,
  "validations": [
    {
      "method": "typeof",
      "args": ["string"],
      "message": "{{input}}类型错误, {{label}}应该为字符串"
    },
    {
      "method": "minLength",
      "args": [6],
      "message": "{{label}}应该由6-18位,大小写字母、数字和符号构成"
    },
    {
      "method": "maxLength",
      "args": [18],
      "message": "{{label}}应该由6-18位,大小写字母、数字和符号构成"
    },
    {
      "method": "pattern",
      "args": ["[0-9]+"],
      "message": "{{label}}应该至少包含一个数字"
    },
    {
      "method": "pattern",
      "args": ["[A-Z]+"],
      "message": "{{label}}应该至少包含一个大写字母"
    },
    {
      "method": "pattern",
      "args": ["[a-z]+"],
      "message": "{{label}}应该至少包含一个小写字母"
    },
    {
      "method": "pattern",
      "args": ["[@#$&*]+"],
      "message": "{{label}}应该至少包含一个符号"
    }
  ]
}