Skip to content

Commit

Permalink
Merge pull request #38 from yassun7010/fix_custom_message
Browse files Browse the repository at this point in the history
Fix custom message
  • Loading branch information
yassun7010 authored Jan 13, 2024
2 parents ee6b7ce + 5c6c882 commit fbcba49
Show file tree
Hide file tree
Showing 60 changed files with 1,172 additions and 811 deletions.
5 changes: 3 additions & 2 deletions serde_valid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ once_cell = "^1.7"
paste = { workspace = true }
regex = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
serde_json = { workspace = true, optional = true }
serde_toml = { package = "toml", version = "^0.8", optional = true }
serde_valid_derive = { version = "0.16.3", path = "../serde_valid_derive" }
serde_valid_literal = { version = "0.16.3", path = "../serde_valid_literal" }
Expand All @@ -33,7 +33,8 @@ unicode-segmentation = "^1.7"
unic-langid = "0.9.1"

[features]
default = ["i128"]
default = ["i128", "json"]
json = ["serde_json"]
toml = ["serde_toml"]
yaml = ["serde_yaml"]
i128 = ["num-traits/i128", "indexmap/std", "serde_valid_literal/i128"]
Expand Down
2 changes: 2 additions & 0 deletions serde_valid/expands/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
45 changes: 22 additions & 23 deletions serde_valid/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use itertools::Itertools;
use serde_valid_literal::Literal;

use crate::validation::error::FormatDefault;
use crate::validation::Number;
use crate::validation::ToDefaultMessage;

#[derive(Debug, thiserror::Error)]
pub enum Error<E>
Expand Down Expand Up @@ -62,12 +62,23 @@ macro_rules! struct_error_params {
pub $limit: Vec<$type>,
}

