Skip to content

Commit fbcba49

Browse files
authored
Merge pull request #38 from yassun7010/fix_custom_message
Fix custom message
2 parents ee6b7ce + 5c6c882 commit fbcba49

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1172
-811
lines changed

serde_valid/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ once_cell = "^1.7"
2121
paste = { workspace = true }
2222
regex = { workspace = true }
2323
serde = { workspace = true, features = ["derive"] }
24-
serde_json = { workspace = true }
24+
serde_json = { workspace = true, optional = true }
2525
serde_toml = { package = "toml", version = "^0.8", optional = true }
2626
serde_valid_derive = { version = "0.16.3", path = "../serde_valid_derive" }
2727
serde_valid_literal = { version = "0.16.3", path = "../serde_valid_literal" }
@@ -33,7 +33,8 @@ unicode-segmentation = "^1.7"
3333
unic-langid = "0.9.1"
3434

3535
[features]
36-
default = ["i128"]
36+
default = ["i128", "json"]
37+
json = ["serde_json"]
3738
toml = ["serde_toml"]
3839
yaml = ["serde_yaml"]
3940
i128 = ["num-traits/i128", "indexmap/std", "serde_valid_literal/i128"]

serde_valid/expands/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

serde_valid/src/error.rs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use itertools::Itertools;
22
use serde_valid_literal::Literal;
33

4+
use crate::validation::error::FormatDefault;
45
use crate::validation::Number;
5-
use crate::validation::ToDefaultMessage;
66

77
#[derive(Debug, thiserror::Error)]
88
pub enum Error<E>
@@ -62,12 +62,23 @@ macro_rules! struct_error_params {
6262
pub $limit: Vec<$type>,
6363
}
6464

