mirror of
https://gitee.com/johng/gf.git
synced 2024-12-05 05:37:55 +08:00
43 lines
836 B
Go
43 lines
836 B
Go
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with this file,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
package builtin
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/gogf/gf/v2/text/gregex"
|
|
)
|
|
|
|
// RulePostcode implements `postcode` rule:
|
|
// Postcode number.
|
|
//
|
|
// Format: postcode
|
|
type RulePostcode struct{}
|
|
|
|
func init() {
|
|
Register(RulePostcode{})
|
|
}
|
|
|
|
func (r RulePostcode) Name() string {
|
|
return "postcode"
|
|
}
|
|
|
|
func (r RulePostcode) Message() string {
|
|
return "The {field} value `{value}` is not a valid postcode format"
|
|
}
|
|
|
|
func (r RulePostcode) Run(in RunInput) error {
|
|
ok := gregex.IsMatchString(
|
|
`^\d{6}$`,
|
|
in.Value.String(),
|
|
)
|
|
if ok {
|
|
return nil
|
|
}
|
|
return errors.New(in.Message)
|
|
}
|