diff options
author | sinanmohd <sinan@sinanmohd.com> | 2024-03-16 11:32:54 +0530 |
---|---|---|
committer | sinanmohd <sinan@sinanmohd.com> | 2024-03-16 11:33:26 +0530 |
commit | 61265f790e3c32accb3ce844422bb6b23db7fe8a (patch) | |
tree | b44c1a7c4725f4aed418a178aad0a63c54a8822b /db | |
parent | fb387fe290d907dd8a1b0e3600c5468043071628 (diff) |
validator: init
Diffstat (limited to 'db')
-rw-r--r-- | db/account.go | 33 | ||||
-rw-r--r-- | db/main.go | 5 |
2 files changed, 30 insertions, 8 deletions
diff --git a/db/account.go b/db/account.go index c7f76bf..507060e 100644 --- a/db/account.go +++ b/db/account.go @@ -1,18 +1,22 @@ package db -import "errors" +import ( + "errors" + "fmt" + "log" +) type Account struct { - UserName string - PassHash string `json:",omitempty"` + UserName string `validate:"required,alphanum,max=64"` + PassHash string `json:",omitempty" validate:"required,min=10,max=128"` Info *Login } type Login struct { id uint - Level uint - FirstName, LastName string + Level uint `validate:"gte=0,lte=100"` + FirstName, LastName string `validate:"required,alphanumunicode"` Bearer *Bearer } @@ -29,10 +33,18 @@ func (ac *Account) CreateAccount(safe *SafeDB) error { VALUES (NULL, ?, ?, ?, ?, ?); ` + err := safe.validate.Struct(ac) + if err == nil { + err = safe.validate.Struct(ac.Login) + } + if err != nil { + return err + } + safe.mu.Lock() defer safe.mu.Unlock() - _, err := safe.db.Exec( + _, err = safe.db.Exec( sqlStatement, ac.UserName, ToBlake3(ac.PassHash), @@ -52,6 +64,13 @@ func (ac *Account) Login(safe *SafeDB) error { WHERE Accounts.UserName = ? ` + err := safe.validate.Struct(ac) + fmt.Println(ac.PassHash, ac.UserName) + if err != nil { + log.Println(err) + return err + } + ac.Info = &Login{} ac.Info.Bearer = &Bearer{} safe.mu.Lock() @@ -59,7 +78,7 @@ func (ac *Account) Login(safe *SafeDB) error { safe.mu.Unlock() var PassHash string - err := row.Scan( + err = row.Scan( &ac.Info.id, &PassHash, &ac.Info.FirstName, @@ -6,11 +6,13 @@ import ( "path/filepath" "sync" + "github.com/go-playground/validator/v10" _ "github.com/mattn/go-sqlite3" ) type SafeDB struct { - mu sync.Mutex + mu sync.Mutex + validate *validator.Validate path string db *sql.DB @@ -70,5 +72,6 @@ func NewSafeDB() (*SafeDB, error) { return nil, err } + safe.validate = validator.New(validator.WithRequiredStructEnabled()) return safe, nil } |