| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- package fairwind
- import (
- "regexp"
- )
- type Validator interface {
- Validate(value any) bool
- }
- type MinLenghtValidator struct {
- length int
- }
- func NewMinLenghtValidator(length int) *MinLenghtValidator {
- return &MinLenghtValidator{
- length: length,
- }
- }
- func (this *MinLenghtValidator) Validate(value any) bool {
- v, ok := value.(string)
- if !ok {
- return false
- }
- return len(v) >= this.length
- }
- type MaxLenghtValidator struct {
- length int
- }
- func NewMaxLenghtValidator(length int) *MaxLenghtValidator {
- return &MaxLenghtValidator{
- length: length,
- }
- }
- func (this *MaxLenghtValidator) Validate(value any) bool {
- v, ok := value.(string)
- if !ok {
- return false
- }
- return len(v) <= this.length
- }
- type RegexValidator struct {
- expression *regexp.Regexp
- }
- func NewRegexValidator(regex string) *RegexValidator {
- expression, err := regexp.Compile(regex)
- if err != nil {
- return &RegexValidator{}
- }
- return &RegexValidator{
- expression: expression,
- }
- }
- func (this *RegexValidator) Validate(value any) bool {
- if this.expression == nil {
- return false
- }
- v, ok := value.(string)
- if !ok {
- return false
- }
- return this.expression.Match([]byte(v))
- }
|