You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Standard rules on repeated items and map keys/values are silently dropped (max_bytes, prefix, email, uuid, …)
#114
protoc-gen-protovalidate-buffa v0.10.6 leaves many standard rules out of the generated validator when they are set on the items of a repeated field, or on the keys or values of a map. There is no error and no compile_error, and validate() accepts a value that breaks the rule. The same rules on a singular field are generated correctly. proto3 and edition 2024 behave the same.
The cause is in emit/repeated.rs. Items go through emit_repeated_items_checks, and map keys and values go through emit_map_kv_checks, with emit_scalar_checks as a fallback. Each is a hand-kept list that covers only part of what the singular emitters in emit/field.rs cover. A rule missing from a list is skipped without a word.
Expected:validate() refuses a 65-byte key, a 513-byte value and a 1025-byte id, as it does when max_bytes is set on a singular string.
Actual: all three are accepted. The generated probe.v1.probe.validate.rs checks map.max_pairs and string.min_len, but has no string.max_bytes at all (0 occurrences; 6 with the fix, for 3 rules × owned and view).
Downstream, we have such bounds on a document's attribute map (at most 16 pairs, keys 1–64 bytes, values 1–512 bytes) and on lists of ids (1024 bytes per id). Today those bounds have to live in application code, because the proto rule is dropped.
What is dropped
One proto per rule and position. A rule is "yes" when validate() refuses an invalid value, for owned messages and views. Checked with the tests in the reference fix against v0.10.6, and also by looking for the rule id in the generated file.
¹ string.const is emitted as elem != "abc". That compiles for the owned message, but not in the view impl, where the element is &&str (E0277: can't compare &str with str).
The same lists also drop numeric and bool rules:
in and not_in of every integer family and of float/double: dropped on items, keys and values.
uint32/fixed32 keys and values: const, lt and lte are dropped. uint64/fixed64 keys and values: const, lt, lte, gt and gte are dropped.
float values: const and finite are dropped. double values: const, lt, lte and finite are dropped.
bool.const on keys and values is dropped.
Two neighbouring paths have the same kind of narrower list:
Explicit presence (any singular scalar under editions, or proto3 optional): numeric in/not_in and float/doubleconst are dropped. The same field in plain proto3 gets them.
Wrapper fields (google.protobuf.StringValue, BytesValue, Int32Value, …): most rules are dropped. Examples are string.max_bytes, len, pattern, in and every well-known format, every bytes rule but min_len/max_len, numeric in/not_in, and float/doubleconst and finite.
Related defects on the same path
Empty messages. The rules that the item and map lists do emit carry message: "". For example, repeated double weights with items double: {gte: 0, finite: true} gives double.gte and double.finite with message "", while the same rules on a singular double give value must be >= 0 (got -inf) and value must be finite.
Item CEL rules dropped. When any standard item rule is emitted, emit_repeated returns early and skips repeated.items.cel and predefined item rules. For example, items: {string: {min_len: 1}, cel: {...}} generates min_len and no CEL check.
Rule paths. Rules that come from the emit_scalar_checks fallback (bytes.min_len/max_len on items and values, and some numeric rules on keys and values) report a rule path without the repeated.items / map.keys / map.values prefix, and a field path without the index or key subscript.
Change
emit/field.rs gets one entry point, emit_scalar_rules(value, kind, rules, site), built from the existing singular emitters (emit_string_value, emit_bytes_value, the numeric, bool and enum emitters). Each emitter now takes the value expression and a RuleSite in place of self.<field> and a fixed field path. A RuleSite holds the violation's field path, the rule-path prefix ([], repeated.items or map.keys/map.values) and for_key. Singular fields, explicit presence, oneof members, wrappers, repeated items and map keys and values all call this one function. emit_repeated_items_checks, emit_map_kv_checks, emit_scalar_checks, emit_numeric_checks_on, and the rule lists in emit_optional_inner and emit_wrapper_inner are removed. A rule added later to a singular emitter then applies at every position. Messages are the singular ones, and the early return in emit_repeated is gone.
The diff is +4548 / −3997 lines. Most of the additions are the new test fixtures; field.rs and repeated.rs shrink by about 3,250 lines. The public emit::field::emit_*_on_pub helpers are left as they were, since emit is a public module; nothing in the crate calls them any more.
Verification
New testtests/validation_collection_rules.rs, with fixtures in proto/validation/collection_rules/{proto3,editions2024}. It covers every StringRules and BytesRules rule and every numeric and bool rule, on items, keys and values (136 cases), in proto3 and edition 2024, for owned messages and views. Each case checks that a valid value passes and that an invalid one gives exactly one violation with the right rule_id, rule path, field number, subscript (index 1 for items, the key for maps) and for_key (true on keys only). It also checks explicit presence, wrappers, item CEL next to standard rules, and that item messages equal singular ones. The 14 tests fail on v0.10.6 (the fixture does not even compile there because of string.const) and pass with the fix.
Upstream conformance: 2872/2872 before and after (protovalidate-conformance from @latest).
Full suite:cargo test --workspace --all-targets passes 338 tests. hk check --all --slow passes all steps, including clippy with -D warnings on the pinned Rust 1.98.0. cargo package --list --locked --workspace and rustup run 1.88.0 cargo check --locked --workspace pass too.
Consumers must regenerate validators with the updated plugin. The runtime crate is unchanged.
Versions
protoc-gen-protovalidate-buffa 0.10.6 (34fc2bc), with runtime protovalidate-buffa 0.7.2
buf/validate/validate.proto as vendored in this repository's conformance crate
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
A reference fix with regression tests is in amsokol/protovalidate-buffa@3f4e350, on the branch
fix/item-standard-rules. Feel free to take it as is or reimplement it.Problem
protoc-gen-protovalidate-buffav0.10.6 leaves many standard rules out of the generated validator when they are set on the items of a repeated field, or on the keys or values of a map. There is no error and nocompile_error, andvalidate()accepts a value that breaks the rule. The same rules on a singular field are generated correctly. proto3 and edition 2024 behave the same.The cause is in
emit/repeated.rs. Items go throughemit_repeated_items_checks, and map keys and values go throughemit_map_kv_checks, withemit_scalar_checksas a fallback. Each is a hand-kept list that covers only part of what the singular emitters inemit/field.rscover. A rule missing from a list is skipped without a word.Minimal example
Expected:
validate()refuses a 65-byte key, a 513-byte value and a 1025-byte id, as it does whenmax_bytesis set on a singularstring.Actual: all three are accepted. The generated
probe.v1.probe.validate.rschecksmap.max_pairsandstring.min_len, but has nostring.max_bytesat all (0 occurrences; 6 with the fix, for 3 rules × owned and view).Downstream, we have such bounds on a document's attribute map (at most 16 pairs, keys 1–64 bytes, values 1–512 bytes) and on lists of ids (1024 bytes per id). Today those bounds have to live in application code, because the proto rule is dropped.
What is dropped
One proto per rule and position. A rule is "yes" when
validate()refuses an invalid value, for owned messages and views. Checked with the tests in the reference fix against v0.10.6, and also by looking for the rule id in the generated file.stringconstlenmin_len,max_lenlen_bytes,min_bytes,max_bytespatternprefix,suffix,contains,not_containsin,not_inemail,hostname,ip,ipv4,ipv6,uri,uri_ref,address,uuid,tuuid,ip_with_prefixlen,ipv4_with_prefixlen,ipv6_with_prefixlen,ip_prefix,ipv4_prefix,ipv6_prefix,host_and_port,ulid,protobuf_fqn,protobuf_dot_fqn,well_known_regexbytesmin_len,max_lenconst,len,pattern,prefix,suffix,contains,in,not_in,ip,ipv4,ipv6,uuid¹
string.constis emitted aselem != "abc". That compiles for the owned message, but not in the view impl, where the element is&&str(E0277: can't compare &str with str).The same lists also drop numeric and bool rules:
inandnot_inof every integer family and offloat/double: dropped on items, keys and values.uint32/fixed32keys and values:const,ltandlteare dropped.uint64/fixed64keys and values:const,lt,lte,gtandgteare dropped.floatvalues:constandfiniteare dropped.doublevalues:const,lt,lteandfiniteare dropped.bool.conston keys and values is dropped.Two neighbouring paths have the same kind of narrower list:
optional): numericin/not_inandfloat/doubleconstare dropped. The same field in plain proto3 gets them.google.protobuf.StringValue,BytesValue,Int32Value, …): most rules are dropped. Examples arestring.max_bytes,len,pattern,inand every well-known format, everybytesrule butmin_len/max_len, numericin/not_in, andfloat/doubleconstandfinite.Related defects on the same path
message: "". For example,repeated double weightswith itemsdouble: {gte: 0, finite: true}givesdouble.gteanddouble.finitewith message"", while the same rules on a singulardoublegivevalue must be >= 0 (got -inf)andvalue must be finite.emit_repeatedreturns early and skipsrepeated.items.celand predefined item rules. For example,items: {string: {min_len: 1}, cel: {...}}generatesmin_lenand no CEL check.emit_scalar_checksfallback (bytes.min_len/max_lenon items and values, and some numeric rules on keys and values) report a rule path without therepeated.items/map.keys/map.valuesprefix, and a field path without the index or key subscript.Change
emit/field.rsgets one entry point,emit_scalar_rules(value, kind, rules, site), built from the existing singular emitters (emit_string_value,emit_bytes_value, the numeric, bool and enum emitters). Each emitter now takes the value expression and aRuleSitein place ofself.<field>and a fixed field path. ARuleSiteholds the violation's field path, the rule-path prefix ([],repeated.itemsormap.keys/map.values) andfor_key. Singular fields, explicit presence, oneof members, wrappers, repeated items and map keys and values all call this one function.emit_repeated_items_checks,emit_map_kv_checks,emit_scalar_checks,emit_numeric_checks_on, and the rule lists inemit_optional_innerandemit_wrapper_innerare removed. A rule added later to a singular emitter then applies at every position. Messages are the singular ones, and the early return inemit_repeatedis gone.The diff is +4548 / −3997 lines. Most of the additions are the new test fixtures;
field.rsandrepeated.rsshrink by about 3,250 lines. The publicemit::field::emit_*_on_pubhelpers are left as they were, sinceemitis a public module; nothing in the crate calls them any more.Verification
tests/validation_collection_rules.rs, with fixtures inproto/validation/collection_rules/{proto3,editions2024}. It covers everyStringRulesandBytesRulesrule and every numeric and bool rule, on items, keys and values (136 cases), in proto3 and edition 2024, for owned messages and views. Each case checks that a valid value passes and that an invalid one gives exactly one violation with the rightrule_id, rule path, field number, subscript (index 1 for items, the key for maps) andfor_key(true on keys only). It also checks explicit presence, wrappers, item CEL next to standard rules, and that item messages equal singular ones. The 14 tests fail on v0.10.6 (the fixture does not even compile there because ofstring.const) and pass with the fix.protovalidate-conformancefrom@latest).cargo test --workspace --all-targetspasses 338 tests.hk check --all --slowpasses all steps, including clippy with-D warningson the pinned Rust 1.98.0.cargo package --list --locked --workspaceandrustup run 1.88.0 cargo check --locked --workspacepass too.Consumers must regenerate validators with the updated plugin. The runtime crate is unchanged.
Versions
protoc-gen-protovalidate-buffa0.10.6 (34fc2bc), with runtimeprotovalidate-buffa0.7.2buf/validate/validate.protoas vendored in this repository's conformance crateDisclosure: Claude Opus 5.5 found this bug; Claude Opus 5.5 wrote the reference fix and drafted this Discussion.
All reactions