如何为无效输入自定义错误消息?

时间:2016-10-24 16:07:08

标签: json jsonschema json-schema-validator

如何为无效输入自定义错误消息?

 {
   "$schema": "http://json-schema.org/draft-04/schema#",
   "type": "object",
   "properties": {
     "username": {
       "type": "string",
       "pattern": "^[A-Za-z0-9-_.]+$",
       "minLength": 3
     },
     "password": {
       "type": "string",
       "minLength": 8,
       "pattern": "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d\\W]$"
     }
   },
   "required": [
     "username",
     "password"
   ],
   "errors": [
     {
       "property": "username",
       "message": "min 3 characters, do not use spaces or special characters"
     }
   ]
 }

例如,如果用户名输入不是必需的最小长度或不满足正则表达式模式,则显示一个自定义消息min 3 characters, do not use spaces or special characters

1 个答案:

答案 0 :(得分:2)

不支持自定义错误消息。但是,有一些讨论会在下一版本的JSON Schema中添加这样的功能。