egg-yup
Base on yup
Install
$ npm i egg-yup --save
or
$ yarn add egg-yup
Usage
// {app_root}/config/plugin.jsexportsyup = enable: true package: 'egg-yup';
Configuration
// {app_root}/config/config.default.jsexportsyup = locale: 'zh-CN' // default null locales: // default {} 'zh-CN': number: min: '${path} 不能小于 {$min}' options: // ref: https://github.com/jquense/yup#mixedvalidatevalue-any-options-object-promiseany-validationerror strict: false; abortEarly: true; stripUnknown: false; recursive: true; context: null; { // default null ctx; };
see config/config.default.js for more detail.
Example
// app/controller/user.js#register const ctx app: yup = this; await ctx;
Api reference
app.yup
Original yup
app.setYupLocale(locale: string | object)
Language setting
// set by stringapp; // set by objectapp;
ctx.validate(rules: object[, values: object = null [, options: object = null]])
Async validate. In order to allow asynchronous custom validations all (or no) tests are run asynchronously. A consequence of this is that test execution order cannot be guaranteed. ref: yup-doc
rules
: validate rulesvalues
: validate data, defaultctx.request.body
options
: this will override theoptions
in the config.
ctx.validateSync(rules: object[, values: object = null [, options: object = null]])
Sync validate
rules
: validate rulesvalues
: validate data, defaultctx.request.body
options
: this will override theoptions
in the config.
Questions & Suggestions
Please open an issue here.