Skip to content

Commit

Permalink
i18n plugin proposal ajv-validator#153
Browse files Browse the repository at this point in the history
  • Loading branch information
ggondim committed Jul 5, 2020
1 parent eddd238 commit aaf1458
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion localize/localize.jst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
}}

{{ var name = 'localize_' + it.locale.replace('-','_'); }}
module.exports = function {{=name}}(errors) {
function {{=name}}(errors) {
if (!(errors && errors.length)) return;
for (var i=0; i<errors.length; i++) {
var e = errors[i];
Expand All @@ -24,6 +24,31 @@ module.exports = function {{=name}}(errors) {
}
};

function makeLocalizedErrors(ajv) {
return {
get: function() {
return ajv._errors;
},
set: function(errors) {
ajv._errors = {{=name}}(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 = {{=name}};
module.exports = initialize;

{{
function stripFunction(code) {
return code.toString()
Expand Down

0 comments on commit aaf1458

Please sign in to comment.