egg-antd-form-validator
v0.0.7
Published
validate plugin for egg
Downloads
15
Maintainers
Readme
egg-antd-form-validator
Sharing validator scheme between frontend and backend with Ant Design style
Install
$ npm i egg-antd-form-validator --save
Usage
// {app_root}/config/plugin.js
exports['egg-antd-form-validator'] = {
package: 'egg-antd-form-validator',
};
Validate Request Body
More Scheme reference async-validator
// {app_root}/app/controller/home.js
const scheme = {
id: [{
type: 'string',
required: true,
}]
};
exports.index = function* () {
const error = this.validate(scheme)(this.request.body);
if (error) {
// [{ fields: 'id', message: 'why it fail' }]
// throw manually
}
};
typings
import { IErrorField, ISchemeOptions } from 'egg-antd-form-validator';
declare module 'chair' {
export interface Context {
validate: (scheme: any, options?: ISchemeOptions) => (values: any) => Promise<IErrorField[] | null>
}
}
use it standalone without egg.js
import { validate } from 'egg-antd-form-validator';
if (await validate(scheme)(values)) {
// throw
}