Solvedajv Custom Error Messages

The docs are a bit terse so I'm not entirely sure on this, but do you have the ability to define custom error messages?

33 Answers

βœ”οΈAccepted Answer

πŸ‘
This feature would be very interesting to me, too bad it isn't handled in ajv... As amcdnl said, a format like (jsen) would be cool :

var schema = {
    type: 'number',
    minimum: 18,
    messages: {
        type: 'Must be a number',
        minimum: 'No children...'
    }
}

also with the hability to provide a function :

messages: {
     length: function(val, obj) {
           return obj.otherProperty === true ? "You're wrong" : "You're not right";
     }
}

Other Answers:

1.0.0 published

I am thinking about implementing invalidMessage and requiredMessage as custom keywords and add them to ajv-keywords. This way those who need this functionality can use those keywords without polluting the namespace and increasing the size of ajv for everybody else.

These keywords will analyse errors collected so far for the same property/item and if it was invalid/required the error will be added. Then it's possible to filter those errors out (using keyword property of the error object).

What do you think?

Thank you. I can also see in revalidator test that it supports messages keyword to define keyword-specific messages.

So I am considering implementing custom keyword errorMessages that can be a string or an object.

If it's a string "error_message" and the validation fails on the current level it will add an error object:

{
  keyword: 'errorMessages',
  message: 'error_message',
  params: { keywords: ['...' /*, '...', ... */] } // failing keywords
  // other properties as usual
}

If it's an object, then it's keys are keywords and the values are strings with errors for each keyword (a bonus would be to allow templates referring to the keyword schema and data values). required and dependencies keywords are special, they should also allow property specific messages (as there is no such thing as "required": true in draft 4 and instead the array of properties should be used on the object level).

{
  "properties": {
    "size": {
      "type": "number",
      "minimum": 4,
      "multipleOf": 2,
      "errorMessages": {
        "type": "size should be a number",
        "minimum": "size should be bigger or equal to 4",
        "multipleOf": "size should be even"
      }
    }
  },
  "required": ["size"],
  "errorMessages": {
    "required": {
      "size": "size is mandatory"
    }
  }
}

In this case instead of adding additional error objects, the keyword would add the property errorMessage to them (it's better than replacing message property as it would still allow functions such as errorsText produce meaningful results and also i18n would not replace errors in those additional properties.

Or maybe it's better to always add additional objects - they can be easily filtered out. Need to think about it.

This custom keyword will be defined in ajv-keywords package so it would be quite easy to use.

Suggestions? @amcdnl @Xamage @joaoreynolds @claudio-viola @crkepler

PLEDGE: Contribute any amount to this project and I will match your contribution 100%, to reach the project goal of US$ 500.00.

There's still US$ 425.00 to go, so my needed match limit is US$ 212.50, which I'll happily contribute as others chip in. I prefer not to be the sole contributor and would like more people involved.

My pledge is valid until 4-July-2017, one year after the first contribution was made.

Please, contribute any amount you can:

https://www.bountysource.com/issues/30029477-custom-error-messages