-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefaults.go
More file actions
62 lines (47 loc) · 2.21 KB
/
Copy pathdefaults.go
File metadata and controls
62 lines (47 loc) · 2.21 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
58
59
60
61
62
package validator
import (
"context"
"net/url"
"sync"
)
var defaultValidator = sync.OnceValue(func() *Validator { return MustNew() })
// Default returns the shared Validator used by package-level helpers.
func Default() *Validator {
return defaultValidator()
}
// Struct prepares a struct-tag validation.
func Struct(data any) (*Validation, error) { return Default().Struct(data) }
// MustStruct is Struct with panic-on-error semantics.
func MustStruct(data any) *Validation { return Default().MustStruct(data) }
// Valid reports whether data passes its struct-tag rules.
func Valid(ctx context.Context, data any) (bool, error) { return Default().Valid(ctx, data) }
// Map prepares a Validation for any map type and explicit rules.
func Map[M ~map[K]V, K comparable, V any](data M, rules map[string]string) (*Validation, error) {
return Default().Map(data, rules)
}
// MustMap is Map with panic-on-error semantics.
func MustMap[M ~map[K]V, K comparable, V any](data M, rules map[string]string) *Validation {
return Default().MustMap(data, rules)
}
// JSON prepares a Validation from one JSON object and explicit rules.
func JSON[Bytes ~[]byte | ~string](data Bytes, rules map[string]string) (*Validation, error) {
return Default().JSON(data, rules)
}
// MustJSON is JSON with panic-on-error semantics.
func MustJSON[Bytes ~[]byte | ~string](data Bytes, rules map[string]string) *Validation {
return Default().MustJSON(data, rules)
}
// Values prepares a Validation from the first value of each form key.
func Values(data url.Values, rules map[string]string) (*Validation, error) {
return Default().Values(data, rules)
}
// MustValues is Values with panic-on-error semantics.
func MustValues(data url.Values, rules map[string]string) *Validation {
return Default().MustValues(data, rules)
}
// Value prepares a Validation for one value under the field name "value".
func Value(value any, rule string) (*Validation, error) { return Default().Value(value, rule) }
// MustValue is Value with panic-on-error semantics.
func MustValue(value any, rule string) *Validation { return Default().MustValue(value, rule) }
// Check reports every invalid rule tag on T; see Validator.Check.
func Check[T any]() error { return Default().Check[T]() }