Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
atian25 opened this issue Nov 16, 2017 · 11 comments
Open

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

atian25 opened this issue Nov 16, 2017 · 11 comments
Assignees

Comments

@atian25
Copy link
Member

atian25 commented Nov 16, 2017

Background

Currently, egg-validate don't support i18n:

Proposal

i18n

  • egg-validate will use egg-i18n as optionalDependencies
  • change Parameter's translate to ctx.gettext which provide by egg-i8n

rule file

  • auto load app/validate/**/*.js, and register to app.validator.addRule(type, fn)
@atian25
Copy link
Member Author

atian25 commented Nov 17, 2017

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
Copy link
Member Author

atian25 commented Nov 17, 2017

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

@runfan
Copy link

runfan commented Dec 4, 2017

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

@atian25
Copy link
Member Author

atian25 commented Dec 13, 2017

@atian25
Copy link
Member Author

atian25 commented Dec 13, 2017

Parameter seems hard to pass a context-level translate fn

@dead-horse any suggest ?

@Ailein
Copy link

Ailein commented Dec 29, 2017

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

@monkeym4ster
Copy link

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

@atian25
Copy link
Member Author

atian25 commented Aug 30, 2018

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

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

@JoaoCnh
Copy link

JoaoCnh commented Nov 13, 2018

@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
Copy link
Member Author

atian25 commented Nov 14, 2018

@JoaoCnh
Copy link

JoaoCnh commented Nov 14, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants