egg-async-validator
v0.0.1
Published
async validate plugin base on async-validator
Downloads
8
Maintainers
Readme
egg-async-validator
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.