简体   繁体   English

AJV - 如何在 Javascrip 中添加自定义错误消息

[英]AJV - How to add custom Error messages in Javascrip

I am using AJV for custom validation.我正在使用 AJV 进行自定义验证。 I want to change the error message as well while validate, I am using -我也想在验证时更改错误消息,我正在使用 -

<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
};

I am getting each and every time default error message provided by AJV.我每次都收到 AJV 提供的默认错误消息。 I am excepting - My Error message should be -我除外 - 我的错误信息应该是 -

Image extension should match with jpeg, JPEG, jpg, JPG format图片扩展名应与 jpeg、JPEG、jpg、JPG 格式匹配

Also tried with message attributes.还尝试了消息属性。 Same issue.同样的问题。 Am I doing something wrong, Or do I need to import some scripts ?我做错了什么,还是我需要导入一些脚本?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM