ajv-validator / ajv-validator/ajv-i18n

Make i18n a real AJV plugin

オープン
#153 コメント 3 件 リアクション 1 件 担当者 0 名 GitHub で見る
主要言語
JavaScript
スター
180
フォーク
91
PR マージ指標
30日以内にマージされた PR はありません

説明

# 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 call `errorsText()`.

I propose expose a initialization function in **ajv-i18n**, just like other plugins, so this function could replace the `errorsText()` by its localized method.

```javascript
// @param {Ajv} ajv An Ajv instance
function initialize(ajv) {

ajv._errorsText = ajv.errorsText;

// intercepts errorsText function
ajv.errorsText = (errors) => {
localize(errors);
return ajv._errorsText(errors);
}

}
```

So, this initialization could be used like other plugins ([ajv-keywords example](https://github.com/ajv-validator/ajv-keywords#usage)):

```javascript
require('ajv-i18n')(ajv);
```

#### Here is a **[working Run-Kit](https://npm.runkit.com/ajv-i18n)** running this example.

## errors

If the `localize` method already converts all errors messages in `validate.errors` , the plugin could also replace errors by a getter/setter that does this conversion:

```javascript
function initialize(ajv, locale) {

ajv._compile = ajv.compile;

// intercepts compile function
ajv.compile = schema => {
const validate = ajv._compile(schema);
Object.defineProperty(validate, 'errors', makeLocalizedErrors(ajv));
return validate;
}

}

function makeLocalizedErrors(ajv) {
return {
get: () => {
return ajv._errors;
},
set: (errors) => {
ajv._errors = localize(errors);
},
configurable: true
};
}
```

# Considerations about the existent implementation

## What would happen to the current `localize` function in [`module.exports`](https://github.com/ajv-validator/ajv-i18n/blob/master/localize/localize.jst#L10)?

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](https://github.com/ajv-validator/ajv-i18n/blob/master/localize/localize.jst) could be exported as a property of the initialization function, just like [ajv-keywords does with the `get()` function](https://github.com/ajv-validator/ajv-keywords/blob/master/index.js#L29).

```javascript
module.exports = initialize;

function initialize(ajv) {
// ... initialize function as defined above
}

initialize.localize = localize;

function localize(errors) {
// current exported function in module.exports
}
```

## 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:

```javascript
module.exports = localize;

function localize(errors) {
// current exported function in module.exports
}

function initialize(ajv) {
// ... initialize function as defined above
}

localize.localize = initialize;

```

... used as:

```javascript
const { localize } = require('ajv-i18n');
localize(ajv);
```

# 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 🤓

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。