github.com/wangyougui/gf/v2@v2.6.5/util/gvalid/internal/builtin/builtin_regex.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package builtin 8 9 import ( 10 "errors" 11 12 "github.com/wangyougui/gf/v2/text/gregex" 13 ) 14 15 // RuleRegex implements `regex` rule: 16 // Value should match custom regular expression pattern. 17 // 18 // Format: regex:pattern 19 type RuleRegex struct{} 20 21 func init() { 22 Register(RuleRegex{}) 23 } 24 25 func (r RuleRegex) Name() string { 26 return "regex" 27 } 28 29 func (r RuleRegex) Message() string { 30 return "The {field} value `{value}` must be in regex of: {pattern}" 31 } 32 33 func (r RuleRegex) Run(in RunInput) error { 34 if !gregex.IsMatchString(in.RulePattern, in.Value.String()) { 35 return errors.New(in.Message) 36 } 37 return nil 38 }