github.com/gogf/gf/v2@v2.7.4/util/gvalid/internal/builtin/builtin_date.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/gogf/gf. 6 7 package builtin 8 9 import ( 10 "errors" 11 "time" 12 13 "github.com/gogf/gf/v2/text/gregex" 14 ) 15 16 // RuleDate implements `date` rule: 17 // Standard date, like: 2006-01-02, 20060102, 2006.01.02. 18 // 19 // Format: date 20 type RuleDate struct{} 21 22 func init() { 23 Register(RuleDate{}) 24 } 25 26 func (r RuleDate) Name() string { 27 return "date" 28 } 29 30 func (r RuleDate) Message() string { 31 return "The {field} value `{value}` is not a valid date" 32 } 33 34 func (r RuleDate) Run(in RunInput) error { 35 type iTime interface { 36 Date() (year int, month time.Month, day int) 37 IsZero() bool 38 } 39 // support for time value, eg: gtime.Time/*gtime.Time, time.Time/*time.Time. 40 if obj, ok := in.Value.Val().(iTime); ok { 41 if obj.IsZero() { 42 return errors.New(in.Message) 43 } 44 } 45 if !gregex.IsMatchString( 46 `\d{4}[\.\-\_/]{0,1}\d{2}[\.\-\_/]{0,1}\d{2}`, 47 in.Value.String(), 48 ) { 49 return errors.New(in.Message) 50 } 51 return nil 52 }