83e6079f by slene

fix MinSize / MaxSize / Length should use Rune Count

1 parent d043ebcd
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
5 "reflect" 5 "reflect"
6 "regexp" 6 "regexp"
7 "time" 7 "time"
8 "unicode/utf8"
8 ) 9 )
9 10
10 var MessageTmpls = map[string]string{ 11 var MessageTmpls = map[string]string{
...@@ -158,7 +159,7 @@ type MinSize struct { ...@@ -158,7 +159,7 @@ type MinSize struct {
158 159
159 func (m MinSize) IsSatisfied(obj interface{}) bool { 160 func (m MinSize) IsSatisfied(obj interface{}) bool {
160 if str, ok := obj.(string); ok { 161 if str, ok := obj.(string); ok {
161 return len(str) >= m.Min 162 return utf8.RuneCountInString(str) >= m.Min
162 } 163 }
163 v := reflect.ValueOf(obj) 164 v := reflect.ValueOf(obj)
164 if v.Kind() == reflect.Slice { 165 if v.Kind() == reflect.Slice {
...@@ -187,7 +188,7 @@ type MaxSize struct { ...@@ -187,7 +188,7 @@ type MaxSize struct {
187 188
188 func (m MaxSize) IsSatisfied(obj interface{}) bool { 189 func (m MaxSize) IsSatisfied(obj interface{}) bool {
189 if str, ok := obj.(string); ok { 190 if str, ok := obj.(string); ok {
190 return len(str) <= m.Max 191 return utf8.RuneCountInString(str) <= m.Max
191 } 192 }
192 v := reflect.ValueOf(obj) 193 v := reflect.ValueOf(obj)
193 if v.Kind() == reflect.Slice { 194 if v.Kind() == reflect.Slice {
...@@ -216,7 +217,7 @@ type Length struct { ...@@ -216,7 +217,7 @@ type Length struct {
216 217
217 func (l Length) IsSatisfied(obj interface{}) bool { 218 func (l Length) IsSatisfied(obj interface{}) bool {
218 if str, ok := obj.(string); ok { 219 if str, ok := obj.(string); ok {
219 return len(str) == l.N 220 return utf8.RuneCountInString(str) == l.N
220 } 221 }
221 v := reflect.ValueOf(obj) 222 v := reflect.ValueOf(obj)
222 if v.Kind() == reflect.Slice { 223 if v.Kind() == reflect.Slice {
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!