arktypea
arktype7mo ago
Emuzex

Custom error message with conditions and i18n

Hi i am new here, and i try to create translated error messages. i have a schema like this.

const Thing = type({
    price: type('string.numeric>0').configure({
    message: (ctx) => {
      return `Custom message`
    }
  }),
})

const out = Thing({
   "price": ""
})


The output : price ("") must be...
◦ a well-formed numeric string
◦ non-empty

But i want a bit different.

If value empty i want a "Required" message.
If value is non-empty and not a numeric string i want a "Must be a number" message.

The custom message from the configure only appears if the price is not empty.

Can you help me out please?

Used in Nuxt3 with Regle, the message translated with i18n inside the configure's message return. I couldn't find a better solution
Was this page helpful?