-
Notifications
You must be signed in to change notification settings - Fork 4
/
validate.go
66 lines (61 loc) · 1.83 KB
/
validate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package model
import (
"strings"
"github.com/pkg/errors"
"golang.org/x/exp/slices"
)
var goKeywords = []string{
"break", "case", "chan", "const", "continue", "default", "defer", "else",
"fallthrough", "for", "func", "go", "goto", "if", "import", "interface",
"map", "package", "range", "return", "select", "struct", "switch", "type", "var",
}
var reservedNames = map[string][]string{"audit": {"audit", "audit_record"}}
//nolint:gocognit
func (m *Model) Validate(mods []string, models Models, groups Groups) error {
if len(m.PKs()) == 0 {
return errors.Errorf("model [%s] has no primary key", m.Name)
}
if m.Package == "vendor" {
return errors.Errorf("model [%s] uses [vendor] package, which is reserved by Go", m.Name)
}
for _, mod := range mods {
if slices.Contains(reservedNames[mod], m.Name) {
return errors.Errorf("model [%s] uses name which is reserved by [%s]", m.Name, mod)
}
}
if m.IsRevision() {
hc := m.HistoryColumns(true)
if hc.Err != nil {
return hc.Err
}
hc = m.HistoryColumns(false)
if hc.Err != nil {
return hc.Err
}
}
if m.IsSoftDelete() {
if d := m.Columns.WithTag("deleted"); len(d) != 1 {
return errors.New("when set to soft delete, model must have one column tagged [deleted]")
}
}
for _, col := range m.Columns {
if slices.Contains(goKeywords, col.Name) {
return errors.Errorf("column [%s] uses reserved keyword", col.Name)
}
}
for _, rel := range m.Relations {
for _, s := range rel.Src {
if m.Columns.Get(s) == nil {
return errors.Errorf("relation [%s] references missing source column [%s]", rel.Name, s)
}
}
}
if len(m.Group) > 0 {
if groups.Get(m.Group...) == nil {
if len(m.Group) != 1 && models.Get(m.Group[0]) == nil {
return errors.Errorf("model [%s] references undefined group [%s]", m.Name, strings.Join(m.Group, "/"))
}
}
}
return nil
}