eggjs / egg

🥚 Born to build better enterprise frameworks and apps with Node.js & Koa
https://eggjs.org
MIT License
18.9k stars 1.82k forks source link

[RFC] egg-validate support i18n and auto load rule file #1672

Open atian25 opened 6 years ago

atian25 commented 6 years ago

Background

Currently, egg-validate don't support i18n:

Proposal

i18n

rule file

atian25 commented 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');
});
atian25 commented 6 years ago

thinking about drop Parameter dependency, copy it's logic inside

runfan commented 6 years ago

推荐一下 property-validator https://github.com/nettofarah/property-validator

atian25 commented 6 years ago

https://github.com/ianstormtaylor/superstruct

atian25 commented 6 years ago

Parameter seems hard to pass a context-level translate fn

@dead-horse any suggest ?

Ailein commented 6 years ago

这个好了?急需多语言支持

monkeym4ster commented 6 years ago

In enterprise-level development, data validation is very necessary and we hope to do so as soon as possible.

atian25 commented 6 years ago

@monkeym4ster new version of egg-validate now support custom paramter constructor

https://github.com/eggjs/egg-validate/blob/master/app.js#L27

JoaoCnh commented 5 years ago

@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?

atian25 commented 5 years ago

@JoaoCnh you could config it by https://github.com/eggjs/egg-validate#configurations

cc @dead-horse

JoaoCnh commented 5 years ago

@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)