@@ -62,10 +62,13 @@ pub(crate) static KERBEROS_REALM_NAME_REGEX: LazyLock<Regex> = LazyLock::new(||
62
62
. expect ( "failed to compile Kerberos realm name regex" )
63
63
} ) ;
64
64
65
+ type Result < T = ( ) , E = Errors > = std:: result:: Result < T , E > ;
66
+
67
+ /// A collection of errors discovered during validation.
65
68
#[ derive( Debug ) ]
66
- pub struct ValidationErrors ( Vec < ValidationError > ) ;
69
+ pub struct Errors ( Vec < Error > ) ;
67
70
68
- impl Display for ValidationErrors {
71
+ impl Display for Errors {
69
72
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
70
73
for ( i, error) in self . 0 . iter ( ) . enumerate ( ) {
71
74
let prefix = match i {
@@ -77,10 +80,11 @@ impl Display for ValidationErrors {
77
80
Ok ( ( ) )
78
81
}
79
82
}
80
- impl std:: error:: Error for ValidationErrors { }
83
+ impl std:: error:: Error for Errors { }
81
84
85
+ /// A single validation error.
82
86
#[ derive( Debug , Snafu ) ]
83
- pub enum ValidationError {
87
+ pub enum Error {
84
88
#[ snafu( transparent) ]
85
89
Regex { source : RegexError } ,
86
90
#[ snafu( display( "input is {length} bytes long but must be no more than {max_length}" ) ) ]
@@ -121,7 +125,7 @@ impl Display for RegexError {
121
125
impl std:: error:: Error for RegexError { }
122
126
123
127
/// Returns [`Ok`] if `value`'s length fits within `max_length`.
124
- fn validate_str_length ( value : & str , max_length : usize ) -> Result < ( ) , ValidationError > {
128
+ fn validate_str_length ( value : & str , max_length : usize ) -> Result < ( ) , Error > {
125
129
if value. len ( ) > max_length {
126
130
TooLongSnafu {
127
131
length : value. len ( ) ,
@@ -139,7 +143,7 @@ fn validate_str_regex(
139
143
regex : & ' static Regex ,
140
144
error_msg : & ' static str ,
141
145
examples : & ' static [ & ' static str ] ,
142
- ) -> Result < ( ) , ValidationError > {
146
+ ) -> Result < ( ) , Error > {
143
147
if regex. is_match ( value) {
144
148
Ok ( ( ) )
145
149
} else {
@@ -157,22 +161,20 @@ fn validate_str_regex(
157
161
}
158
162
159
163
/// Returns [`Ok`] if *all* validations are [`Ok`], otherwise returns all errors.
160
- fn validate_all (
161
- validations : impl IntoIterator < Item = Result < ( ) , ValidationError > > ,
162
- ) -> Result < ( ) , ValidationErrors > {
164
+ fn validate_all ( validations : impl IntoIterator < Item = Result < ( ) , Error > > ) -> Result {
163
165
let errors = validations
164
166
. into_iter ( )
165
167
. filter_map ( |res| res. err ( ) )
166
168
. collect :: < Vec < _ > > ( ) ;
167
169
if errors. is_empty ( ) {
168
170
Ok ( ( ) )
169
171
} else {
170
- Err ( ValidationErrors ( errors) )
172
+ Err ( Errors ( errors) )
171
173
}
172
174
}
173
175
174
176
/// Tests for a string that conforms to the definition of a subdomain in DNS (RFC 1123).
175
- pub fn is_rfc_1123_subdomain ( value : & str ) -> Result < ( ) , ValidationErrors > {
177
+ pub fn is_rfc_1123_subdomain ( value : & str ) -> Result {
176
178
validate_all ( [
177
179
validate_str_length ( value, RFC_1123_SUBDOMAIN_MAX_LENGTH ) ,
178
180
validate_str_regex (
@@ -186,7 +188,7 @@ pub fn is_rfc_1123_subdomain(value: &str) -> Result<(), ValidationErrors> {
186
188
187
189
/// Tests for a string that conforms to the definition of a label in DNS (RFC 1123).
188
190
/// Maximum label length supported by k8s is 63 characters (minimum required).
189
- pub fn is_rfc_1123_label ( value : & str ) -> Result < ( ) , ValidationErrors > {
191
+ pub fn is_rfc_1123_label ( value : & str ) -> Result {
190
192
validate_all ( [
191
193
validate_str_length ( value, RFC_1123_LABEL_MAX_LENGTH ) ,
192
194
validate_str_regex (
@@ -199,7 +201,7 @@ pub fn is_rfc_1123_label(value: &str) -> Result<(), ValidationErrors> {
199
201
}
200
202
201
203
/// Tests for a string that conforms to the definition of a label in DNS (RFC 1035).
202
- pub fn is_rfc_1035_label ( value : & str ) -> Result < ( ) , ValidationErrors > {
204
+ pub fn is_rfc_1035_label ( value : & str ) -> Result {
203
205
validate_all ( [
204
206
validate_str_length ( value, RFC_1035_LABEL_MAX_LENGTH ) ,
205
207
validate_str_regex (
@@ -211,7 +213,10 @@ pub fn is_rfc_1035_label(value: &str) -> Result<(), ValidationErrors> {
211
213
] )
212
214
}
213
215
214
- pub fn is_kerberos_realm_name ( value : & str ) -> Result < ( ) , ValidationErrors > {
216
+ /// Tests whether a string looks like a reasonable Kerberos realm name.
217
+ ///
218
+ /// This check is much stricter than krb5's own validation,
219
+ pub fn is_kerberos_realm_name ( value : & str ) -> Result {
215
220
validate_all ( [ validate_str_regex (
216
221
value,
217
222
& KERBEROS_REALM_NAME_REGEX ,
@@ -238,7 +243,7 @@ fn mask_trailing_dash(mut name: String) -> String {
238
243
///
239
244
/// * `name` - is the name to check for validity
240
245
/// * `prefix` - indicates whether `name` is just a prefix (ending in a dash, which would otherwise not be legal at the end)
241
- pub fn name_is_dns_subdomain ( name : & str , prefix : bool ) -> Result < ( ) , ValidationErrors > {
246
+ pub fn name_is_dns_subdomain ( name : & str , prefix : bool ) -> Result {
242
247
let mut name = name. to_string ( ) ;
243
248
if prefix {
244
249
name = mask_trailing_dash ( name) ;
@@ -254,7 +259,7 @@ pub fn name_is_dns_subdomain(name: &str, prefix: bool) -> Result<(), ValidationE
254
259
///
255
260
/// * `name` - is the name to check for validity
256
261
/// * `prefix` - indicates whether `name` is just a prefix (ending in a dash, which would otherwise not be legal at the end)
257
- pub fn name_is_dns_label ( name : & str , prefix : bool ) -> Result < ( ) , ValidationErrors > {
262
+ pub fn name_is_dns_label ( name : & str , prefix : bool ) -> Result {
258
263
let mut name = name. to_string ( ) ;
259
264
if prefix {
260
265
name = mask_trailing_dash ( name) ;
@@ -266,7 +271,7 @@ pub fn name_is_dns_label(name: &str, prefix: bool) -> Result<(), ValidationError
266
271
/// Validates a namespace name.
267
272
///
268
273
/// See [`name_is_dns_label`] for more information.
269
- pub fn validate_namespace_name ( name : & str , prefix : bool ) -> Result < ( ) , ValidationErrors > {
274
+ pub fn validate_namespace_name ( name : & str , prefix : bool ) -> Result {
270
275
name_is_dns_label ( name, prefix)
271
276
}
272
277
0 commit comments