egg-async-validator
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

egg-async-validator

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

Async validate plugin for egg, sharing validator scheme between frontend and backend with Ant Design style

See async-validator for more information such as custom rule.

Install

$ npm i egg-async-validator --save

Enable plugin

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

Validate rules

validate based on scheme, which define the shape of form fields, as simple as following

const productScheme = {
  id: [{
    type: 'string',
    required: true,
  }]
};

Validate Request Body

// app/controller/home.js
exports.index = async () => {
  const error = await this.validate(productScheme)(this.request.body);
  if (error) {
    // throw manually
  }
};

Tips

The package is so simple that it's easy to use as a npm module

import { validate } from 'egg-async-validator';
 
const errors = await validate(productScheme)(values);
if (errors) {
  // erros maybe [{ fields: 'id', message: 'why it fail' }] or null
  // throw
}

Questions & Suggestions

Please open an issue here.

License

MIT

Package Sidebar

Install

npm i egg-async-validator

Weekly Downloads

2

Version

0.0.1

License

MIT

Last publish

Collaborators

  • jaredleechn