Open atian25 opened 6 years ago
Parameter.addRule(type, check)
don't support check
to be arrow-function is you want to use this.t()
, so maybe it's better to pass translate
to rule, that is:
addRule('json', (rule, value, obj) => {
return rule.__('error message');
});
thinking about drop Parameter
dependency, copy it's logic inside
推荐一下 property-validator https://github.com/nettofarah/property-validator
Parameter seems hard to pass a context-level translate fn
@dead-horse any suggest ?
这个好了?急需多语言支持
In enterprise-level development, data validation is very necessary and we hope to do so as soon as possible.
@monkeym4ster new version of egg-validate
now support custom paramter
constructor
https://github.com/eggjs/egg-validate/blob/master/app.js#L27
@atian25 So how can I translate error messages using gettext or __ method?
// in config.[env].js
exports.validate = {
translate: error => {
// something here but I cannot use ctx.__
}
};
Or do I need to configure a brand new app.validator
on my app.js file?
@JoaoCnh you could config it by https://github.com/eggjs/egg-validate#configurations
cc @dead-horse
@atian25 I realised that and I know that Parameter as a translate method.
My problem is actually the translation. We should translate according to the locale from the request and in said method we have no access to context. So we can't even use gettext.
EDIT: It's hard to grasp because the messages already come translated from english to the translate method. So a functional translating method is not as straight forward as it should be.
I know yup as very good schema validating lib and it even lets you define labels for each property.
See [here] an example of yup for egg (https://github.com/seekcx/egg-yup)
Background
Currently, egg-validate don't support
i18n
:Proposal
i18n
egg-validate
will use egg-i18n asoptionalDependencies
translate
toctx.gettext
which provide byegg-i8n
rule file
app/validate/**/*.js
, and register toapp.validator.addRule(type, fn)