validation.gno
1.83 Kb · 90 lines
1package blueprint
2
3import (
4 "strconv"
5 "strings"
6 "unicode/utf8"
7)
8
9func assertBlueprintName(name string) {
10 if len(name) == 0 {
11 panic("name cannot be empty")
12 }
13 if utf8.RuneCountInString(name) > 100 {
14 panic("name too long (max 100)")
15 }
16
17 trimmed := strings.TrimSpace(name)
18 if len(trimmed) == 0 {
19 panic("name cannot be only whitespace")
20 }
21 if name != trimmed {
22 panic("name cannot have leading or trailing whitespace")
23 }
24
25 for _, ch := range name {
26 if ch < 0x20 {
27 panic("name cannot contain control characters")
28 }
29 }
30}
31
32func assertBiomeName(biomeName string) {
33 if biomeName == "" {
34 panic("biome name cannot be empty")
35 }
36}
37
38func assertBlueprintSize(size string) {
39 if size != "1" && size != "3" {
40 panic("invalid blueprint size")
41 }
42}
43
44func assertChunkKey(chunkKey string) {
45 if chunkKey == "" {
46 panic("chunkKey must not be empty")
47 }
48 if utf8.RuneCountInString(chunkKey) > 256 {
49 panic("chunkKey too long (max 256)")
50 }
51 for _, ch := range chunkKey {
52 if ch < 0x20 {
53 panic("chunkKey cannot contain control characters")
54 }
55 }
56}
57
58func assertCreationCost(cost int64) {
59 if cost < 0 {
60 panic("creationCost must be non-negative")
61 }
62}
63
64func assertListLimit(field string, count int) {
65 if count > listLimit {
66 panic(field + " exceeds listLimit (max: " + strconv.Itoa(listLimit) + ")")
67 }
68}
69
70func assertListPageCount(page int, count int) {
71 if page < 1 {
72 panic("page must be at least 1")
73 }
74 if count < 1 {
75 panic("count must be at least 1")
76 }
77 assertListLimit("count", count)
78}
79
80func assertBatchLimit(field string, count int) {
81 if count > batchLimit {
82 panic(field + " exceeds batchLimit (max: " + strconv.Itoa(batchLimit) + ")")
83 }
84}
85
86func assertExactPayment(expected int64, actual int64) {
87 if actual != expected {
88 panic("exact payment required: expected " + strconv.FormatInt(expected, 10) + ", got " + strconv.FormatInt(actual, 10))
89 }
90}