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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#![recursion_limit = "128"]


#[macro_use]
extern crate quote;
extern crate proc_macro;
#[macro_use]
extern crate syn;
#[macro_use]
extern crate if_chain;
extern crate validator;

use std::collections::HashMap;

use proc_macro::TokenStream;
use quote::ToTokens;

use validator::Validator;


mod lit;
mod validation;
mod asserts;
mod quoting;

use lit::*;
use validation::*;
use asserts::{assert_string_type, assert_type_matches, assert_has_len, assert_has_range};
use quoting::{FieldQuoter, quote_field_validation, quote_schema_validation};


#[proc_macro_derive(Validate, attributes(validate))]
pub fn derive_validation(input: TokenStream) -> TokenStream {
    let ast = syn::parse(input).unwrap();

    let expanded = impl_validate(&ast);
    expanded.into()
}


fn impl_validate(ast: &syn::DeriveInput) -> quote::Tokens {
    // Ensure the macro is on a struct with named fields
    let fields = match ast.data {
        syn::Data::Struct(syn::DataStruct { ref fields, .. }) => {
            if fields.iter().any(|field| field.ident.is_none()) {
                panic!("struct has unnamed fields");
            }
            fields.iter().cloned().collect()
        },
        _ => panic!("#[derive(Validate)] can only be used with structs"),
    };

    let mut validations = vec![];

    let field_types = find_fields_type(&fields);

    for field in &fields {
        let field_ident = field.ident.clone().unwrap();
        let (name, field_validations) = find_validators_for_field(field, &field_types);
        let field_type = field_types.get(&field_ident.to_string()).cloned().unwrap();
        let field_quoter = FieldQuoter::new(field_ident, name, field_type);

        for validation in &field_validations {
            validations.push(quote_field_validation(&field_quoter, validation));
        }
    }

    let schema_validation = quote_schema_validation(find_struct_validation(&ast.attrs));

    let ident = &ast.ident;

    // Helper is provided for handling complex generic types correctly and effortlessly
    let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
    let impl_ast = quote!(
        impl #impl_generics Validate for #ident #ty_generics #where_clause {
            #[allow(unused_mut)]
            fn validate(&self) -> ::std::result::Result<(), ::validator::ValidationErrors> {
                let mut errors = ::validator::ValidationErrors::new();

                #(#validations)*

                #schema_validation

                if errors.is_empty() {
                    ::std::result::Result::Ok(())
                } else {
                    ::std::result::Result::Err(errors)
                }
            }
        }
    );
    // println!("{}", impl_ast.to_string());
    impl_ast
}


/// Find if a struct has some schema validation and returns the info if so
fn find_struct_validation(struct_attrs: &Vec<syn::Attribute>) -> Option<SchemaValidation> {
    let error = |msg: &str| -> ! {
        panic!("Invalid schema level validation: {}", msg);
    };

    for attr in struct_attrs {
        if attr.path != parse_quote!(validate) {
            continue;
        }

        if_chain! {
            if let Some(syn::Meta::List(syn::MetaList { ref nested, .. })) = attr.interpret_meta();
            if let syn::NestedMeta::Meta(ref item) = nested[0];
            if let &syn::Meta::List(syn::MetaList { ref ident, ref nested, .. }) = item;

            then {
                if ident.as_ref() != "schema" {
                    error("Only `schema` is allowed as validator on a struct")
                }

                let mut function = String::new();
                let mut skip_on_field_errors = true;
                let mut code = None;
                let mut message = None;

                for arg in nested {
                    if_chain! {
                        if let syn::NestedMeta::Meta(ref item) = *arg;
                        if let syn::Meta::NameValue(syn::MetaNameValue { ref ident, ref lit, .. }) = *item;

                        then {
                            match ident.as_ref() {
                                "function" => {
                                    function = match lit_to_string(lit) {
                                        Some(s) => s,
                                        None => error("invalid argument type for `function` \
                                        : only a string is allowed"),
                                    };
                                },
                                "skip_on_field_errors" => {
                                    skip_on_field_errors = match lit_to_bool(lit) {
                                        Some(s) => s,
                                        None => error("invalid argument type for `skip_on_field_errors` \
                                        : only a bool is allowed"),
                                    };
                                },
                                "code" => {
                                    code = match lit_to_string(lit) {
                                        Some(s) => Some(s),
                                        None => error("invalid argument type for `code` \
                                        : only a string is allowed"),
                                    };
                                },
                                "message" => {
                                    message = match lit_to_string(lit) {
                                        Some(s) => Some(s),
                                        None => error("invalid argument type for `message` \
                                        : only a string is allowed"),
                                    };
                                },
                                _ => error("Unknown argument")
                            }
                        } else {
                            error("Unexpected args")
                        }
                    }
                }

                if function == "" {
                    error("`function` is required");
                }

                return Some(
                    SchemaValidation {
                        function,
                        skip_on_field_errors,
                        code,
                        message,
                    }
                );
            } else {
                error("Unexpected struct validator")
            }
        }
    }

    None
}


