1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use super::message::ToDefaultMessage;
use crate::validation::{Number, Pattern};

macro_rules! struct_error_params {
    (
        #[derive(Debug, Clone)]
        #[default_message=$default_message:literal]
        pub struct $ErrorParams:ident {
            pub $limit:ident: $type:ty,
        }
    ) => {
        #[derive(Debug, Clone)]
        pub struct $ErrorParams {
            pub $limit: $type,
        }

        impl $ErrorParams {
            pub fn new<N: Into<$type>>($limit: N) -> Self {
                Self {
                    $limit: $limit.into(),
                }
            }
        }

        impl ToDefaultMessage for $ErrorParams {
            fn to_default_message(&self) -> String {
                format!($default_message, self.$limit)
            }
        }
    };

    (
        #[derive(Debug, Clone)]
        #[default_message=$default_message:literal]
        pub struct $ErrorParams:ident {
        }
    ) => {
        #[derive(Debug, Clone)]
        pub struct $ErrorParams {}

        impl ToDefaultMessage for $ErrorParams {
            fn to_default_message(&self) -> String {
                format!($default_message)
            }
        }
    };
}

// Number
struct_error_params!(
    #[derive(Debug, Clone)]
    #[default_message = "the number must be `>= {}`."]
    pub struct MinimumErrorParams {
        pub minimum: Number,
    }
);

struct_error_params!(
    #[derive(Debug, Clone)]
    #[default_message = "the number must be `<= {}`."]
    pub struct MaximumErrorParams {
        pub maximum: Number,
    }
);

struct_error_params!(
    #[derive(Debug, Clone)]
    #[default_message = "the number must be `> {}`."]
    pub struct ExclusiveMinimumErrorParams {
        pub exclusive_minimum: Number,
    }
);

struct_error_params!(
    #[derive(Debug, Clone)]
    #[default_message = "the number must be `< {}`."]
    pub struct ExclusiveMaximumErrorParams {
        pub exclusive_maximum: Number,
    }
);

struct_error_params!(
    #[derive(Debug, Clone)]
    #[default_message = "the value must be multiple of `{}`."]
    pub struct MultipleOfErrorParams {
        pub multiple_of: Number,
    }
);

// String
struct_error_params!(
    #[derive(Debug, Clone)]
    #[default_message = "the length of the value must be `>= {}`."]
    pub struct MinLengthErrorParams {
        pub min_length: usize,
    }
);

struct_error_params!(
    #[derive(Debug, Clone)]
    #[default_message = "the length of the value must be `<= {}`."]
    pub struct MaxLengthErrorParams {
        pub max_length: usize,
    }
);

struct_error_params!(
    #[derive(Debug, Clone)]
    #[default_message = "the value must match the pattern of \"{}\"."]
    pub struct PatternErrorParams {
        pub pattern: Pattern,
    }
);

// Array
struct_error_params!(
    #[derive(Debug, Clone)]
    #[default_message = "the length of the items must be `<= {}`."]
    pub struct MaxItemsErrorParams {
        pub max_items: usize,
    }
);

struct_error_params!(
    #[derive(Debug, Clone)]
    #[default_message = "the length of the items must be `>= {}`."]
    pub struct MinItemsErrorParams {
        pub min_items: usize,
    }
);

struct_error_params!(
    #[derive(Debug, Clone)]
    #[default_message = "items must be unique."]
    pub struct UniqueItemsErrorParams {}
);

// Object
struct_error_params!(
    #[derive(Debug, Clone)]
    #[default_message = "the size of the properties must be `<= {}`."]
    pub struct MaxPropertiesErrorParams {
        pub max_properties: usize,
    }
);

struct_error_params!(
    #[derive(Debug, Clone)]
    #[default_message = "the size of the properties must be `>= {}`."]
    pub struct MinPropertiesErrorParams {
        pub min_properties: usize,
    }
);