@@ -38,16 +38,16 @@ impl std::error::Error for ValidationError {
38
38
}
39
39
}
40
40
41
- #[ derive( Debug , Serialize , Clone , PartialEq ) ]
41
+ #[ derive( Debug , Serialize , Deserialize , Clone , PartialEq ) ]
42
42
#[ serde( untagged) ]
43
43
pub enum ValidationErrorsKind {
44
44
Struct ( Box < ValidationErrors > ) ,
45
45
List ( BTreeMap < usize , Box < ValidationErrors > > ) ,
46
46
Field ( Vec < ValidationError > ) ,
47
47
}
48
48
49
- #[ derive( Default , Debug , Serialize , Clone , PartialEq ) ]
50
- pub struct ValidationErrors ( pub HashMap < & ' static str , ValidationErrorsKind > ) ;
49
+ #[ derive( Default , Debug , Serialize , Deserialize , Clone , PartialEq ) ]
50
+ pub struct ValidationErrors ( pub HashMap < Cow < ' static , str > , ValidationErrorsKind > ) ;
51
51
52
52
impl ValidationErrors {
53
53
pub fn new ( ) -> ValidationErrors {
@@ -134,28 +134,28 @@ impl ValidationErrors {
134
134
135
135
/// Returns a map of field-level validation errors found for the struct that was validated and
136
136
/// any of it's nested structs that are tagged for validation.
137
- pub fn errors ( & self ) -> & HashMap < & ' static str , ValidationErrorsKind > {
137
+ pub fn errors ( & self ) -> & HashMap < Cow < ' static , str > , ValidationErrorsKind > {
138
138
& self . 0
139
139
}
140
140
141
141
/// Returns a mutable map of field-level validation errors found for the struct that was validated and
142
142
/// any of it's nested structs that are tagged for validation.
143
- pub fn errors_mut ( & mut self ) -> & mut HashMap < & ' static str , ValidationErrorsKind > {
143
+ pub fn errors_mut ( & mut self ) -> & mut HashMap < Cow < ' static , str > , ValidationErrorsKind > {
144
144
& mut self . 0
145
145
}
146
146
147
147
/// Consume the struct, returning the validation errors found
148
- pub fn into_errors ( self ) -> HashMap < & ' static str , ValidationErrorsKind > {
148
+ pub fn into_errors ( self ) -> HashMap < Cow < ' static , str > , ValidationErrorsKind > {
149
149
self . 0
150
150
}
151
151
152
152
/// Returns a map of only field-level validation errors found for the struct that was validated.
153
- pub fn field_errors ( & self ) -> HashMap < & ' static str , & Vec < ValidationError > > {
153
+ pub fn field_errors ( & self ) -> HashMap < Cow < ' static , str > , & Vec < ValidationError > > {
154
154
self . 0
155
155
. iter ( )
156
156
. filter_map ( |( k, v) | {
157
157
if let ValidationErrorsKind :: Field ( errors) = v {
158
- Some ( ( * k , errors) )
158
+ Some ( ( k . clone ( ) , errors) )
159
159
} else {
160
160
None
161
161
}
@@ -164,8 +164,10 @@ impl ValidationErrors {
164
164
}
165
165
166
166
pub fn add ( & mut self , field : & ' static str , error : ValidationError ) {
167
- if let ValidationErrorsKind :: Field ( ref mut vec) =
168
- self . 0 . entry ( field) . or_insert_with ( || ValidationErrorsKind :: Field ( vec ! [ ] ) )
167
+ if let ValidationErrorsKind :: Field ( ref mut vec) = self
168
+ . 0
169
+ . entry ( Cow :: Borrowed ( field) )
170
+ . or_insert_with ( || ValidationErrorsKind :: Field ( vec ! [ ] ) )
169
171
{
170
172
vec. push ( error) ;
171
173
} else {
@@ -179,7 +181,7 @@ impl ValidationErrors {
179
181
}
180
182
181
183
fn add_nested ( & mut self , field : & ' static str , errors : ValidationErrorsKind ) {
182
- if let Vacant ( entry) = self . 0 . entry ( field) {
184
+ if let Vacant ( entry) = self . 0 . entry ( Cow :: Borrowed ( field) ) {
183
185
entry. insert ( errors) ;
184
186
} else {
185
187
panic ! ( "Attempt to replace non-empty ValidationErrors entry" ) ;
0 commit comments