aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/aws/smithy-go/validation.go
blob: b5eedc1f90aba394a28609701c8a4e2baacc3dac (plain) (blame)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package smithy

import (
	"bytes"
	"fmt"
	"strings"
)

// An InvalidParamsError provides wrapping of invalid parameter errors found when
// validating API operation input parameters.
type InvalidParamsError struct {
	// Context is the base context of the invalid parameter group.
	Context string
	errs    []InvalidParamError
}

// Add adds a new invalid parameter error to the collection of invalid
// parameters. The context of the invalid parameter will be updated to reflect
// this collection.
func (e *InvalidParamsError) Add(err InvalidParamError) {
	err.SetContext(e.Context)
	e.errs = append(e.errs, err)
}

// AddNested adds the invalid parameter errors from another InvalidParamsError
// value into this collection. The nested errors will have their nested context
// updated and base context to reflect the merging.
//
// Use for nested validations errors.
func (e *InvalidParamsError) AddNested(nestedCtx string, nested InvalidParamsError) {
	for _, err := range nested.errs {
		err.SetContext(e.Context)
		err.AddNestedContext(nestedCtx)
		e.errs = append(e.errs, err)
	}
}

// Len returns the number of invalid parameter errors
func (e *InvalidParamsError) Len() int {
	return len(e.errs)
}

// Error returns the string formatted form of the invalid parameters.
func (e InvalidParamsError) Error() string {
	w := &bytes.Buffer{}
	fmt.Fprintf(w, "%d validation error(s) found.\n", len(e.errs))

	for _, err := range e.errs {
		fmt.Fprintf(w, "- %s\n", err.Error())
	}

	return w.String()
}

// Errs returns a slice of the invalid parameters
func (e InvalidParamsError) Errs() []error {
	errs := make([]error, len(e.errs))
	for i := 0; i < len(errs); i++ {
		errs[i] = e.errs[i]
	}

	return errs
}

// An InvalidParamError represents an invalid parameter error type.
type InvalidParamError interface {
	error

	// Field name the error occurred on.
	Field() string

	// SetContext updates the context of the error.
	SetContext(string)

	// AddNestedContext updates the error's context to include a nested level.
	AddNestedContext(string)
}

type invalidParamError struct {
	context       string
	nestedContext string
	field         string
	reason        string
}

// Error returns the string version of the invalid parameter error.
func (e invalidParamError) Error() string {
	return fmt.Sprintf("%s, %s.", e.reason, e.Field())
}

// Field Returns the field and context the error occurred.
func (e invalidParamError) Field() string {
	sb := &strings.Builder{}
	sb.WriteString(e.context)
	if sb.Len() > 0 {
		if len(e.nestedContext) == 0 || (len(e.nestedContext) > 0 && e.nestedContext[:1] != "[") {
			sb.WriteRune('.')
		}
	}
	if len(e.nestedContext) > 0 {
		sb.WriteString(e.nestedContext)
		sb.WriteRune('.')
	}
	sb.WriteString(e.field)
	return sb.String()
}

// SetContext updates the base context of the error.
func (e *invalidParamError) SetContext(ctx string) {
	e.context = ctx
}

// AddNestedContext prepends a context to the field's path.
func (e *invalidParamError) AddNestedContext(ctx string) {
	if len(e.nestedContext) == 0 {
		e.nestedContext = ctx
		return
	}
	// Check if our nested context is an index into a slice or map
	if e.nestedContext[:1] != "[" {
		e.nestedContext = fmt.Sprintf("%s.%s", ctx, e.nestedContext)
		return
	}
	e.nestedContext = ctx + e.nestedContext
}

// An ParamRequiredError represents an required parameter error.
type ParamRequiredError struct {
	invalidParamError
}

// NewErrParamRequired creates a new required parameter error.
func NewErrParamRequired(field string) *ParamRequiredError {
	return &ParamRequiredError{
		invalidParamError{
			field:  field,
			reason: fmt.Sprintf("missing required field"),
		},
	}
}