Repository navigation
Expand file tree
/
Copy pathdefaults.go
More file actions
72 lines (57 loc) · 2.89 KB
/
Copy pathdefaults.go
File metadata and controls
72 lines (57 loc) · 2.89 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
63
64
65
66
67
68
69
70
71
72
package validator
import (
"context"
"net/url"
"sync"
)
var defaultValidator = sync.OnceValue(func() *Validator { return MustNew() })
// Default returns the shared Validator behind the package-level functions:
// the built-in rules and filters with default options, created on first use
// and safe for concurrent use. Construct your own Validator with New when
// rules, messages, translations or other options differ.
func Default() *Validator {
return defaultValidator()
}
// Struct prepares a struct-tag validation with the Default Validator; see
// Validator.Struct for the input rules and errors.
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 using the Default
// Validator; see Validator.Valid for the verdict and error semantics.
func Valid(ctx context.Context, data any) (bool, error) { return Default().Valid(ctx, data) }
// Map prepares a Validation for a map and explicit field expressions with the
// Default Validator; see Validator.Map.
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 field
// expressions with the Default Validator; see Validator.JSON.
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 form or query data with the Default
// Validator; a repeated key becomes a []string. See Validator.Values.
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"
// with the Default Validator; see Validator.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 compiles every rule tag reachable from struct type T with the
// Default Validator and reports the invalid ones; see Validator.Check.
func Check[T any]() error { return Default().Check[T]() }