自定义错误信息

未匹配的标注

validate 方法接受自定义消息以及验证模式对象,你可以仅为验证规则定义消息,也可以为单个字段指定。

await request.validate({
  schema: schema.create({
    // ...
  }),
  messages: {
    required: 'The {{ field }} is required to create a new account',
    'username.unique': 'Username not available'
  }
})
  • required 规则的自定义消息将被所有未通过所需验证的字段使用。
  • username.unique 组合仅适用于 unique 验证规则的 username 字段。

可以使用点分隔符定义嵌套对象和数组的消息。

{
  messages: {
    'user.username.required': 'Missing value for username',
    'tags.*.number': 'Tags must be an array of numbers',
    'products.*.title.required': 'Each product must have a title'
  }  
}

动态占位符

你可以使用以下占位符来引用自定义消息中的运行时值。

{
  messages: {
    required: '{{ field }} is required to sign up',
    enum: 'The value of {{ field }} must be in {{ options.choices }}'
  }
}
占位符 描述
{{ field }} 正在验证的字段的名称,嵌套对象路径用点分隔符表示。例如:user.profile.username
{{ rule }} 验证规则的名称
{{ options }} 验证方法传递的选项。例如,enum 规则会传递一个 choices 数组,有些规则可能根本不传递任何选项

通配符回调

你还可以定义一个回调函数来在运行时构造消息,回调只能使用通配符 * 表达式定义。

回调将在以下示例中的所有字段中调用,除了 username 字段。 username 仅当它未通过 required 要求时可以产生回调。

{
  messages: {
    '*': (field, rule, arrayExpressionPointer, options) => {
      return `${rule} validation error on ${field}`
    },
    'username.required': 'Username is required to sign up',
  }
}

传递给消息字符串的选项

以下是不同验证方法传递给消息字符串的选项列表。

date

date 验证规则将传递 options.format

{
  'date.format': '{{ field }} must be formatted as {{ options.format }}',
}

distinct

distinct 验证规则将传递应用不同规则的 field 以及找到重复值的 index

{
  'products.distinct': 'The product at {{ options.index + 1 }} position has already been added earlier'
}

enum / enumSet

enumenumSet 验证规则将传递 options.choices 数组。

{
  'enum': 'The value must be one of {{ options.choices }}',
  'enumSet': 'The values must be one of {{ options.choices }}',
}

file

文件验证允许为子规则定义自定义消息,例如:

{
  'file.size': 'The file size must be under {{ options.size }}',
  'file.extname': 'The file must have one of {{ options.extnames }} extension names',
}

minLength / maxLength

minLengthmaxLength 验证规则会将以下选项传递给自定义消息。

{
  'minLength': 'The array must have minimum of {{ options.minLength }} items',
  'maxLength': 'The array can contain maximum of {{ options.maxLength }} items',
}

range

range 验证规则将 startstop 选项传递给自定义消息。

{
  'range': 'Candidate age must be between {{ options.start }} and {{ options.stop }} years',
}

requiredIfExists / requiredIfNotExists

requiredIfExistsrequiredIfNotExists 验证规则将 options.otherField 作为字符串传递。

{
  'requiredIfExists': '{{ options.otherField }} requires {{ field }}',
}

条件必要规则

以下 requiredIf* 规则将 options.otherFields 作为字符串数组传递。

  • requiredIfExistsAll
  • requiredIfExistsAny
  • requiredIfNotExistsAll
  • requiredIfNotExistsAny
{
  'requiredIfExistsAll': '{{ options.otherFields }} requires {{ field }}',
}

requiredWhen

requiredWhen 验证规则将通过以下选项:

  • options.otherField
  • options.operator
  • options.values
{
  'requiredWhen': '{{ field }} is required when {{ otherField }}{{ operator }}{{ values }}'
}

本文章首发在 LearnKu.com 网站上。

本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:https://learnku.com/docs/adonisjs/5.x/va...

译文地址:https://learnku.com/docs/adonisjs/5.x/va...

上一篇 下一篇
贡献者:1
讨论数量: 0
发起讨论 只看当前版本


暂无话题~