prost-protovalidate-build 0.4.2

Build-time code generator for zero-cost Protocol Buffer validation using buf.validate rules
Documentation
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! String rule code generation.

use proc_macro2::TokenStream;
use quote::quote;

use prost_protovalidate_types::{StringRules, string_rules};

#[allow(clippy::too_many_lines, clippy::cast_possible_truncation)]
pub(crate) fn generate(
    rules: &StringRules,
    value_access: &TokenStream,
    proto_name: &str,
) -> Vec<TokenStream> {
    let mut checks = Vec::new();

    // Const
    if let Some(ref c) = rules.r#const {
        let msg = format!("must equal `{c}`");
        checks.push(quote! {
            if #value_access != #c {
                violations.push(::prost_protovalidate::Violation::new(
                    #proto_name, "string.const", #msg,
                ));
            }
        });
    }

    // Exact character length
    if let Some(len) = rules.len {
        let len_usize = len as usize;
        let msg = format!("must be {len} characters");
        checks.push(quote! {
            if #value_access.chars().count() != #len_usize {
                violations.push(::prost_protovalidate::Violation::new(
                    #proto_name, "string.len", #msg,
                ));
            }
        });
    }

    // Min length (characters)
    if let Some(min) = rules.min_len {
        let min_usize = min as usize;
        let msg = format!("value length must be at least {min} characters");
        checks.push(quote! {
            if #value_access.chars().count() < #min_usize {
                violations.push(::prost_protovalidate::Violation::new(
                    #proto_name, "string.min_len", #msg,
                ));
            }
        });
    }

    // Max length (characters)
    if let Some(max) = rules.max_len {
        let max_usize = max as usize;
        let msg = format!("value length must be at most {max} characters");
        checks.push(quote! {
            if #value_access.chars().count() > #max_usize {
                violations.push(::prost_protovalidate::Violation::new(
                    #proto_name, "string.max_len", #msg,
                ));
            }
        });
    }

    // Exact byte length
    if let Some(len) = rules.len_bytes {
        let len_usize = len as usize;
        let msg = format!("must be {len} bytes");
        checks.push(quote! {
            if #value_access.len() != #len_usize {
                violations.push(::prost_protovalidate::Violation::new(
                    #proto_name, "string.len_bytes", #msg,
                ));
            }
        });
    }

    // Min bytes
    if let Some(min) = rules.min_bytes {
        let min_usize = min as usize;
        let msg = format!("must be at least {min} bytes");
        checks.push(quote! {
            if #value_access.len() < #min_usize {
                violations.push(::prost_protovalidate::Violation::new(
                    #proto_name, "string.min_bytes", #msg,
                ));
            }
        });
    }

    // Max bytes
    if let Some(max) = rules.max_bytes {
        let max_usize = max as usize;
        let msg = format!("must be at most {max} bytes");
        checks.push(quote! {
            if #value_access.len() > #max_usize {
                violations.push(::prost_protovalidate::Violation::new(
                    #proto_name, "string.max_bytes", #msg,
                ));
            }
        });
    }

    // Pattern (regex).
    //
    // The capability analyzer (see `find_invalid_regex` in `codegen.rs`)
    // ensures only valid patterns reach codegen; the `expect` below
    // surfaces a future regex-crate regression loudly instead of
    // misreporting validation as a silent pattern mismatch.
    if let Some(ref pattern) = rules.pattern {
        let msg = format!("does not match regex pattern `{pattern}`");
        checks.push(quote! {
            {
                static RE: ::std::sync::LazyLock<::prost_protovalidate::regex::Regex> =
                    ::std::sync::LazyLock::new(|| {
                        ::prost_protovalidate::regex::Regex::new(#pattern)
                            .expect("pattern validated at codegen time")
                    });
                if !RE.is_match(&#value_access) {
                    violations.push(::prost_protovalidate::Violation::new(
                        #proto_name, "string.pattern", #msg,
                    ));
                }
            }
        });
    }

    // Prefix
    if let Some(ref prefix) = rules.prefix {
        let msg = format!("does not have prefix `{prefix}`");
        checks.push(quote! {
            if !#value_access.starts_with(#prefix) {
                violations.push(::prost_protovalidate::Violation::new(
                    #proto_name, "string.prefix", #msg,
                ));
            }
        });
    }

    // Suffix
    if let Some(ref suffix) = rules.suffix {
        let msg = format!("does not have suffix `{suffix}`");
        checks.push(quote! {
            if !#value_access.ends_with(#suffix) {
                violations.push(::prost_protovalidate::Violation::new(
                    #proto_name, "string.suffix", #msg,
                ));
            }
        });
    }

    // Contains
    if let Some(ref contains) = rules.contains {
        let msg = format!("does not contain substring `{contains}`");
        checks.push(quote! {
            if !#value_access.contains(#contains) {
                violations.push(::prost_protovalidate::Violation::new(
                    #proto_name, "string.contains", #msg,
                ));
            }
        });
    }

    // Not contains
    if let Some(ref not_contains) = rules.not_contains {
        let msg = format!("contains substring `{not_contains}`");
        checks.push(quote! {
            if #value_access.contains(#not_contains) {
                violations.push(::prost_protovalidate::Violation::new(
                    #proto_name, "string.not_contains", #msg,
                ));
            }
        });
    }

    // In list — sort to match the deterministic runtime format
    // (`format!("must be in list {sorted:?}")` where `sorted: Vec<&String>`).
    if !rules.r#in.is_empty() {
        let mut sorted: Vec<&String> = rules.r#in.iter().collect();
        sorted.sort();
        let vals = sorted;
        checks.push(quote! {
            {
                let _s: &str = &#value_access;
                if ![#(#vals),*].contains(&_s) {
                    violations.push(::prost_protovalidate::Violation::new(
                        #proto_name, "string.in", format!("must be in list {:?}", &[#(#vals),*]),
                    ));
                }
            }
        });
    }

    // Not-in list — same sorted format.
    if !rules.not_in.is_empty() {
        let mut sorted: Vec<&String> = rules.not_in.iter().collect();
        sorted.sort();
        let vals = sorted;
        checks.push(quote! {
            {
                let _s: &str = &#value_access;
                if [#(#vals),*].contains(&_s) {
                    violations.push(::prost_protovalidate::Violation::new(
                        #proto_name, "string.not_in", format!("must not be in list {:?}", &[#(#vals),*]),
                    ));
                }
            }
        });
    }

    // Well-known format validators
    let strict = rules.strict.unwrap_or(true);
    if let Some(wk) = rules.well_known {
        checks.extend(generate_well_known(wk, value_access, proto_name, strict));
    }

    checks
}

