72 lines
1.6 KiB
Go
72 lines
1.6 KiB
Go
// Package devicecatalog manages the two small reference tables staff pick
|
|
// from when creating an order — device_groups (drives order-number
|
|
// prefixing, see internal/ordernum) and device_brands (a plain name list).
|
|
// Both are owner-curated taxonomy, not order data itself: orders keep their
|
|
// own device_type/device_brand text columns and only optionally link back
|
|
// here via device_group_id/device_brand_id.
|
|
package devicecatalog
|
|
|
|
import (
|
|
"strings"
|
|
"unicode"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
const maxNameLen = 120
|
|
const maxPrefixLen = 4
|
|
|
|
type groupInput struct {
|
|
Name string
|
|
Prefix string
|
|
}
|
|
|
|
func (g groupInput) validate() string {
|
|
if utf8.RuneCountInString(strings.TrimSpace(g.Name)) == 0 {
|
|
return "name is required"
|
|
}
|
|
if utf8.RuneCountInString(g.Name) > maxNameLen {
|
|
return "name is too long"
|
|
}
|
|
prefix := strings.TrimSpace(g.Prefix)
|
|
if prefix == "" {
|
|
return "prefix is required"
|
|
}
|
|
if utf8.RuneCountInString(prefix) > maxPrefixLen {
|
|
return "prefix is too long (max 4 characters)"
|
|
}
|
|
for _, r := range prefix {
|
|
if !unicode.IsLetter(r) {
|
|
return "prefix must contain letters only"
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
type brandInput struct {
|
|
Name string
|
|
}
|
|
|
|
func (b brandInput) validate() string {
|
|
if utf8.RuneCountInString(strings.TrimSpace(b.Name)) == 0 {
|
|
return "name is required"
|
|
}
|
|
if utf8.RuneCountInString(b.Name) > maxNameLen {
|
|
return "name is too long"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
type faultInput struct {
|
|
Label string
|
|
}
|
|
|
|
func (f faultInput) validate() string {
|
|
if utf8.RuneCountInString(strings.TrimSpace(f.Label)) == 0 {
|
|
return "label is required"
|
|
}
|
|
if utf8.RuneCountInString(f.Label) > maxNameLen {
|
|
return "label is too long"
|
|
}
|
|
return ""
|
|
}
|