Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduction of the "_defaultMessage" overrides messages defined in schema #232

Open
karlguillotte opened this issue Dec 5, 2021 · 3 comments

Comments

@karlguillotte
Copy link

karlguillotte commented Dec 5, 2021

Hi!

Thanks for this great library!

I am in the process of upgrading to v8 as well as errors, i18n and formats plugins. I am noticing that one thing that works differently is the uses of default messages in i18n.

const schema = {/* ... */} // valid draft-07 schema
const data = {/* ... */} // valid data object, but not validating against the schema

const ajv = new Ajv({  allErrors: true })
const validate = ajv.compile(schema)

if (!validate(data)) {
    localize.en(validate.errors)
    console.log(validate.errors)
}

The console outputs lots of "must pass "errorMessage" keyword validation", even though I have defined "errorMessage" in the schema. It used to work in v7 with plugins.

https://github.com/ajv-validator/ajv-i18n/blob/master/messages/index.js#L80 generate a default case to the switch statements that override every single message.

The code definitely works, it is not a bug. However, the behaviour is totally different from the previous version, and wondering if it is the desired behaviour. If it is, wondering how to provide error messages from schema (using "errorMessage") as well as from the i18n plugin. Messages within "errorMessage" in my schema are translated.

EDIT
I am noticing that #220 is probably a similar issue.

@karlguillotte karlguillotte changed the title Introduction of the "_defaultMessage" overrides messages defined in the schema Introduction of the "_defaultMessage" overrides messages defined in schema Dec 6, 2021
@Megumin2k17
Copy link

So any solution on this so far? Why should this thing replace custom messages with translated default ones? =/

@maspendig
Copy link

maspendig commented May 8, 2023

So any solution on this so far? Why should this thing replace custom messages with translated default ones? =/

I avoid this with the following workaround by filtering errors for specific keywords (e.g. "errorMessage") and only localize the others.

For example:

if (!validate(data)) {
    localize.en(validate.errors.filter((err) => err.keyword !== 'errorMessage'))
    console.log(validate.errors)
}

@jingyuLin1999
Copy link

jingyuLin1999 commented Aug 17, 2023

I get the same issue. How to solve it?

So any solution on this so far? Why should this thing replace custom messages with translated default ones? =/

I avoid this with the following workaround by filtering errors for specific keywords (e.g. "errorMessage") and only localize the others.

For example:

if (!validate(data)) {
    localize.en(validate.errors.filter((err) => err.keyword !== 'errorMessage'))
    console.log(validate.errors)
}

it work, thanks this reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants