3

I am using AJV for custom validation. 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. I am excepting - My Error message should be -

Image extension should match with jpeg, JPEG, jpg, JPG format

Also tried with message attributes. Same issue. Am I doing something wrong, Or do I need to import some scripts ?

Atique Ahmed
  • 308
  • 4
  • 16

1 Answers1

4

i think you should use ajv-errors package to make custom error messages

Ali Hosseini
  • 879
  • 6
  • 13
  • I can't do npm install ajv-errors . Is that any other way to add ajv-errors inside – Atique Ahmed Feb 10 '20 at 08:49
  • i don't know if it works or not but use this link and check if it works: [ https://cdn.jsdelivr.net/npm/ajv-errors@1.0.1/index.min.js ] if not you can download package from github page and use it in your project staticly hope it woks! – Ali Hosseini Feb 10 '20 at 09:19
  • No bro. Not working. Thanks you, for your quick response. – Atique Ahmed Feb 10 '20 at 10:21
  • This may help https://github.com/rofrol/ajv-regexp-errormessage-example – rofrol Jan 11 '21 at 20:30