impl ToDefaultMessage for $Error {
impl $Error {
pub fn new<T>($limit: &[T]) -> Self
where
T: Into<$type> + std::fmt::Debug + Clone,
{
Self {
$limit: (*$limit).iter().map(|x| x.clone().into()).collect(),
}
}
}

impl FormatDefault for $Error {
#[inline]
fn to_default_message(&self) -> String {
fn format_default(&self) -> String {
format!(
$default_message,
self.enumerate.iter().map(|v| format!("{}", v)).join(", ")
self.$limit.iter().map(|v| format!("{}", v)).join(", ")
)
}
}
Expand All @@ -93,9 +104,9 @@ macro_rules! struct_error_params {
}
}

impl ToDefaultMessage for $Error {
impl FormatDefault for $Error {
#[inline]
fn to_default_message(&self) -> String {
fn format_default(&self) -> String {
format!($default_message, self.$limit)
}
}
Expand All @@ -104,15 +115,14 @@ macro_rules! struct_error_params {
(
#[derive(Debug, Clone)]
#[default_message=$default_message:literal]
pub struct $Error:ident {
}
pub struct $Error:ident;
) => {
#[derive(Debug, Clone)]
pub struct $Error {}
pub struct $Error;

impl ToDefaultMessage for $Error {
impl FormatDefault for $Error {
#[inline]
fn to_default_message(&self) -> String {
fn format_default(&self) -> String {
format!($default_message)
}
}
Expand Down Expand Up @@ -205,7 +215,7 @@ struct_error_params!(
struct_error_params!(
#[derive(Debug, Clone)]
#[default_message = "The items must be unique."]
pub struct UniqueItemsError {}
pub struct UniqueItemsError;
);

// Object
Expand Down Expand Up @@ -233,14 +243,3 @@ struct_error_params!(
pub enumerate: Vec<Literal>,
}
);

impl EnumerateError {
pub fn new<T>(enumerate: &[T]) -> Self
where
T: Into<Literal> + std::fmt::Debug + Clone,
{
Self {
enumerate: (*enumerate).iter().map(|x| x.clone().into()).collect(),
}
}
}
6 changes: 6 additions & 0 deletions serde_valid/src/features.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#[cfg(feature = "flatten")]
pub mod flatten;

#[cfg(feature = "fluent")]
pub mod fluent;

#[cfg(feature = "json")]
pub mod json;

#[cfg(feature = "toml")]
pub mod toml;

#[cfg(feature = "yaml")]
pub mod yaml;
5 changes: 3 additions & 2 deletions serde_valid/src/features/flatten/flat_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ mod tests {

use crate::{
flatten::IntoFlat,
validation::{ArrayErrors, Error, Errors, Message, ToDefaultMessage},
validation::error::{ArrayErrors, Message},
validation::{Error, Errors},
MinItemsError,
};

Expand All @@ -62,7 +63,7 @@ mod tests {
Errors::Array(ArrayErrors {
errors: vec![Error::MinItems(Message::new(
MinItemsError { min_items: 1 },
MinItemsError::to_default_message
crate::validation::error::Format::Default
))],
items: indexmap! {},
})
Expand Down
24 changes: 12 additions & 12 deletions serde_valid/src/features/flatten/into_flat.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use jsonschema::paths::{JSONPointer, PathChunk};

use crate::validation::{
ArrayErrors, ItemErrorsMap, Message, ObjectErrors, PropertyErrorsMap, ToDefaultMessage,
use crate::validation::error::{
ArrayErrors, FormatDefault, ItemErrorsMap, Message, ObjectErrors, PropertyErrorsMap,
};

use super::{FlatError, FlatErrors};
Expand Down Expand Up @@ -109,10 +109,10 @@ where

impl<T> IntoFlat for Message<T>
where
T: ToDefaultMessage,
T: FormatDefault,
{
fn into_flat_at(self, path: &JSONPointer) -> FlatErrors {
FlatErrors::new(vec![FlatError::new(path.to_owned(), self.error())])
FlatErrors::new(vec![FlatError::new(path.to_owned(), self.to_string())])
}
}

Expand Down Expand Up @@ -168,13 +168,13 @@ mod tests {
fn array_errors_flatten() {
let min_items = Message::new(
MinItemsError { min_items: 1 },
MinItemsError::to_default_message,
crate::validation::error::Format::Default,
);
let maximum = Message::new(
MaximumError {
maximum: Number::I32(1),
},
MaximumError::to_default_message,
crate::validation::error::Format::Default,
);
assert_eq!(
Errors::Array(ArrayErrors {
Expand Down Expand Up @@ -203,29 +203,29 @@ mod tests {
FlatErrors::new(vec![
FlatError::new(
JSONPointer::default(),
min_items.error().to_default_message(),
min_items.format_default(),
),
FlatError::new(
JSONPointer::from([PathChunk::from(0)].as_ref()),
maximum.error().to_default_message(),
maximum.format_default(),
),
FlatError::new(
JSONPointer::from([PathChunk::from(0), PathChunk::from(2)].as_ref()),
maximum.error().to_default_message(),
maximum.format_default(),
),
FlatError::new(
JSONPointer::from([PathChunk::from(3)].as_ref()),
maximum.error().to_default_message(),
maximum.format_default(),
),
FlatError::new(
JSONPointer::from([PathChunk::from(5)].as_ref()),
maximum.error().to_default_message(),
maximum.format_default(),
),
FlatError::new(
JSONPointer::from(
[PathChunk::from(5), PathChunk::from("name".to_owned())].as_ref()
),
maximum.error().to_default_message(),
maximum.format_default(),
)
])
);
Expand Down
8 changes: 6 additions & 2 deletions serde_valid/src/features/fluent.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
mod into_localization;
mod error;
mod localize;
mod message;
mod try_localize;

pub use into_localization::IntoLocalization;
pub use error::LocalizedError;
pub use localize::Localize;
pub use message::Message;
pub use try_localize::TryLocalize;
9 changes: 9 additions & 0 deletions serde_valid/src/features/fluent/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use crate::validation::{ArrayErrors, ObjectErrors};

#[derive(Debug, Clone, serde::Serialize)]
#[serde(untagged)]
pub enum LocalizedError {
String(String),
Items(ArrayErrors<LocalizedError>),
Properties(ObjectErrors<LocalizedError>),
}
Loading

0 comments on commit fbcba49

Please sign in to comment.