Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

validation.gno

1.23 Kb · 63 lines
 1package definition
 2
 3import (
 4	"strconv"
 5	"strings"
 6	"unicode"
 7	"unicode/utf8"
 8)
 9
10func assertIdentifier(value, name string, limit int) {
11	if strings.TrimSpace(value) == "" {
12		panic(name + " is required")
13	}
14	if len(value) > limit {
15		panic(name + " bytes exceeds " + strconv.Itoa(limit))
16	}
17	if !utf8.ValidString(value) {
18		panic("invalid " + name)
19	}
20	for _, r := range value {
21		if unicode.IsControl(r) {
22			panic("invalid " + name)
23		}
24	}
25}
26
27func assertFields(fields []Field) int {
28	if len(fields) < 1 || len(fields) > 32 {
29		panic("field count must be between 1 and 32")
30	}
31	size := 0
32	for i, field := range fields {
33		assertIdentifier(field.Key, "field key", 128)
34		if !utf8.ValidString(field.Value) {
35			panic("invalid field value")
36		}
37		for j := 0; j < i; j++ {
38			if fields[j].Key == field.Key {
39				panic("duplicate field key")
40			}
41		}
42		size += len(field.Key) + len(field.Value)
43		if size > 16384 {
44			panic("entry bytes exceeds 16384")
45		}
46	}
47	return size
48}
49
50func assertBatchCount(count int) {
51	if count < 1 || count > 100 {
52		panic("batch count must be between 1 and 100")
53	}
54}
55
56func assertPage(page, count int) {
57	if page < 1 {
58		panic("page must be positive")
59	}
60	if count < 1 || count > 50 {
61		panic("count must be between 1 and 50")
62	}
63}