/// Generate well-known string format checks.
#[allow(clippy::too_many_lines)]
fn generate_well_known(
    wk: string_rules::WellKnown,
    value_access: &TokenStream,
    proto_name: &str,
    strict: bool,
) -> Vec<TokenStream> {
    match wk {
        string_rules::WellKnown::Email(true) => {
            vec![format_check(
                value_access,
                proto_name,
                "email",
                quote! { ::prost_protovalidate::validators::is_email(&#value_access) },
            )]
        }
        string_rules::WellKnown::Hostname(true) => {
            vec![format_check(
                value_access,
                proto_name,
                "hostname",
                quote! { ::prost_protovalidate::validators::is_hostname(&#value_access) },
            )]
        }
        string_rules::WellKnown::Ip(true) => {
            vec![format_check(
                value_access,
                proto_name,
                "ip",
                quote! { ::prost_protovalidate::validators::is_ip(&#value_access) },
            )]
        }
        string_rules::WellKnown::Ipv4(true) => {
            vec![format_check(
                value_access,
                proto_name,
                "ipv4",
                quote! { ::prost_protovalidate::validators::is_ipv4(&#value_access) },
            )]
        }
        string_rules::WellKnown::Ipv6(true) => {
            vec![format_check(
                value_access,
                proto_name,
                "ipv6",
                quote! { ::prost_protovalidate::validators::is_ipv6(&#value_access) },
            )]
        }
        string_rules::WellKnown::Uri(true) => {
            vec![format_check(
                value_access,
                proto_name,
                "uri",
                quote! { ::prost_protovalidate::validators::is_uri(&#value_access) },
            )]
        }
        string_rules::WellKnown::UriRef(true) => {
            vec![format_check(
                value_access,
                proto_name,
                "uri_ref",
                quote! { ::prost_protovalidate::validators::is_uri_ref(&#value_access) },
            )]
        }
        string_rules::WellKnown::Uuid(true) => {
            vec![format_check(
                value_access,
                proto_name,
                "uuid",
                quote! { ::prost_protovalidate::validators::is_uuid(&#value_access) },
            )]
        }
        string_rules::WellKnown::Tuuid(true) => {
            vec![format_check(
                value_access,
                proto_name,
                "tuuid",
                quote! { ::prost_protovalidate::validators::is_tuuid(&#value_access) },
            )]
        }
        string_rules::WellKnown::Ulid(true) => {
            vec![format_check(
                value_access,
                proto_name,
                "ulid",
                quote! { ::prost_protovalidate::validators::is_ulid(&#value_access) },
            )]
        }
        string_rules::WellKnown::Address(true) => {
            vec![format_check(
                value_access,
                proto_name,
                "address",
                quote! {
                    ::prost_protovalidate::validators::is_hostname(&#value_access)
                    || ::prost_protovalidate::validators::is_ip(&#value_access)
                },
            )]
        }
        string_rules::WellKnown::HostAndPort(true) => {
            vec![format_check(
                value_access,
                proto_name,
                "host_and_port",
                quote! { ::prost_protovalidate::validators::is_host_and_port(&#value_access, false) },
            )]
        }
        string_rules::WellKnown::IpWithPrefixlen(true) => {
            vec![format_check(
                value_access,
                proto_name,
                "ip_with_prefixlen",
                quote! { ::prost_protovalidate::validators::is_ip_prefix(&#value_access, false) },
            )]
        }
        string_rules::WellKnown::Ipv4WithPrefixlen(true) => {
            vec![format_check(
                value_access,
                proto_name,
                "ipv4_with_prefixlen",
                quote! { ::prost_protovalidate::validators::is_ipv4_prefix(&#value_access, false) },
            )]
        }
        string_rules::WellKnown::Ipv6WithPrefixlen(true) => {
            vec![format_check(
                value_access,
                proto_name,
                "ipv6_with_prefixlen",
                quote! { ::prost_protovalidate::validators::is_ipv6_prefix(&#value_access, false) },
            )]
        }
        string_rules::WellKnown::IpPrefix(true) => {
            vec![format_check(
                value_access,
                proto_name,
                "ip_prefix",
                quote! { ::prost_protovalidate::validators::is_ip_prefix(&#value_access, true) },
            )]
        }
        string_rules::WellKnown::Ipv4Prefix(true) => {
            vec![format_check(
                value_access,
                proto_name,
                "ipv4_prefix",
                quote! { ::prost_protovalidate::validators::is_ipv4_prefix(&#value_access, true) },
            )]
        }
        string_rules::WellKnown::Ipv6Prefix(true) => {
            vec![format_check(
                value_access,
                proto_name,
                "ipv6_prefix",
                quote! { ::prost_protovalidate::validators::is_ipv6_prefix(&#value_access, true) },
            )]
        }
        string_rules::WellKnown::WellKnownRegex(kind) => {
            match prost_protovalidate_types::KnownRegex::try_from(kind) {
                Ok(prost_protovalidate_types::KnownRegex::HttpHeaderName) => {
                    vec![quote! {
                        if !::prost_protovalidate::validators::is_http_header_name(&#value_access, #strict) {
                            let rule_id = if #value_access.is_empty() {
                                "string.well_known_regex.header_name_empty"
                            } else {
                                "string.well_known_regex.header_name"
                            };
                            violations.push(::prost_protovalidate::Violation::new_constraint(
                                #proto_name, rule_id, "string.well_known_regex",
                            ));
                        }
                    }]
                }
                Ok(prost_protovalidate_types::KnownRegex::HttpHeaderValue) => {
                    vec![quote! {
                        if !::prost_protovalidate::validators::is_http_header_value(&#value_access, #strict) {
                            violations.push(::prost_protovalidate::Violation::new_constraint(
                                #proto_name,
                                "string.well_known_regex.header_value",
                                "string.well_known_regex",
                            ));
                        }
                    }]
                }
                _ => Vec::new(),
            }
        }
        _ => Vec::new(),
    }
}

/// Generate the standard two-phase format check:
/// 1. Empty string → `{format}_empty` violation
/// 2. Invalid format → `string.{format}` violation
#[allow(clippy::needless_pass_by_value)]
fn format_check(
    value_access: &TokenStream,
    proto_name: &str,
    format_name: &str,
    is_valid: TokenStream,
) -> TokenStream {
    let empty_rule_id = format!("string.{format_name}_empty");
    let format_rule_id = format!("string.{format_name}");
    let rule_path = format!("string.{format_name}");

    quote! {
        if #value_access.is_empty() {
            violations.push(::prost_protovalidate::Violation::new_constraint(
                #proto_name, #empty_rule_id, #rule_path,
            ));
        } else if !(#is_valid) {
            violations.push(::prost_protovalidate::Violation::new_constraint(
                #proto_name, #format_rule_id, #rule_path,
            ));
        }
    }
}