【问题标题】:AJV - How to add custom Error messages in JavascripAJV - 如何在 Javascript 中添加自定义错误消息
【发布时间】:2020-02-10 08:27:49
【问题描述】:

我正在使用 AJV 进行自定义验证。我也想在验证时更改错误消息, 我正在使用 -

<script src="https://cdnjs.cloudflare.com/ajax/libs/ajv/6.11.0/ajv.min.js"></script>

var imageValidation = {
    required: ['name', 'url', 'source','tags'],
    properties: {
      name: {type: 'string'},
      url: {
        type: 'string', 
        pattern: "(^s3\:\/\/.+(jpeg|JPEG|jpg|JPG)$)",
        errorMessage: {
            type:'must be a string',
            pattern: "Image extension should match with jpeg, JPEG, jpg, JPG format"
        }
        // messages: {
        //     pattern: 'Image extention should match with jpeg, JPEG, jpg, JPG format'
        // }
        // message: "Image extention should match with jpeg, JPEG, jpg, JPG format"
      },
      source: {type: 'string'},
      tags: {type: 'array'}
    }
    allRequired: true
};

我每次都会收到 AJV 提供的默认错误消息。我例外 - 我的错误消息应该是 -

图片扩展名应与jpeg、JPEG、jpg、JPG格式匹配

还尝试了消息属性。同样的问题。是我做错了什么,还是需要导入一些脚本?

【问题讨论】:

    标签: javascript ajv


    【解决方案1】:

    我认为您应该使用ajv-errors 包来制作自定义错误消息

    【讨论】:

    猜你喜欢
    • 2018-08-19
    • 2021-07-04
    • 2018-05-17
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    相关资源
    最近更新 更多