summaryrefslogtreecommitdiff
path: root/proto/validate
diff options
context:
space:
mode:
authorraven <citrons@mondecitronne.com>2025-10-22 19:37:40 -0500
committerraven <citrons@mondecitronne.com>2026-02-09 13:14:00 -0600
commit71722ee7c2f833f6244410f6ddad688a2b5c20c3 (patch)
treeaa92426c8d12e47b5dd5bb91d8d8d13a73687e1c /proto/validate
parente65a9a95e0ece214249548a263f9a7a6185bd1e0 (diff)
validation as a subpackage of proto
Diffstat (limited to 'proto/validate')
-rw-r--r--proto/validate/validate.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/proto/validate/validate.go b/proto/validate/validate.go
new file mode 100644
index 0000000..7251895
--- /dev/null
+++ b/proto/validate/validate.go
@@ -0,0 +1,49 @@
+package validate
+
+import (
+ "strings"
+ "unicode"
+)
+
+func Name(name string) bool {
+ if len(Fold(name)) == 0 || len(name) > 64 {
+ return false
+ }
+ for _, r := range name {
+ if unicode.IsControl(r) {
+ return false
+ }
+ }
+ return true
+}
+
+func Fold(s string) string {
+ var sb strings.Builder
+ var wasSpace bool
+ for _, r := range s {
+ for {
+ f := unicode.SimpleFold(r)
+ if f <= r {
+ r = f
+ break
+ }
+ r = f
+ }
+ r = unicode.ToLower(r)
+
+ if !unicode.IsPrint(r) {
+ continue
+ }
+
+ if r == ' ' {
+ if wasSpace {
+ continue
+ }
+ wasSpace = true
+ } else {
+ wasSpace = false
+ }
+ sb.WriteRune(r)
+ }
+ return strings.TrimSpace(sb.String())
+}