base-validator
Advanced tools
Comparing version 0.0.3 to 0.0.4
// 定义在全局,避免每次校验都花时间初始化正则。 | ||
const PHONE = /^(?:13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/ | ||
const EMAIL = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/ | ||
const URL = /^((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-.,@?^=%&:\/~+#]*[\w\-@?^=%&\/~+#])?$/ | ||
const IDCARD = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)/ | ||
const PHONE = /^\s*(?:13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}\s*$/ | ||
const EMAIL = /^\s*(?:[A-Za-z0-9_\-\.])+\@(?:[A-Za-z0-9_\-\.])+\.(?:[A-Za-z]{2,4})\s*$/ | ||
const URL = /^\s*(?:(?:ht|f)tps?):\/\/[\w\-]+(?:\.[\w\-]+)+(?:[\w\-.,@?^=%&:\/~+#]*[\w\-@?^=%&\/~+#])?\s*$/ | ||
const IDCARD = /(^\s*[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]\s*$)|(^\s*[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}\s*$)/ | ||
const NUMBER = /^\s*[+\-]?(?:[1-9]\d*|0)(?:[\.]\d*[1-9])?\s*$/ | ||
@@ -70,4 +70,9 @@ const DIGITS = /^\s*[+\-]?(?:[1-9]\d*|0)\s*$/ | ||
} | ||
// 对于数组,元素的个数应大于等于期望值;对于字符串,则是字符的个数。 | ||
return (Array.isArray(actual) || typeof actual === 'string') && this.min(actual.length, expected) | ||
// 对于数组,元素的个数应大于等于期望值; | ||
if (Array.isArray(actual)) { | ||
return this.min(actual.length, expected) | ||
// 对于字符串,则是字符的个数。不包含前后的空白字符 | ||
} else if (typeof actual === 'string') { | ||
return this.min(actual.trim().length, expected) | ||
} | ||
}, | ||
@@ -83,4 +88,9 @@ | ||
} | ||
// 对于数组,元素的个数应小于等于期望值;对于字符串,则是字符的个数。 | ||
return (Array.isArray(actual) || typeof actual === 'string') && this.max(actual.length, expected) | ||
// 对于数组,元素的个数应小于等于期望值; | ||
if (Array.isArray(actual)) { | ||
return this.max(actual.length, expected) | ||
// 对于字符串,则是字符的个数。不包含前后的空白字符 | ||
} else if (typeof actual === 'string') { | ||
return this.max(actual.trim().length, expected) | ||
} | ||
}, | ||
@@ -173,4 +183,4 @@ | ||
// 包含期望值。对于数组,对比的是单项;对于字符串,对比的是字串。 | ||
contains: function (actual, expected) { | ||
return (Array.isArray(actual) || typeof actual === 'string') && actual.contains(expected) | ||
includes: function (actual, expected) { | ||
return (Array.isArray(actual) || typeof actual === 'string') && actual.includes(expected) | ||
}, | ||
@@ -183,3 +193,7 @@ | ||
} | ||
return expected.contains(actual) | ||
// 对于用户的数据,忽略前后空白字符 | ||
if (typeof actual === 'string') { | ||
actual = actual.trim() | ||
} | ||
return expected.includes(actual) | ||
} | ||
@@ -186,0 +200,0 @@ } |
{ | ||
"name": "base-validator", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "基础的表单验证器", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -69,3 +69,3 @@ # base-validator | ||
| equalTo | 值相等。对于复合类型(数组和对象),进行递归对比。对于基本类型,进行全等对比(即对比类型和值)。别名:**eq** | | ||
| contains | 包含期望值。对于数组,对比的是单项;对于字符串,对比的是字串。 | | ||
| includes | 包含期望值。对于数组,对比的是单项;对于字符串,对比的是字串。 | | ||
| in | 在期望值中存在。对于数组,对比的是单项;对于字符串,对比的是字串。 | | ||
@@ -72,0 +72,0 @@ | regular | 正则校验。期望值是 **RegExp** 类型或 **string** 类型(正则字面量格式) | |
16053
240