Comments (3)
For anyone else finding this in the future here's an up-to-date workaround to this problem.
I'm using ajv
as part of a jsonforms.io form, so I don't have easy access to the error objects. Alternatively I would have had to add localize calls to each individual form cell renderer, which isn't very clean.
Here I overwrite the ajv.compile
function with a wrapper into which I can inject the localization call.
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)
from ajv-i18n.
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.
from ajv-i18n.
Actually I just developed it in my fork: ggondim@aaf1458
Example of a localize file (en/index.js) built with npm run build
:
'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;
from ajv-i18n.
Related Issues (20)
- Minor release for translation fix HOT 1
- An in-range update of uglify-js is breaking the build 🚨 HOT 21
- An in-range update of coveralls is breaking the build 🚨 HOT 9
- An in-range update of mocha is breaking the build 🚨 HOT 4
- An in-range update of karma is breaking the build 🚨 HOT 1
- An in-range update of eslint is breaking the build 🚨 HOT 5
- An in-range update of dot is breaking the build 🚨 HOT 1
- An in-range update of js-beautify is breaking the build 🚨 HOT 2
- An in-range update of ajv is breaking the build 🚨 HOT 3
- How to properly remove "data" prefix ? HOT 1
- Merge Catalan translation pull request
- how to throw error if my property value is "NA" or "undefined"? HOT 1
- peerDependencies not support ajv v7
- Messages for JSON Schema draft-2019-09 and JSON Type Definition HOT 5
- How to customize error information HOT 3
- New release with fixed types HOT 1
- Custom keyword error message
- ajv-i18n no work in IE11?
- Introduction of the "_defaultMessage" overrides messages defined in schema HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ajv-i18n.