@@ -38,16 +38,16 @@ impl std::error::Error for ValidationError {
3838 }
3939}
4040
41- #[ derive( Debug , Serialize , Clone , PartialEq ) ]
41+ #[ derive( Debug , Serialize , Deserialize , Clone , PartialEq ) ]
4242#[ serde( untagged) ]
4343pub enum ValidationErrorsKind {
4444 Struct ( Box < ValidationErrors > ) ,
4545 List ( BTreeMap < usize , Box < ValidationErrors > > ) ,
4646 Field ( Vec < ValidationError > ) ,
4747}
4848
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 > ) ;
5151
5252impl ValidationErrors {
5353 pub fn new ( ) -> ValidationErrors {
@@ -134,28 +134,28 @@ impl ValidationErrors {
134134
135135 /// Returns a map of field-level validation errors found for the struct that was validated and
136136 /// 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 > {
138138 & self . 0
139139 }
140140
141141 /// Returns a mutable map of field-level validation errors found for the struct that was validated and
142142 /// 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 > {
144144 & mut self . 0
145145 }
146146
147147 /// 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 > {
149149 self . 0
150150 }
151151
152152 /// 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 > > {
154154 self . 0
155155 . iter ( )
156156 . filter_map ( |( k, v) | {
157157 if let ValidationErrorsKind :: Field ( errors) = v {
158- Some ( ( * k , errors) )
158+ Some ( ( k . clone ( ) , errors) )
159159 } else {
160160 None
161161 }
@@ -164,8 +164,10 @@ impl ValidationErrors {
164164 }
165165
166166 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 ! [ ] ) )
169171 {
170172 vec. push ( error) ;
171173 } else {
@@ -179,7 +181,7 @@ impl ValidationErrors {
179181 }
180182
181183 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) ) {
183185 entry. insert ( errors) ;
184186 } else {
185187 panic ! ( "Attempt to replace non-empty ValidationErrors entry" ) ;
0 commit comments