@ai-lion/egg-lion-validate
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

egg-lion-validate

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Validate plugin for egg.

See parameter for more information such as custom rule.

Install

$ npm i egg-lion-validate --save

Usage

// config/plugin.js
exports.validate = {
  enable: true,
  package: 'egg-lion-validate',
};

Configurations

egg-lion-validate support all parameter's configurations, check parameter documents to get more infomations.

// config/config.default.js
exports.validate = {
  // convert: false,
  // validateRoot: false,
};

Validate Request Body

// app/controller/home.js
const Controller = require('egg').Controller;
class HomeController extends Controller {
  async index() {
    const { ctx, app } = this;
    ctx.validate({ id: 'id' }); // will throw if invalid
    // or
    const errors = app.validator.validate({ id: 'id' }, ctx.request.body);
  }
}
module.exports = HomeController;

Extend Rules

  • app.js
app.validator.addRule('jsonString', (rule, value) => {
  try {
    JSON.parse(value);
  } catch (err) {
    return 'must be json string';
  }
});

Questions & Suggestions

Please open an issue here.

License

MIT

Package Sidebar

Install

npm i @ai-lion/egg-lion-validate

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

24.5 kB

Total Files

9

Last publish

Collaborators

  • ai-lion