|
|
@@ -0,0 +1,75 @@
|
|
|
+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))
|
|
|
+}
|