Skip to content

Commit

Permalink
validation
Browse files Browse the repository at this point in the history
  • Loading branch information
iesreza committed Jun 7, 2024
1 parent 4e1a2f8 commit 806c217
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions lib/validation/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package validation
import (
"fmt"
"github.com/getevo/evo/v2/lib/db"
scm "github.com/getevo/evo/v2/lib/db/schema"
"github.com/getevo/evo/v2/lib/generic"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"net/url"
"reflect"
"regexp"
"strconv"
"strings"
Expand All @@ -16,6 +18,7 @@ import (
var DBValidators = map[*regexp.Regexp]func(match []string, value *generic.Value, stmt *gorm.Statement, field *schema.Field) error{
regexp.MustCompile("^unique$"): uniqueValidator,
regexp.MustCompile("^fk$"): foreignKeyValidator,
regexp.MustCompile("^enum$"): enumValidator,
}

var Validators = map[*regexp.Regexp]func(match []string, value *generic.Value) error{
Expand All @@ -37,7 +40,36 @@ var Validators = map[*regexp.Regexp]func(match []string, value *generic.Value) e
regexp.MustCompile(`^ip$`): ipValidator,
}

var enumRegex = regexp.MustCompile(`(?m)enum\(([^)]+)\)`)
var enumBodyRegex = regexp.MustCompile(`(?m)'([^']*)'`)

func enumValidator(match []string, value *generic.Value, stmt *gorm.Statement, field *schema.Field) error {
var v = value.String()
if field.StructField.Type.Kind() == reflect.Ptr && v == "<nil>" {
return nil
}
var tag = field.Tag.Get("gorm")
var expected = ""
if tag != "" {
var enumMatch = enumRegex.FindAllStringSubmatch(tag, 1)
if len(enumMatch) == 1 {
var values = enumBodyRegex.FindAllStringSubmatch(enumMatch[0][1], -1)
for _, item := range values {
expected += "," + item[1]
if item[1] == v {
return nil
}
}
}
}
return fmt.Errorf("invalid value, expected values are: %s", strings.TrimLeft(expected, ","))
}

func uniqueValidator(match []string, value *generic.Value, stmt *gorm.Statement, field *schema.Field) error {
if field.StructField.Type.Kind() == reflect.Ptr && value.String() == "<nil>" {
return nil
}

var c int64
db.Where(field.DBName+" = ?", value.Input).Table(stmt.Table).Count(&c)
if c > 0 {
Expand All @@ -47,12 +79,17 @@ func uniqueValidator(match []string, value *generic.Value, stmt *gorm.Statement,
}

func foreignKeyValidator(match []string, value *generic.Value, stmt *gorm.Statement, field *schema.Field) error {
if field.StructField.Type.Kind() == reflect.Ptr && value.String() == "<nil>" {
return nil
}
var c int64
//field.Tag.Get("")
//scm.Find()
db.Where(field.DBName+" = ?", value.Input).Table(stmt.Table).Count(&c)
if c > 0 {
return fmt.Errorf("duplicate entry")
if foreignTable, ok := field.TagSettings["FK"]; ok {
if foreignModel := scm.Find(foreignTable); foreignModel != nil {
db.Where(foreignModel.PrimaryKey[0]+" = ?", value.Input).Table(foreignTable).Count(&c)
if c == 0 {
return fmt.Errorf("value does not match foreign key")
}
}
}
return nil
}
Expand Down

0 comments on commit 806c217

Please sign in to comment.