Code coverage report for thinkjs/lib/Lib/Util/Valid.js

Statements: 100% (81 / 81)      Branches: 100% (46 / 46)      Functions: 100% (22 / 22)      Lines: 100% (81 / 81)      Ignored: none     

All files » thinkjs/lib/Lib/Util/ » Valid.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277        1   1                 7 7 7 2   5 1   4               13                 17               4 4               2 2               3 3               3 1   2 1       1 17   1 1   1               2 2               2 2                 2               2 2                 6 6 1   5               2                   4 4 4 1   3 1   2               2               1               1               2 2                   1                                                     1   58 1   57 49   57 57 62 62 54   62 64   64 4 4 3 3   60 1   59 59 3   59 59 59 33 33 33     63     56  
/**
 * Valid
 * @return {[type]} [description]
 */
var net = require('net');
 
var Valid = {
  /**
   * 长度区域
   * @param  {[type]} min [description]
   * @param  {[type]} max [description]
   * @return {[type]}     [description]
   */
  length: function(value, min, max){
    'use strict';
    min = min | 0;
    var length = ((value || '') + '').length;
    if (length < min) {
      return false;
    }
    if (max && length > max) {
      return false;
    }
    return true;
  },
  /**
   * 必填
   * @return {[type]} [description]
   */
  required: function(value){
    'use strict';
    return ((value || '') + '').length > 0;
  },
  /**
   * 自定义正则校验
   * @param  {[type]} reg [description]
   * @return {[type]}     [description]
   */
  regexp: function(value, reg){
    'use strict';
    return reg.test(value);
  },
  /**
   * 邮箱
   * @return {[type]} [description]
   */
  email: function(value){
    'use strict';
    var reg = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
    return this.regexp(value, reg);
  },
  /**
   * 时间戳
   * @return {[type]} [description]
   */
  time: function(value){
    'use strict';
    var reg = /^[1-5]\d{12}$/;
    return this.regexp(value, reg);
  },
  /**
   * 中文名
   * @return {[type]} [description]
   */
  cnname: function(value){
    'use strict';
    var reg = /^[\u4e00-\u9fa5\u3002\u2022]{2,32}$/;
    return this.regexp(value, reg);
  },
  /**
   * 身份证号码
   * @return {[type]} [description]
   */
  idnumber: function(value){
    'use strict';
    if (/^\d{15}$/.test(value)) {
      return true;
    }
    if ((/^\d{17}[0-9xX]$/).test(value)) {
      var vs = '1,0,x,9,8,7,6,5,4,3,2'.split(','),
        ps = '7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2'.split(','),
        ss = value.toLowerCase().split(''),
        r = 0;
      for (var i = 0; i < 17; i++) {
        r += ps[i] * ss[i];
      }
      var isOk = (vs[r % 11] === ss[17]);
      return isOk;
    }
    return false;
  },
  /**
   * 手机号
   * @return {[type]} [description]
   */
  mobile: function(value){
    'use strict';
    var reg = /^(13|15|18|14|17)\d{9}$/;
    return this.regexp(value, reg);
  },
  /**
   * 邮编
   * @return {[type]} [description]
   */
  zipcode: function(value){
    'use strict';
    var reg = /^\d{6}$/;
    return this.regexp(value, reg);
  },
  /**
   * 2次值是否一致
   * @param  {[type]} field [description]
   * @return {[type]}       [description]
   */
  confirm: function(value, cvalue){
    'use strict';
    return value === cvalue;
  },
  /**
   * url
   * @return {[type]} [description]
   */
  url: function(value){
    'use strict';
    var reg = /^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/;
    return this.regexp(value, reg);
  },
  /**
   * 整数
   * @param  {[type]} o [description]
   * @return {[type]}   [description]
   */
  int: function(value){
    'use strict';
    var val = parseInt(value, 10);
    if (isNaN(val)) {
      return false;
    }
    return (val + '').length === value.length;
  },
  /**
   * 浮点数
   * @return {[type]} [description]
   */
  float: function(value){
    'use strict';
    return isNumberString(value);
  },
  /**
   * 整数范围
   * @param  {[type]} min [description]
   * @param  {[type]} max [description]
   * @return {[type]}     [description]
   */
  range: function(value, min, max){
    'use strict';
    value = parseInt(value, 10);
    min = min | 0;
    if (isNaN(value) || value < min) {
      return false;
    }
    if (max && value > max) {
      return false;
    }
    return true;
  },
  /**
   * ip4校验
   * @return {[type]} [description]
   */
  ip4: function(value){
    'use strict';
    return net.isIPv4(value);
  },
  /**
   * ip6校验
   * @return {[type]} [description]
   */
  ip6: function(value){
    'use strict';
    return net.isIPv6(value);
  },
  /**
   * ip校验
   * @return {[type]} [description]
   */
  ip: function(value){
    'use strict';
    return net.isIP(value);
  },
  /**
   * 日期校验
   * @return {[type]} [description]
   */
  date: function(value){
    'use strict';
    var reg = /^\d{4}-\d{1,2}-\d{1,2}$/;
    return this.regexp(value, reg);
  },
  /**
   * 在一个范围内
   * @param  {[type]} value [description]
   * @param  {[type]} arr   [description]
   * @return {[type]}       [description]
   */
  in: function(value, arr){
    'use strict';
    return arr.indexOf(value) > -1;
  }
};
/**
 * data格式
 * [{
 *     value: xxx,
 *     name: '',
 *     valid: ['required', 'range'],
 *     range_args: [],
 *     msg:{
 *         required: '',
 *         range: ''
 *     }
 * },{
 *     value: xxx,
 *     name: '',
 *     valid: ['required', 'range'],
 *     range_args: [],
 *     msg:{
 *         required: '',
 *         range: ''
 *     }
 * }]
 * @param  {[type]} data [description]
 * @return {[type]}      [description]
 */
module.exports = function(data){
  'use strict';
  if (!data) {
    return true;
  }
  if (!isArray(data)) {
    data = [data];
  }
  var result = {};
  data.forEach(function(item){
    var valid = item.valid;
    if (!isArray(valid)) {
      valid = [valid];
    }
    valid.some(function(validItem){
      var flag = true;
      //自定义检测方法
      if (isFunction(validItem)) {
        flag = validItem(item.value, item);
        if (isString(flag)) {
          result[item.name] = flag;
          flag = false;
        }
      }else if(!isFunction(Valid[validItem])){
        throw new Error(validItem + ' is not valid');
      }else{
        var args = item[validItem + '_args'] || [];
        if (!isArray(args)) {
          args = [args];
        }
        args = [item.value].concat(args);
        flag = Valid[validItem].apply(Valid, args);
        if (flag === false) {
          var msg = (isObject(item.msg) ? item.msg[validItem] : item.msg) || '';
          msg = msg.replace('{name}', item.name).replace('{value}', item.value);
          result[item.name] = msg;
        }
      }
      return !flag;
    });
  });
  return result;
};