-
-
Notifications
You must be signed in to change notification settings - Fork 497
Expand file tree
/
Copy pathschema_validation_settings.go
More file actions
160 lines (133 loc) · 6.29 KB
/
schema_validation_settings.go
File metadata and controls
160 lines (133 loc) · 6.29 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package openapi3
import (
"sync"
)
// SchemaValidationOption describes options a user has when validating request / response bodies.
type SchemaValidationOption func(*schemaValidationSettings)
type RegexCompilerFunc func(expr string) (RegexMatcher, error)
type RegexMatcher interface {
MatchString(s string) bool
}
type schemaValidationSettings struct {
failfast bool
multiError bool
asreq, asrep bool // exclusive (XOR) fields
formatValidationEnabled bool
patternValidationDisabled bool
readOnlyValidationDisabled bool
writeOnlyValidationDisabled bool
regexCompiler RegexCompilerFunc
onceSettingDefaults sync.Once
defaultsSet func()
customizeMessageError func(err *SchemaError) string
// Per-validation format validators (checked before global ones)
stringFormats map[string]StringFormatValidator
numberFormats map[string]NumberFormatValidator
integerFormats map[string]IntegerFormatValidator
}
// FailFast returns schema validation errors quicker.
func FailFast() SchemaValidationOption {
return func(s *schemaValidationSettings) { s.failfast = true }
}
func MultiErrors() SchemaValidationOption {
return func(s *schemaValidationSettings) { s.multiError = true }
}
func VisitAsRequest() SchemaValidationOption {
return func(s *schemaValidationSettings) { s.asreq, s.asrep = true, false }
}
func VisitAsResponse() SchemaValidationOption {
return func(s *schemaValidationSettings) { s.asreq, s.asrep = false, true }
}
// EnableFormatValidation setting makes Validate not return an error when validating documents that mention schema formats that are not defined by the OpenAPIv3 specification.
func EnableFormatValidation() SchemaValidationOption {
return func(s *schemaValidationSettings) { s.formatValidationEnabled = true }
}
// DisablePatternValidation setting makes Validate not return an error when validating patterns that are not supported by the Go regexp engine.
func DisablePatternValidation() SchemaValidationOption {
return func(s *schemaValidationSettings) { s.patternValidationDisabled = true }
}
// DisableReadOnlyValidation setting makes Validate not return an error when validating properties marked as read-only
func DisableReadOnlyValidation() SchemaValidationOption {
return func(s *schemaValidationSettings) { s.readOnlyValidationDisabled = true }
}
// DisableWriteOnlyValidation setting makes Validate not return an error when validating properties marked as write-only
func DisableWriteOnlyValidation() SchemaValidationOption {
return func(s *schemaValidationSettings) { s.writeOnlyValidationDisabled = true }
}
// DefaultsSet executes the given callback (once) IFF schema validation set default values.
func DefaultsSet(f func()) SchemaValidationOption {
return func(s *schemaValidationSettings) { s.defaultsSet = f }
}
// SetSchemaErrorMessageCustomizer allows to override the schema error message.
// If the passed function returns an empty string, it returns to the previous Error() implementation.
func SetSchemaErrorMessageCustomizer(f func(err *SchemaError) string) SchemaValidationOption {
return func(s *schemaValidationSettings) { s.customizeMessageError = f }
}
// SetSchemaRegexCompiler allows to override the regex implementation used to validate field "pattern".
func SetSchemaRegexCompiler(c RegexCompilerFunc) SchemaValidationOption {
return func(s *schemaValidationSettings) { s.regexCompiler = c }
}
// WithStringFormatValidators adds per-validation string format validators.
// These validators are checked before global SchemaStringFormats and allow
// different validations for the same format name across different specs.
func WithStringFormatValidators(validators map[string]StringFormatValidator) SchemaValidationOption {
return func(s *schemaValidationSettings) {
s.stringFormats = validators
}
}
// WithStringFormatValidator adds a single per-validation string format validator.
// This validator is checked before global SchemaStringFormats and allows
// different validations for the same format name across different specs.
func WithStringFormatValidator(name string, validator StringFormatValidator) SchemaValidationOption {
return func(s *schemaValidationSettings) {
if s.stringFormats == nil {
s.stringFormats = make(map[string]StringFormatValidator)
}
s.stringFormats[name] = validator
}
}
// WithNumberFormatValidators adds per-validation number format validators.
// These validators are checked before global SchemaNumberFormats and allow
// different validations for the same format name across different specs.
func WithNumberFormatValidators(validators map[string]NumberFormatValidator) SchemaValidationOption {
return func(s *schemaValidationSettings) {
s.numberFormats = validators
}
}
// WithNumberFormatValidator adds a single per-validation number format validator.
// This validator is checked before global SchemaNumberFormats and allows
// different validations for the same format name across different specs.
func WithNumberFormatValidator(name string, validator NumberFormatValidator) SchemaValidationOption {
return func(s *schemaValidationSettings) {
if s.numberFormats == nil {
s.numberFormats = make(map[string]NumberFormatValidator)
}
s.numberFormats[name] = validator
}
}
// WithIntegerFormatValidators adds per-validation integer format validators.
// These validators are checked before global SchemaIntegerFormats and allow
// different validations for the same format name across different specs.
func WithIntegerFormatValidators(validators map[string]IntegerFormatValidator) SchemaValidationOption {
return func(s *schemaValidationSettings) {
s.integerFormats = validators
}
}
// WithIntegerFormatValidator adds a single per-validation integer format validator.
// This validator is checked before global SchemaIntegerFormats and allows
// different validations for the same format name across different specs.
func WithIntegerFormatValidator(name string, validator IntegerFormatValidator) SchemaValidationOption {
return func(s *schemaValidationSettings) {
if s.integerFormats == nil {
s.integerFormats = make(map[string]IntegerFormatValidator)
}
s.integerFormats[name] = validator
}
}
func newSchemaValidationSettings(opts ...SchemaValidationOption) *schemaValidationSettings {
settings := &schemaValidationSettings{}
for _, opt := range opts {
opt(settings)
}
return settings
}