-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.go
67 lines (52 loc) · 1.4 KB
/
input.go
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
// Copyright 2021 Hyperscale. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package validator
import "net/url"
// InputValidator interface.
type InputValidator interface {
ValidateMap(input map[string]interface{}) map[string][]error
ValidateValues(values url.Values) map[string][]error
}
type inputValidator struct {
validators map[string][]Validator
}
// NewInputValidator constructor.
func NewInputValidator(validators map[string][]Validator) InputValidator {
return &inputValidator{
validators: validators,
}
}
func (v inputValidator) validateField(key string, value interface{}) []error {
errs := []error{}
validators, ok := v.validators[key]
if !ok {
return errs
}
for _, validator := range validators {
if err := validator.Validate(value); err != nil {
errs = append(errs, err)
}
}
return errs
}
func (v inputValidator) ValidateMap(input map[string]interface{}) map[string][]error {
errs := make(map[string][]error)
for key, val := range input {
if err := v.validateField(key, val); len(err) > 0 {
errs[key] = err
}
}
return errs
}
func (v inputValidator) ValidateValues(values url.Values) map[string][]error {
errs := make(map[string][]error)
for key, vals := range values {
for _, val := range vals {
if err := v.validateField(key, val); len(err) > 0 {
errs[key] = err
}
}
}
return errs
}