65-
impl ToDefaultMessage for $Error {
65+
impl $Error {
66+
pub fn new<T>($limit: &[T]) -> Self
67+
where
68+
T: Into<$type> + std::fmt::Debug + Clone,
69+
{
70+
Self {
71+
$limit: (*$limit).iter().map(|x| x.clone().into()).collect(),
72+
}
73+
}
74+
}
75+
76+
impl FormatDefault for $Error {
6677
#[inline]
67-
fn to_default_message(&self) -> String {
78+
fn format_default(&self) -> String {
6879
format!(
6980
$default_message,
70-
self.enumerate.iter().map(|v| format!("{}", v)).join(", ")
81+
self.$limit.iter().map(|v| format!("{}", v)).join(", ")
7182
)
7283
}
7384
}
@@ -93,9 +104,9 @@ macro_rules! struct_error_params {
93104
}
94105
}
95106

96-
impl ToDefaultMessage for $Error {
107+
impl FormatDefault for $Error {
97108
#[inline]
98-
fn to_default_message(&self) -> String {
109+
fn format_default(&self) -> String {
99110
format!($default_message, self.$limit)
100111
}
101112
}
@@ -104,15 +115,14 @@ macro_rules! struct_error_params {
104115
(
105116
#[derive(Debug, Clone)]
106117
#[default_message=$default_message:literal]
107-
pub struct $Error:ident {
108-
}
118+
pub struct $Error:ident;
109119
) => {
110120
#[derive(Debug, Clone)]
111-
pub struct $Error {}
121+
pub struct $Error;
112122

113-
impl ToDefaultMessage for $Error {
123+
impl FormatDefault for $Error {
114124
#[inline]
115-
fn to_default_message(&self) -> String {
125+
fn format_default(&self) -> String {
116126
format!($default_message)
117127
}
118128
}
@@ -205,7 +215,7 @@ struct_error_params!(
205215
struct_error_params!(
206216
#[derive(Debug, Clone)]
207217
#[default_message = "The items must be unique."]
208-
pub struct UniqueItemsError {}
218+
pub struct UniqueItemsError;
209219
);
210220

211221
// Object
@@ -233,14 +243,3 @@ struct_error_params!(
233243
pub enumerate: Vec<Literal>,
234244
}
235245
);
236-
237-
impl EnumerateError {
238-
pub fn new<T>(enumerate: &[T]) -> Self
239-
where
240-
T: Into<Literal> + std::fmt::Debug + Clone,
241-
{
242-
Self {
243-
enumerate: (*enumerate).iter().map(|x| x.clone().into()).collect(),
244-
}
245-
}
246-
}

serde_valid/src/features.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
#[cfg(feature = "flatten")]
22
pub mod flatten;
3+
34
#[cfg(feature = "fluent")]
45
pub mod fluent;
6+
7+
#[cfg(feature = "json")]
8+
pub mod json;
9+
510
#[cfg(feature = "toml")]
611
pub mod toml;
12+
713
#[cfg(feature = "yaml")]
814
pub mod yaml;

serde_valid/src/features/flatten/flat_errors.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ mod tests {
5151

5252
use crate::{
5353
flatten::IntoFlat,
54-
validation::{ArrayErrors, Error, Errors, Message, ToDefaultMessage},
54+
validation::error::{ArrayErrors, Message},
55+
validation::{Error, Errors},
5556
MinItemsError,
5657
};
5758

@@ -62,7 +63,7 @@ mod tests {
6263
Errors::Array(ArrayErrors {
6364
errors: vec![Error::MinItems(Message::new(
6465
MinItemsError { min_items: 1 },
65-
MinItemsError::to_default_message
66+
crate::validation::error::Format::Default
6667
))],
6768
items: indexmap! {},
6869
})

serde_valid/src/features/flatten/into_flat.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use jsonschema::paths::{JSONPointer, PathChunk};
22

3-
use crate::validation::{
4-
ArrayErrors, ItemErrorsMap, Message, ObjectErrors, PropertyErrorsMap, ToDefaultMessage,
3+
use crate::validation::error::{
4+
ArrayErrors, FormatDefault, ItemErrorsMap, Message, ObjectErrors, PropertyErrorsMap,
55
};
66

77
use super::{FlatError, FlatErrors};
@@ -109,10 +109,10 @@ where
109109

110110
impl<T> IntoFlat for Message<T>
111111
where
112-
T: ToDefaultMessage,
112+
T: FormatDefault,
113113
{
114114
fn into_flat_at(self, path: &JSONPointer) -> FlatErrors {
115-
FlatErrors::new(vec![FlatError::new(path.to_owned(), self.error())])
115+
FlatErrors::new(vec![FlatError::new(path.to_owned(), self.to_string())])
116116
}
117117
}
118118

@@ -168,13 +168,13 @@ mod tests {
168168
fn array_errors_flatten() {
169169
let min_items = Message::new(
170170
MinItemsError { min_items: 1 },
171-
MinItemsError::to_default_message,
171+
crate::validation::error::Format::Default,
172172
);
173173
let maximum = Message::new(
174174
MaximumError {
175175
maximum: Number::I32(1),
176176
},
177-
MaximumError::to_default_message,
177+
crate::validation::error::Format::Default,
178178
);
179179
assert_eq!(
180180
Errors::Array(ArrayErrors {
@@ -203,29 +203,29 @@ mod tests {
203203
FlatErrors::new(vec![
204204
FlatError::new(
205205
JSONPointer::default(),
206-
min_items.error().to_default_message(),
206+
min_items.format_default(),
207207
),
208208
FlatError::new(
209209
JSONPointer::from([PathChunk::from(0)].as_ref()),
210-
maximum.error().to_default_message(),
210+
maximum.format_default(),
211211
),
212212
FlatError::new(
213213
JSONPointer::from([PathChunk::from(0), PathChunk::from(2)].as_ref()),
214-
maximum.error().to_default_message(),
214+
maximum.format_default(),
215215
),
216216
FlatError::new(
217217
JSONPointer::from([PathChunk::from(3)].as_ref()),
218-
maximum.error().to_default_message(),
218+
maximum.format_default(),
219219
),
220220
FlatError::new(
221221
JSONPointer::from([PathChunk::from(5)].as_ref()),
222-
maximum.error().to_default_message(),
222+
maximum.format_default(),
223223
),
224224
FlatError::new(
225225
JSONPointer::from(
226226
[PathChunk::from(5), PathChunk::from("name".to_owned())].as_ref()
227227
),
228-
maximum.error().to_default_message(),
228+
maximum.format_default(),
229229
)
230230
])
231231
);

serde_valid/src/features/fluent.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
mod into_localization;
1+
mod error;
2+
mod localize;
23
mod message;
4+
mod try_localize;
35

4-
pub use into_localization::IntoLocalization;
6+
pub use error::LocalizedError;
7+
pub use localize::Localize;
58
pub use message::Message;
9+
pub use try_localize::TryLocalize;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use crate::validation::{ArrayErrors, ObjectErrors};
2+
3+
#[derive(Debug, Clone, serde::Serialize)]
4+
#[serde(untagged)]
5+
pub enum LocalizedError {
6+
String(String),
7+
Items(ArrayErrors<LocalizedError>),
8+
Properties(ObjectErrors<LocalizedError>),
9+
}

0 commit comments

Comments
 (0)