Error message not working with Json schema pattern validation

Hi, I have a pattern validation, but tried to avoid the long message setting the errormessage property without success.

any idea?

  "ip": {
    "type": "string",
    "title": "Server IP or DNS",
    "pattern": "^((?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.[A-Za-z]{2,6})+|((25[0-5]|2[0-4][0-9]|1?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]?)))$",
    "errorMessage":  " Hostname or ip invalid"
  }
const i18n = {
    translateError: (error: any, translate: (id: string, defaultMessage: string, values?: any) => string) => {
       if (error.instancePath === '/ip' && error.keyword === 'pattern') {
         return 'Please enter a valid IP .';
       }
      return translate(error.message, error.message);
    }
  };

<JsonForms
    .....
     i18n={i18n}
/>

you can do like this

Thanks @vubh ,

I ajusted the function to handle that field if informed

Something like this

const i18n = {
  translateError: (error: any, translate: (id: string, defaultMessage: string, values?: any) => string) => {
    if (error.parentSchema?.errorMessage) {
      return error.parentSchema.errorMessage;
    }
    return translate(error.message, error.message);
  }
};
1 Like

Hi @gallarl4_roche, @vubh,

You normally don’t need to overwrite translateError as the also handed over translate is used within it anyway. See the i18n documentation for more information.

Yes, that is strange I tried all the combinations of message, but dont’ have luck with the pattern one…

I tried all this combinations

  "infinity-ip": {
    "type": "string",
    "title": "Server IP or DNS",
    "pattern": "^((?!-)[A-Za-z0-9-]{1,63}(?<!-)(\\.[A-Za-z]{2,6})+|((25[0-5]|2[0-4][0-9]|1?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|1?[0-9][0-9]?)))$",
    "error": {
      "custom": "Invalid IP or DNS",
      "defaultMessage": "Invalid IP or DNS",
      "pattern": "Invalid IP or DNS",
      "message": "Invalid IP or DNS"
    }
1 Like

Hi @gallarl4_roche,

There is a misunderstanding here. The documentation is describing how the keys of the translate function will look like when translating errors. There is no built-in mechanism to retrieve the properties from the JSON Schema.

Of course you CAN add the translations to the JSON Schema and then in your translate function you can inspect the schema and read the message from there if you like.

In any case, you will need to add a translate function to JSON Forms, as is described in the documentation.

1 Like

A ok, yes I missunderstand it

Thanks for the clarification.