Search Apps Documentation Source Content File Folder Download Copy Actions Download

validation.gno

1.78 Kb · 80 lines
 1package acr
 2
 3import "strings"
 4
 5const (
 6	maxHofCategoryNameLength = 128
 7	maxHofEntriesBytes       = 256 * 1024
 8	maxHofEntriesRows        = 100
 9	maxHofColumns            = 32
10)
11
12// Validation helpers in this file are for value-level checks at public API boundaries.
13//
14// Keep store-owned existence, duplicate, and index consistency checks close to
15// the store methods that own those state transitions.
16func assertListPageCount(page int, count int) {
17	if page < 1 {
18		panic("page must be at least 1")
19	}
20	if count < 1 {
21		panic("count must be at least 1")
22	}
23	if count > listLimit {
24		panic("count exceeds listLimit")
25	}
26}
27
28func validateHofCategoryName(name string) {
29	if name == "" {
30		panic("invalid name: ")
31	}
32	if len(name) > maxHofCategoryNameLength {
33		panic("category name exceeds maxHofCategoryNameLength")
34	}
35}
36
37func validateHofEntries(csvRows string) string {
38	csvRows = strings.TrimRight(csvRows, "\n")
39	if csvRows == "" {
40		return ""
41	}
42	if len(csvRows) > maxHofEntriesBytes {
43		panic("hof entries exceeds maxHofEntriesBytes")
44	}
45
46	rows := strings.Split(csvRows, "\n")
47	if len(rows) > maxHofEntriesRows {
48		panic("hof entries rows exceeds maxHofEntriesRows")
49	}
50
51	headers := strings.Split(rows[0], ",")
52	validateHofColumns(headers, true)
53
54	for i := 1; i < len(rows); i++ {
55		if rows[i] == "" {
56			panic("invalid hof csv: empty row")
57		}
58		cols := strings.Split(rows[i], ",")
59		validateHofColumns(cols, false)
60		if len(cols) != len(headers) {
61			panic("invalid hof csv: inconsistent column count")
62		}
63	}
64
65	return csvRows
66}
67
68func validateHofColumns(cols []string, header bool) {
69	if len(cols) > maxHofColumns {
70		panic("hof entries columns exceeds maxHofColumns")
71	}
72	if !header {
73		return
74	}
75	for _, col := range cols {
76		if strings.TrimSpace(col) == "" {
77			panic("invalid hof csv: empty header")
78		}
79	}
80}