-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoding.go
More file actions
57 lines (50 loc) · 1.7 KB
/
Copy pathencoding.go
File metadata and controls
57 lines (50 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package validations
import (
"encoding/base32"
"encoding/base64"
"encoding/json"
)
// Runtime helpers for the encoding validators. base32/base64 delegate to the
// stdlib decoders (authoritative — no regex, no bespoke padding rules); json uses
// encoding/json.Valid.
//
// The base32/base64 decoders allocate their decoded output even though we discard
// it, so those validators cost 1 small alloc on the success path. That is an
// accepted trade (M24): reimplementing the decoders by hand to save it would mean
// owning base64's residual-bit canonicality forever for a negligible gain — see
// the stdlib-parser note in CLAUDE.md. Do not hand-roll these to "fix" the alloc.
// ValidateBase64 reports whether s is valid standard (padded) base64.
func ValidateBase64(s string) error {
if _, err := base64.StdEncoding.DecodeString(s); err != nil {
return Base64Error{}
}
return nil
}
// ValidateBase64URL reports whether s is valid URL-safe (padded) base64.
func ValidateBase64URL(s string) error {
if _, err := base64.URLEncoding.DecodeString(s); err != nil {
return Base64URLError{}
}
return nil
}
// ValidateBase64RawURL reports whether s is valid URL-safe unpadded base64.
func ValidateBase64RawURL(s string) error {
if _, err := base64.RawURLEncoding.DecodeString(s); err != nil {
return Base64RawURLError{}
}
return nil
}
// ValidateBase32 reports whether s is valid standard (padded) base32.
func ValidateBase32(s string) error {
if _, err := base32.StdEncoding.DecodeString(s); err != nil {
return Base32Error{}
}
return nil
}
// ValidateJSON reports whether s is a well-formed JSON document.
func ValidateJSON(s string) error {
if !json.Valid([]byte(s)) {
return JSONError{}
}
return nil
}