提交 418a68ed 编写于 作者: 雪洛's avatar 雪洛

fix: password strength default value

上级 ad33fc98
...@@ -33,10 +33,10 @@ const ERROR = { ...@@ -33,10 +33,10 @@ const ERROR = {
rePwdErr: '两次输入密码不一致' rePwdErr: '两次输入密码不一致'
}, },
passwordStrengthError: { passwordStrengthError: {
superstrong: '密码必须包含大小写字母、数字和特殊符号', super: '密码必须包含大小写字母、数字和特殊符号,密码长度必须在8-16位之间',
strong: '密码必须包含字母、数字和特殊符号', strong: '密码必须包含字母、数字和特殊符号,密码长度必须在8-16位之间',
medium: '密码必须为字母、数字和特殊符号任意两种的组合', medium: '密码必须为字母、数字和特殊符号任意两种的组合,密码长度必须在8-16位之间',
weak: '密码必须包含字母' weak: '密码必须包含字母,密码长度必须在6-16位之间'
}, },
passwordLengthError: { passwordLengthError: {
normal: '密码长度必须在' + minPasswordLength + '-' + maxPasswordLength + '位之间', normal: '密码长度必须在' + minPasswordLength + '-' + maxPasswordLength + '位之间',
......
...@@ -45,14 +45,14 @@ export default { ...@@ -45,14 +45,14 @@ export default {
}, },
/** /**
* 密码强度 * 密码强度
* superstrong(超强:密码必须包含大小写字母、数字和特殊符号) * super(超强:密码必须包含大小写字母、数字和特殊符号)
* strong(强: 密码必须包含字母、数字和特殊符号) * strong(强: 密码必须包含字母、数字和特殊符号)
* medium (中:密码必须为字母、数字和特殊符号任意两种的组合) * medium (中:密码必须为字母、数字和特殊符号任意两种的组合)
* weak(弱:密码必须包含字母) * weak(弱:密码必须包含字母)
* 为空或false则不验证密码强度 * 为空或false则不验证密码强度
*/ */
"password": { "password": {
"strength": "strong", "strength": "medium",
"length": [6, 20] //密码长度,默认在6-20位之间 "length": [6, 20] //密码长度,默认在6-20位之间
} }
} }
...@@ -29,6 +29,21 @@ baseValidator.username = function (username) { ...@@ -29,6 +29,21 @@ baseValidator.username = function (username) {
} }
} }
baseValidator.password = function (password) {
const errCode = ERROR.INVALID_PASSWORD
if (!isValidString(password)) {
return {
errCode
}
}
if (password.length < 6) {
// 密码长度不能小于6
return {
errCode
}
}
}
baseValidator.mobile = function (mobile) { baseValidator.mobile = function (mobile) {
const errCode = ERROR.INVALID_MOBILE const errCode = ERROR.INVALID_MOBILE
if (!isValidString(mobile)) { if (!isValidString(mobile)) {
...@@ -92,7 +107,7 @@ baseType.forEach((type) => { ...@@ -92,7 +107,7 @@ baseType.forEach((type) => {
} }
}) })
function tokenize (name) { function tokenize(name) {
let i = 0 let i = 0
const result = [] const result = []
let token = '' let token = ''
...@@ -122,7 +137,7 @@ function tokenize (name) { ...@@ -122,7 +137,7 @@ function tokenize (name) {
* 处理validator名 * 处理validator名
* @param {string} name * @param {string} name
*/ */
function parseValidatorName (name) { function parseValidatorName(name) {
const tokenList = tokenize(name) const tokenList = tokenize(name)
let i = 0 let i = 0
let currentToken = tokenList[i] let currentToken = tokenList[i]
...@@ -172,7 +187,7 @@ function parseValidatorName (name) { ...@@ -172,7 +187,7 @@ function parseValidatorName (name) {
return result return result
} }
function getRuleCategory (rule) { function getRuleCategory(rule) {
switch (rule.type) { switch (rule.type) {
case 'array': case 'array':
return 'array' return 'array'
...@@ -183,7 +198,7 @@ function getRuleCategory (rule) { ...@@ -183,7 +198,7 @@ function getRuleCategory (rule) {
} }
} }
function isMatchUnionType (val, rule) { function isMatchUnionType(val, rule) {
if (!rule.children || rule.children.length === 0) { if (!rule.children || rule.children.length === 0) {
return true return true
} }
...@@ -209,7 +224,7 @@ function isMatchUnionType (val, rule) { ...@@ -209,7 +224,7 @@ function isMatchUnionType (val, rule) {
return false return false
} }
function isMatchBaseType (val, rule) { function isMatchBaseType(val, rule) {
if (typeof baseValidator[rule.type] !== 'function') { if (typeof baseValidator[rule.type] !== 'function') {
throw new Error(`invalid schema type: ${rule.type}`) throw new Error(`invalid schema type: ${rule.type}`)
} }
...@@ -220,7 +235,7 @@ function isMatchBaseType (val, rule) { ...@@ -220,7 +235,7 @@ function isMatchBaseType (val, rule) {
return true return true
} }
function isMatchArrayType (arr, rule) { function isMatchArrayType(arr, rule) {
if (getType(arr) !== 'array') { if (getType(arr) !== 'array') {
return false return false
} }
...@@ -249,11 +264,12 @@ const passwordRules = { ...@@ -249,11 +264,12 @@ const passwordRules = {
// 密码必须为字母、数字和特殊符号任意两种的组合 // 密码必须为字母、数字和特殊符号任意两种的组合
medium: /^(?![0-9]+$)(?![a-zA-Z]+$)(?![~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]+$)[0-9a-zA-Z~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]{8,16}$/, medium: /^(?![0-9]+$)(?![a-zA-Z]+$)(?![~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]+$)[0-9a-zA-Z~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]{8,16}$/,
// 密码必须包含字母和数字 // 密码必须包含字母和数字
weak: /^(?=.*[0-9])(?=.*[a-zA-Z])[0-9a-zA-Z~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]{6,16}$/ weak: /^(?=.*[0-9])(?=.*[a-zA-Z])[0-9a-zA-Z~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]{6,16}$/,
} }
function createPasswordVerifier ({ function createPasswordVerifier({
passwordStrength = 'medium' passwordStrength = ''
} = {}) { } = {}) {
return function (password) { return function (password) {
const passwordRegExp = passwordRules[passwordStrength] const passwordRegExp = passwordRules[passwordStrength]
...@@ -275,11 +291,12 @@ function createPasswordVerifier ({ ...@@ -275,11 +291,12 @@ function createPasswordVerifier ({
} }
class Validator { class Validator {
constructor ({ constructor({
passwordStrength = 'medium' passwordStrength = ''
} = {}) { } = {}) {
this.baseValidator = baseValidator this.baseValidator = baseValidator
this.customValidator = Object.create(null) this.customValidator = Object.create(null)
if (passwordStrength) {
this.mixin( this.mixin(
'password', 'password',
createPasswordVerifier({ createPasswordVerifier({
...@@ -287,16 +304,17 @@ class Validator { ...@@ -287,16 +304,17 @@ class Validator {
}) })
) )
} }
}
mixin (type, handler) { mixin(type, handler) {
this.customValidator[type] = handler this.customValidator[type] = handler
} }
getRealBaseValidator (type) { getRealBaseValidator(type) {
return this.customValidator[type] || this.baseValidator[type] return this.customValidator[type] || this.baseValidator[type]
} }
get validator () { get validator() {
return new Proxy({}, { return new Proxy({}, {
get: (_, prop) => { get: (_, prop) => {
if (typeof prop !== 'string') { if (typeof prop !== 'string') {
...@@ -318,7 +336,7 @@ class Validator { ...@@ -318,7 +336,7 @@ class Validator {
}) })
} }
validate (value = {}, schema = {}) { validate(value = {}, schema = {}) {
for (const schemaKey in schema) { for (const schemaKey in schema) {
let schemaValue = schema[schemaKey] let schemaValue = schema[schemaKey]
if (getType(schemaValue) === 'string') { if (getType(schemaValue) === 'string') {
...@@ -358,7 +376,7 @@ class Validator { ...@@ -358,7 +376,7 @@ class Validator {
} }
} }
function checkClientInfo (clientInfo) { function checkClientInfo(clientInfo) {
const stringNotRequired = { const stringNotRequired = {
required: false, required: false,
type: 'string' type: 'string'
......
...@@ -110,7 +110,7 @@ module.exports = { ...@@ -110,7 +110,7 @@ module.exports = {
this.hooks = this.configUtils.getHooks() this.hooks = this.configUtils.getHooks()
this.validator = new Validator({ this.validator = new Validator({
passwordStrength: this.config.passwordStrength || 'medium' passwordStrength: this.config.passwordStrength
}) })
/** /**
* 示例:覆盖密码验证规则 * 示例:覆盖密码验证规则
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册