-
-
Notifications
You must be signed in to change notification settings - Fork 90
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
Make i18n a real AJV plugin #153
Comments
Actually I just developed it in my fork: ggondim@aaf1458 Example of a localize file (en/index.js) built with 'use strict';
function localize_en(errors) {
// omitted for legibility
};
function makeLocalizedErrors(ajv) {
return {
get: function() {
return ajv._errors;
},
set: function(errors) {
ajv._errors = localize_en(errors);
},
configurable: true
};
}
function initialize(ajv) {
ajv._compile = ajv.compile;
ajv.compile = schema => {
const validate = ajv._compile(schema);
Object.defineProperty(validate, 'errors', makeLocalizedErrors(ajv));
return validate;
}
}
initialize.localize = localize_en;
module.exports = initialize; |
Thank you. It is probably better to do in ajv v7-beta that is just released - this implementation will have to be rewritten probably anyway. |
For anyone else finding this in the future here's an up-to-date workaround to this problem. I'm using Here I overwrite the const ajv = createAjv({messages: false})
const compileFunction = ajv.compile
ajv.compile = Object.assign((...compileProps: Parameters<typeof compileFunction>) => {
const validateFunction = compileFunction.apply(ajv, compileProps)
const newValidateFunction = Object.assign((...innerProps: Parameters<typeof validateFunction>) => {
const validationResult = validateFunction.apply(compileFunction, innerProps)
// @ts-expect-error `ErrorObject` is incorrect type in ajv-i18n: https://github.com/ajv-validator/ajv-i18n/pull/205
localize(validateFunction.errors)
Object.assign(newValidateFunction, validateFunction)
return validationResult
}, validateFunction)
return newValidateFunction
}, compileFunction) |
Implementation proposal
Considering ajv-i18n's localization function already mutates the
errors
object:errorsText()
When using ajv-i18 as a plugin, the
ajv.errorsText()
function should return the localized error messages.Currently we need to hack the
errors
object and then callerrorsText()
.I propose expose a initialization function in ajv-i18n, just like other plugins, so this function could replace the
errorsText()
by its localized method.So, this initialization could be used like other plugins (ajv-keywords example):
Here is a working Run-Kit running this example.
errors
If the
localize
method already converts all errors messages invalidate.errors
, the plugin could also replace errors by a getter/setter that does this conversion:Considerations about the existent implementation
What would happen to the current
localize
function inmodule.exports
?To work like other AJV's plugins, the current
module.exports
needs to be changed to the proposed initialization function, that would lead to a breaking change (major version).But the current localization function exported in localize.jst could be exported as a property of the initialization function, just like ajv-keywords does with the
get()
function.If you don't want to make a breaking change, at least approve the inverse
The inverse could be made, so this plugin injection could be:
... used as:
When can I make a pull request?
If this implementation design is approved, I start to code it and in a few hours I make a pull request.
Looking forward to a special reply from the most active member, @epoberezkin 🤓
The text was updated successfully, but these errors were encountered: