Skip to content

Commit ccb8416

Browse files
feat: implement error for validation errors (#38)
1 parent 5c9c906 commit ccb8416

6 files changed

Lines changed: 60 additions & 2 deletions

File tree

packages/fortifier/src/validations/email_address.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::{
22
borrow::Cow,
33
cell::{Ref, RefMut},
4+
error::Error,
5+
fmt,
46
rc::Rc,
57
sync::Arc,
68
};
@@ -197,6 +199,14 @@ pub enum EmailAddressError {
197199
},
198200
}
199201

202+
impl fmt::Display for EmailAddressError {
203+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
204+
write!(f, "{self:#?}")
205+
}
206+
}
207+
208+
impl Error for EmailAddressError {}
209+
200210
impl From<email_address::Error> for EmailAddressError {
201211
fn from(value: email_address::Error) -> Self {
202212
let code = EmailAddressErrorCode;

packages/fortifier/src/validations/length.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use std::{
22
borrow::Cow,
33
cell::{Ref, RefMut},
44
collections::{BTreeMap, BTreeSet, HashMap, HashSet, LinkedList, VecDeque},
5-
fmt::Display,
5+
error::Error,
6+
fmt::{self, Debug, Display},
67
rc::Rc,
78
sync::Arc,
89
};
@@ -77,6 +78,14 @@ pub enum LengthError<T> {
7778
},
7879
}
7980

81+
impl<T: Debug> fmt::Display for LengthError<T> {
82+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83+
write!(f, "{self:#?}")
84+
}
85+
}
86+
87+
impl<T: Debug> Error for LengthError<T> {}
88+
8089
/// Validate a length.
8190
pub trait ValidateLength<T>
8291
where

packages/fortifier/src/validations/phone_number.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::{
22
borrow::Cow,
33
cell::{Ref, RefMut},
4+
error::Error,
5+
fmt,
46
rc::Rc,
57
sync::Arc,
68
};
@@ -107,6 +109,14 @@ pub enum PhoneNumberError {
107109
},
108110
}
109111

112+
impl fmt::Display for PhoneNumberError {
113+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114+
write!(f, "{self:#?}")
115+
}
116+
}
117+
118+
impl Error for PhoneNumberError {}
119+
110120
impl From<ParseError> for PhoneNumberError {
111121
fn from(value: ParseError) -> Self {
112122
let code = PhoneNumberErrorCode;

packages/fortifier/src/validations/range.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{
22
cell::{Ref, RefMut},
3-
fmt::Display,
3+
error::Error,
4+
fmt::{self, Debug, Display},
45
rc::Rc,
56
sync::Arc,
67
};
@@ -88,6 +89,14 @@ pub enum RangeError<T> {
8889
},
8990
}
9091

92+
impl<T: Debug> fmt::Display for RangeError<T> {
93+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94+
write!(f, "{self:#?}")
95+
}
96+
}
97+
98+
impl<T: Debug> Error for RangeError<T> {}
99+
91100
/// Validate a range.
92101
pub trait ValidateRange<T>
93102
where

packages/fortifier/src/validations/regex.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::{
22
borrow::Cow,
33
cell::{Ref, RefMut},
4+
error::Error,
5+
fmt,
46
rc::Rc,
57
sync::{Arc, LazyLock},
68
};
@@ -67,6 +69,14 @@ impl Default for RegexError {
6769
}
6870
}
6971

72+
impl fmt::Display for RegexError {
73+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74+
write!(f, "{self:#?}")
75+
}
76+
}
77+
78+
impl Error for RegexError {}
79+
7080
/// Validate a regular expression.
7181
pub trait ValidateRegex {
7282
/// Validate regular expression.

packages/fortifier/src/validations/url.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::{
22
borrow::Cow,
33
cell::{Ref, RefMut},
4+
error::Error,
5+
fmt,
46
rc::Rc,
57
sync::Arc,
68
};
@@ -136,6 +138,14 @@ pub enum UrlError {
136138
},
137139
}
138140

141+
impl fmt::Display for UrlError {
142+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
143+
write!(f, "{self:#?}")
144+
}
145+
}
146+
147+
impl Error for UrlError {}
148+
139149
impl From<ParseError> for UrlError {
140150
fn from(value: ParseError) -> Self {
141151
let code = UrlErrorCode;

0 commit comments

Comments
 (0)