A parameter verify tools for Egg

>>>點擊獲取更多文章<<<html

最近接觸EGG框架,剛接觸,其中涉及到驗證參數的一些運用,網上找的egg-validate 都不是很好用,最後找到了parameter插件,挺好用,推薦給你們,直接上代碼。git

'use strict';
const Controller = require('egg').Controller;
const Parameter = require('parameter');
const Check = new Parameter();
class LogserviceController extends Controller {

   async get() {
        const ctx = this.ctx;
        const {request,validator,service,constant} = ctx;
        if(typeof(request.body.type) != 'undefined') request.body.type = Number(request.body.type);
        if(typeof(request.body.curpage) != 'undefined') request.body.curpage = Number(request.body.curpage);

        const rule = {
            'start_time': {type:'date',required: false,max:10,allowEmpty: true},
            'end_time':{type:'date',required: false,max:10,allowEmpty: true},
            'type':{type:'enum',required: true,values:[0,200,404,500]},
            'curpage' : {type:'number',required: true},
        };

        const errors  = Check.validate(rule,request.body);

        if(errors == undefined){
            

            //當errors等於undefined 的時候,表示參數驗證經過,這裏寫本身的業務邏輯

        }else
        {
            this.ctx.body = errors;
        }
    }
    
}
module.exports = LogserviceController;

經過 npm install parameter --save 命令來安裝,下面是更多的關於rule的規則。github

Rule

common rule

  • required - if required is set to false, this property can be empty. default to true.
  • type - The type of property, every type has it's own rule for the validate.

int

If type is int, there has tow addition rules:web

  • max - The maximum of the value, value must <= max.
  • min - The minimum of the value, value must >= min.

integer

Alias to int.npm

number

If type is number, there has tow addition rules:框架

  • max - The maximum of the value, value must <= max.
  • min - The minimum of the value, value must >= min.

date

The date type want to match YYYY-MM-DD type date string.async

dateTime

The dateTime type want to match YYYY-MM-DD HH:mm:ss type date string.ui

datetime

Alias to dateTime.this

id

The id type want to match /^\d+$/ type date string.url

boolean

Match boolean type value.

bool

Alias to boolean

string

If type is string, there has four addition rules:

  • allowEmpty(alias to empty) - allow empty string, default to false.
  • format - A RegExp to check string's format.
  • max - The maximum length of the string.
  • min - The minimum length of the string.

email

The email type want to match RFC 5322 email address.

  • allowEmpty - allow empty string, default is false.

password

The password type want to match /^$/ type string.

  • compare - Compare field to check equal, default null, not check.
  • max - The maximum length of the password.
  • min - The minimum length of the password, default is 6.

url

The url type want to match web url.

enum

If type is enum, it requires an addition rule:

  • values - An array of data, value must be one on them. this rule is required.

object

If type is object, there has one addition rule:

  • rule - An object that validate the properties ot the object.

array

If type is array, there has four addition rule:

  • itemType - The type of every item in this array.
  • rule - An object that validate the items of the array. Only work with itemType.
  • max - The maximun length of the array.
  • min - The minimun lenght of the array.

abbr

  • 'int' => {type: 'int', required: true}
  • 'integer' => {type: 'integer', required: true}
  • 'number' => {type: 'number', required: true}
  • 'date' => {type: 'date', required: true}
  • 'dateTime' => {type: 'dateTime', required: true}
  • 'id' => {type: 'id', required: true}
  • 'boolean' => {type: 'boolean', required: true}
  • 'bool' => {type: 'bool', required: true}
  • 'string' => {type: 'string', required: true, allowEmpty: false}
  • 'email' => {type: 'email', required: true, allowEmpty: false, format: EMAIL_RE}
  • 'password' => {type: 'password', required: true, allowEmpty: false, format: PASSWORD_RE, min: 6}
  • 'object' => {type: 'object', required: true}
  • 'array' => {type: 'array', required: true}
  • [1, 2] => {type: 'enum', values: [1, 2]}
  • /\d+/ => {type: 'string', required: true, allowEmpty: false, format: /\d+/}
相關文章
相關標籤/搜索