| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- 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 ExactLenghtValidator struct {
- length int
- }
- func NewExactLenghtValidator(length int) *ExactLenghtValidator {
- return &ExactLenghtValidator{
- length: length,
- }
- }
- func (this *ExactLenghtValidator) 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))
- }
|