Can we customize generated error message?

Hi,
Is there any option to customize the generated message. Say, for example, the required filed error is showing as ‘is a required property’. But I want to show it as “Enter a valid name” for a specific property “Name”. I have added the below-mentioned options in the schema (tested with errorMessages too) but no luck.

“errorMessage”: {
“required”: {
“name”: “Enter a valid name”
}
}

[original thread by Nanda]

[Naushad Rahman]

One way to create custom validation using ajv

[Naushad Rahman]

way to create is

[Naushad Rahman]

const ajv = createAjv();
ajv.addKeyword(“isNotEmpty”, {
type: “string”,
validate: function validate(schema, data) {

if (typeof data === "string" && data.trim() !== "") {
  return true;
}
validate.errors = [
  {
    keyword: "isNotEmpty",
    message: "Cannot be empty",
    params: { keyword: "isNotEmpty" },
  },
];
return false;

},
errors: true,
});

You can also use this: GitHub - ajv-validator/ajv-errors: Custom error messages in JSON Schemas for Ajv validator

[Nanda]

Thanks, @eneufeld(eneufeld) @naushad-rahman(naushad-rahman) for the quick response. The ajv.addKeyword approach is working for me. Already I have tried the ajv-errors approach and it was not working for me.

Hi @nandhu(nandhu) , what exactly does not work with ajv-errors for you?