Skip to content

Commit ac45189

Browse files
committed
add validation to node Interface length
1 parent 422400a commit ac45189

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

node-registrar/pkg/db/models.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package db
22

33
import (
4+
"errors"
45
"time"
56

67
"github.com/lib/pq"
8+
"gorm.io/gorm"
79
)
810

911
type Account struct {
@@ -55,6 +57,20 @@ type Node struct {
5557
Approved bool `json:"approved"`
5658
}
5759

60+
func (n *Node) AfterFind(tx *gorm.DB) error {
61+
if len(n.Interfaces) == 0 {
62+
return errors.New("interfaces must not be empty")
63+
}
64+
return nil
65+
}
66+
67+
func (n *Node) BeforeCreate(tx *gorm.DB) (err error) {
68+
if len(n.Interfaces) == 0 {
69+
return errors.New("interfaces must not be empty")
70+
}
71+
return nil
72+
}
73+
5874
type UptimeReport struct {
5975
ID uint64 `gorm:"primaryKey;autoIncrement"`
6076
NodeID uint64 `gorm:"index" json:"node_id"`

0 commit comments

Comments
 (0)