|
1 | | -use crate::protocol::{IpAddr, JsonLenientString}; |
| 1 | +use crate::protocol::{IpAddr, LenientString}; |
2 | 2 | use crate::types::{Annotated, Object, Value}; |
3 | 3 |
|
4 | 4 | /// Geographical location of the end user or device. |
@@ -28,7 +28,7 @@ pub struct Geo { |
28 | 28 | pub struct User { |
29 | 29 | /// Unique identifier of the user. |
30 | 30 | #[metastructure(pii = "true", max_chars = "enumlike")] |
31 | | - pub id: Annotated<JsonLenientString>, |
| 31 | + pub id: Annotated<LenientString>, |
32 | 32 |
|
33 | 33 | /// Email address of the user. |
34 | 34 | #[metastructure(pii = "true", max_chars = "email", match_regex = r"@")] |
@@ -143,6 +143,34 @@ fn test_user_roundtrip() { |
143 | 143 | assert_eq_str!(json, user.to_json_pretty().unwrap()); |
144 | 144 | } |
145 | 145 |
|
| 146 | +#[test] |
| 147 | +fn test_user_lenient_id() { |
| 148 | + let input = r#"{"id":42}"#; |
| 149 | + let output = r#"{"id":"42"}"#; |
| 150 | + let user = Annotated::new(User { |
| 151 | + id: Annotated::new("42".to_string().into()), |
| 152 | + ..User::default() |
| 153 | + }); |
| 154 | + |
| 155 | + assert_eq_dbg!(user, Annotated::from_json(input).unwrap()); |
| 156 | + assert_eq_str!(output, user.to_json().unwrap()); |
| 157 | +} |
| 158 | + |
| 159 | +#[test] |
| 160 | +fn test_user_invalid_id() { |
| 161 | + use crate::types::Error; |
| 162 | + let json = r#"{"id":[]}"#; |
| 163 | + let user = Annotated::new(User { |
| 164 | + id: Annotated::from_error( |
| 165 | + Error::expected("primitive value"), |
| 166 | + Some(Value::Array(vec![])), |
| 167 | + ), |
| 168 | + ..User::default() |
| 169 | + }); |
| 170 | + |
| 171 | + assert_eq_dbg!(user, Annotated::from_json(json).unwrap()); |
| 172 | +} |
| 173 | + |
146 | 174 | #[test] |
147 | 175 | fn test_explicit_none() { |
148 | 176 | let json = r#"{ |
|
0 commit comments