github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zvalid/has.go (about) 1 package zvalid 2 3 import ( 4 "strings" 5 "unicode" 6 ) 7 8 // HasLetter must contain letters not case sensitive 9 func (v Engine) HasLetter(customError ...string) Engine { 10 return pushQueue(&v, func(v *Engine) *Engine { 11 for _, rv := range v.value { 12 if unicode.IsLower(rv) || unicode.IsUpper(rv) { 13 return v 14 } 15 } 16 17 v.err = setError(v, "必须包含字母", customError...) 18 return v 19 }) 20 } 21 22 // HasLower must contain lowercase letters 23 func (v Engine) HasLower(customError ...string) Engine { 24 return pushQueue(&v, func(v *Engine) *Engine { 25 for _, rv := range v.value { 26 if unicode.IsLower(rv) { 27 return v 28 } 29 } 30 31 v.err = setError(v, "必须包含小写字母", customError...) 32 return v 33 }) 34 } 35 36 // HasUpper must contain uppercase letters 37 func (v Engine) HasUpper(customError ...string) Engine { 38 return pushQueue(&v, func(v *Engine) *Engine { 39 for _, rv := range v.value { 40 if unicode.IsUpper(rv) { 41 return v 42 } 43 } 44 45 v.err = setError(v, "必须包含大写字母", customError...) 46 return v 47 }) 48 } 49 50 // HasNumber must contain numbers 51 func (v Engine) HasNumber(customError ...string) Engine { 52 return pushQueue(&v, func(v *Engine) *Engine { 53 for _, rv := range v.value { 54 if unicode.IsDigit(rv) { 55 return v 56 } 57 } 58 59 v.err = setError(v, "必须包含数字", customError...) 60 return v 61 }) 62 } 63 64 // HasSymbol must contain symbols 65 func (v Engine) HasSymbol(customError ...string) Engine { 66 return pushQueue(&v, func(v *Engine) *Engine { 67 for _, rv := range v.value { 68 if !unicode.IsDigit(rv) && !unicode.IsLetter(rv) && !unicode.Is(unicode.Han, rv) { 69 return v 70 } 71 } 72 v.err = setError(v, "必须包含符号", customError...) 73 return v 74 }) 75 } 76 77 // HasString must contain a specific string 78 func (v Engine) HasString(sub string, customError ...string) Engine { 79 return pushQueue(&v, func(v *Engine) *Engine { 80 if !strings.Contains(v.value, sub) { 81 v.err = setError(v, "必须包含特定的字符串", customError...) 82 return v 83 } 84 return v 85 }, true) 86 } 87 88 // HasPrefix must contain the specified prefix string 89 func (v Engine) HasPrefix(sub string, customError ...string) Engine { 90 return pushQueue(&v, func(v *Engine) *Engine { 91 if !strings.HasPrefix(v.value, sub) { 92 v.err = setError(v, "不允许的值", customError...) 93 return v 94 } 95 return v 96 }) 97 } 98 99 // HasSuffix contains the specified suffix string 100 func (v Engine) HasSuffix(sub string, customError ...string) Engine { 101 return pushQueue(&v, func(v *Engine) *Engine { 102 if !strings.HasSuffix(v.value, sub) { 103 v.err = setError(v, "不允许的值", customError...) 104 return v 105 } 106 return v 107 }) 108 } 109 110 // Password Universal password (any visible character, length between 6 ~ 20) 111 func (v Engine) Password(customError ...string) Engine { 112 return pushQueue(&v, func(v *Engine) *Engine { 113 err := New().Verifi(v.value, v.name).Required().MinLength(6).MaxLength(20).Error() 114 if err != nil { 115 v.err = setError(v, "值不合法", customError...) 116 } 117 return v 118 }) 119 } 120 121 // StrongPassword Strong equal strength password (length is 6 ~ 20, must include uppercase and lowercase letters, numbers and special characters) 122 func (v Engine) StrongPassword(customError ...string) Engine { 123 return pushQueue(&v, func(v *Engine) *Engine { 124 err := New().Verifi(v.value).Required().MinLength(6).MaxLength(20).HasSymbol().HasNumber().HasLetter().HasLower().Error() 125 if err != nil { 126 v.err = setError(v, "值不合法", customError...) 127 } 128 return v 129 }) 130 }