README
egg-valid
A better Validation egg plugin based on @killara/validation
Install
$ npm i egg-valid -S
Usage
// config
exports.valid = {
enable: true,
package: 'egg-valid',
};
// controller
class HomeController extends Controller {
async index() {
const { app, ctx } = this;
const rule = { username: 'required|alpha' };
const errors = await app.validator.validate(ctx.request.body, rule)
// ...
}
}
Rule
Derived from @killara/validation
accepted
- string style:
field: 'accepted' - object style:
field: { accepted: true }
- string style:
alpha
- string style:
field: 'alpha:6'orfield: 'alpha:len=6' - object style:
field: { alpha: { len: 6 } }
- string style:
confirmed
Rule
confirmed: thefieldneed to have the same value as the value that be filled byfield_confirmation. We can changefield_confirmationto any names withconfirmed:"custom"- string style:
field: 'confirmed'orfield: 'confirmed:"custom_field_name"' - object style:
field: { confirmed: "custom_field_name" }
- string style:
date
- string style:
field: 'date' - object style:
field: { date: true }
- string style:
datetime
- string style:
field: 'datetime' - object style:
field: { datetime: true }
- string style:
time
- string style:
field: 'time' - object style:
field: { time: true }
- string style:
email
- string style:
field: 'email:true' - object style:
field: { email: true }
- string style:
in
arraystyle:field: [ 'basketball', 'football' ]- object style:
field: { in: [ 'basketball', 'football' ] }
money
- string style:
field: 'money'orfield: 'money:0'field: 'money:2'(default) - object style:
field: { money: { decimal: true } }orfield: { money: { decimal: 0 } }orfield: { money: { decimal: 2 } }
- string style:
numeric
- string style:
field: 'numeric:6'orfield: 'numeric:len=6' - object style:
field: { numeric: { len: 6 } }
- string style:
regexp
- string style:
field: 'regexp:"^123456
- string style: