How Provide Dynamic Message In Custom Loopback Validation?
Here is from documentation: User.validate('name', customValidator, {message: 'Bad name'}); function customValidator(err) { if (this.name === 'bad') err(); }); var user = new Us
Solution 1:
Suddenly there is a addError
method with signature errors.add(attr, message, code)
, so:
User.validate('name', customValidator);
function customValidator(err) {
if (this.name === 'bad') {
this.errors.add('name', `Name is bad`, 'name.bad')
err();
}
if (this.name === 'very bad') {
this.errors.add('name', `Name is very bad`, 'name.very.bad')
err();
}
});
This works, but keep in mind that you will have +1custom
code and message, see error.details.codes.name
and error.details.messages.name
paths from rough json below:
{"error":{"statusCode":422,"name":"ValidationError","message":"The `Entity` instance is not valid. Details: `name` Name is very bad (value: very bad).","details":{"context":"Entity","codes":{"name":["name.very.bad","custom"],},"messages":{"name":["Name is very bad","is invalid"]}},"stack":"..."}}
Post a Comment for "How Provide Dynamic Message In Custom Loopback Validation?"