-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
83 lines (74 loc) · 2.16 KB
/
schema.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import "strings"
type ValidationResult struct {
Valid bool
Errors []string
}
func ValidateField(field *Field, value any, key string, table *Table) *ValidationResult {
if value == nil {
if !field.Nullable {
return &ValidationResult{false, []string{"Field is not nullable: " + key}}
}
return &ValidationResult{true, nil}
}
if field.Type == "enum" {
params := strings.Split(strings.TrimPrefix(field.Params, ":"), ",")
for _, param := range params {
if value == param {
return &ValidationResult{true, nil}
}
}
return &ValidationResult{false, []string{"Value doesn't match any of the enum values for field: " + key}}
}
if field.Type == "id" && field.Subtype == "uuid" {
if len(value.(string)) == 36 {
return &ValidationResult{true, nil}
}
return &ValidationResult{false, []string{"Invalid UUID format for field: " + key}}
} else if field.Type == "id" && field.Subtype != "uuid" {
entities, err := ReadTable(table)
if err != nil {
return &ValidationResult{false, []string{err.Error()}}
}
idExists := false
for _, entity := range entities {
if entity[key] == value {
idExists = true
}
}
if !idExists {
return &ValidationResult{true, nil}
} else {
return &ValidationResult{false, []string{"Duplicate ID for field: " + key}}
}
}
switch value.(type) {
case bool:
if field.Type == "bool" {
return &ValidationResult{true, nil}
}
return &ValidationResult{false, []string{"Invalid value for field: " + key}}
case string:
if field.Type == "string" || (field.Type == "date" && field.Subtype != "timestamp") {
return &ValidationResult{true, nil}
}
return &ValidationResult{false, []string{"Invalid value for field: " + key}}
case float32:
case float64:
case int:
case int8:
case int16:
case int32:
case int64:
if field.Type == "number" {
return &ValidationResult{true, nil}
}
if field.Type == "date" && field.Subtype == "timestamp" {
return &ValidationResult{true, nil}
}
return &ValidationResult{false, []string{"Invalid value for field: " + key}}
default:
return &ValidationResult{false, []string{"Invalid value for field: " + key}}
}
return &ValidationResult{true, nil}
}