/// Find the types (as string) for each field of the struct
/// Needed for the `must_match` filter
fn find_fields_type(fields: &Vec<syn::Field>) -> HashMap<String, String> {
    let mut types = HashMap::new();

    for field in fields {
        let field_ident = field.ident.clone().unwrap().to_string();
        let field_type = match field.ty {
            syn::Type::Path(syn::TypePath { ref path, .. }) => {
                let mut tokens = quote::Tokens::new();
                path.to_tokens(&mut tokens);
                tokens.to_string().replace(' ', "")

            },
            syn::Type::Reference(syn::TypeReference { ref lifetime, ref elem, .. }) => {
                let mut tokens = quote::Tokens::new();
                elem.to_tokens(&mut tokens);
                let mut name = tokens.to_string().replace(' ', "");
                if lifetime.is_some() {
                    name.insert(0, '&')
                }
                name
            },
            _ => panic!("Type `{:?}` of field `{}` not supported", field.ty, field_ident)
        };

        //println!("{:?}", field_type);
        types.insert(field_ident, field_type);
    }

    types
}

/// Find everything we need to know about a field: its real name if it's changed from the serialization
/// and the list of validators to run on it
fn find_validators_for_field(field: &syn::Field, field_types: &HashMap<String, String>) -> (String, Vec<FieldValidation>) {
    let rust_ident = field.ident.clone().unwrap().to_string();
    let mut field_ident = field.ident.clone().unwrap().to_string();

    let error = |msg: &str| -> ! {
        panic!("Invalid attribute #[validate] on field `{}`: {}", field.ident.clone().unwrap().to_string(), msg);
    };

    let field_type = field_types.get(&field_ident).unwrap();

    let mut validators = vec![];
    let mut has_validate = false;

    for attr in &field.attrs {
        if attr.path != parse_quote!(validate) && attr.path != parse_quote!(serde) {
            continue;
        }

        if attr.path == parse_quote!(validate) {
            has_validate = true;
        }

        match attr.interpret_meta() {
            Some(syn::Meta::List(syn::MetaList { ref nested, .. })) => {
                let meta_items = nested.iter().collect();
                // original name before serde rename
                if attr.path == parse_quote!(serde) {
                    if let Some(s) = find_original_field_name(&meta_items) {
                        field_ident = s;
                    }
                    continue;
                }

                // only validation from there on
                for meta_item in meta_items {
                    match *meta_item {
                        syn::NestedMeta::Meta(ref item) => match *item {
                            // email, url, phone
                            syn::Meta::Word(ref name) => match name.to_string().as_ref() {
                                "email" => {
                                    assert_string_type("email", field_type);
                                    validators.push(FieldValidation::new(Validator::Email));
                                },
                                "url" => {
                                    assert_string_type("url", field_type);
                                    validators.push(FieldValidation::new(Validator::Url));
                                },
                                #[cfg(feature = "phone")]
                                "phone" => {
                                    assert_string_type("phone", field_type);
                                    validators.push(FieldValidation::new(Validator::Phone));
                                },
                                "credit_card" => {
                                    assert_string_type("credit_card", field_type);
                                    validators.push(FieldValidation::new(Validator::CreditCard));
                                },
                                _ => panic!("Unexpected validator: {}", name)
                            },
                            // custom, contains, must_match, regex
                            syn::Meta::NameValue(syn::MetaNameValue { ref ident, ref lit, .. }) => {
                                match ident.as_ref() {
                                    "custom" => {
                                        match lit_to_string(lit) {
                                            Some(s) => validators.push(FieldValidation::new(Validator::Custom(s))),
                                            None => error("invalid argument for `custom` validator: only strings are allowed"),
                                        };
                                    },
                                    "contains" => {
                                        match lit_to_string(lit) {
                                            Some(s) => validators.push(FieldValidation::new(Validator::Contains(s))),
                                            None => error("invalid argument for `contains` validator: only strings are allowed"),
                                        };
                                    },
                                    "regex" => {
                                        match lit_to_string(lit) {
                                            Some(s) => validators.push(FieldValidation::new(Validator::Regex(s))),
                                            None => error("invalid argument for `regex` validator: only strings are allowed"),
                                        };
                                    }
                                    "must_match" => {
                                        match lit_to_string(lit) {
                                            Some(s) => {
                                                assert_type_matches(rust_ident.clone(), field_type, field_types.get(&s));
                                                validators.push(FieldValidation::new(Validator::MustMatch(s)));
                                            },
                                            None => error("invalid argument for `must_match` validator: only strings are allowed"),
                                        };
                                    },
                                    v => panic!("unexpected name value validator: {:?}", v),
                                };
                            },
                            // Validators with several args
                            syn::Meta::List(syn::MetaList { ref ident, ref nested, .. }) => {
                                let meta_items = nested.iter().cloned().collect();
                                match ident.as_ref() {
                                    "length" => {
                                        assert_has_len(rust_ident.clone(), field_type);
                                        validators.push(extract_length_validation(rust_ident.clone(), &meta_items));
                                    },
                                    "range" => {
                                        assert_has_range(rust_ident.clone(), field_type);
                                        validators.push(extract_range_validation(rust_ident.clone(), &meta_items));
                                    },
                                    "email" | "url" | "phone" | "credit_card" => {
                                        validators.push(extract_argless_validation(ident.to_string(), rust_ident.clone(), &meta_items));
                                    },
                                    "custom" => {
                                        validators.push(extract_one_arg_validation("function", ident.to_string(), rust_ident.clone(), &meta_items));
                                    },
                                    "contains" => {
                                        validators.push(extract_one_arg_validation("pattern", ident.to_string(), rust_ident.clone(), &meta_items));
                                    },
                                    "regex" => {
                                        validators.push(extract_one_arg_validation("path", ident.to_string(), rust_ident.clone(), &meta_items));
                                    },
                                    "must_match" => {
                                        let validation = extract_one_arg_validation("other", ident.to_string(), rust_ident.clone(), &meta_items);
                                        if let Validator::MustMatch(ref t2) = validation.validator {
                                            assert_type_matches(rust_ident.clone(), field_type, field_types.get(t2));
                                        }
                                        validators.push(validation);
                                    },
                                    v => panic!("unexpected list validator: {:?}", v)
                                }
                            },
                        },
                        _ => unreachable!("Found a non Meta while looking for validators")
                    };
                }
            },
            _ => unreachable!("Got something other than a list of attributes while checking field `{}`", field_ident),
        }
    }

    if has_validate && validators.is_empty() {
        error("it needs at least one validator");
    }

    (field_ident, validators)
}

/// Serde can be used to rename fields on deserialization but most of the times
/// we want the error on the original field.
///
/// For example a JS frontend might send camelCase fields and Rust converts them to snake_case
/// but we want to send the errors back with the original name
fn find_original_field_name(meta_items: &Vec<&syn::NestedMeta>) -> Option<String> {
    let mut original_name = None;

    for meta_item in meta_items {
        match *meta_item {
            &syn::NestedMeta::Meta(ref item) => match *item {
                syn::Meta::Word(_) => continue,
                syn::Meta::NameValue(syn::MetaNameValue { ref ident, ref lit, .. }) => {
                    if ident.as_ref() == "rename" {
                        original_name = Some(lit_to_string(lit).unwrap());
                    }
                },
                syn::Meta::List(syn::MetaList { ref nested, .. }) => {
                    return find_original_field_name(&nested.iter().collect());
                }
            },
            _ => unreachable!()
        };

        if original_name.is_some() {
            return original_name;
        }
    }

    original_name
}