Skip to content

[Feature Request] Add skipped_fields field constraint for finer-grained skipped behavior #112

Open

Description

@rodaine

Feature description:

Today, we support the skipped standard constraint on message fields to skip any validation constraints applied to the underlying type. This is currently an all or nothing scenario, when perhaps it's desired to opt-out of one or a few of the message's fields.

Potentially, we can support this via a skipped_fields constraint which takes a FieldMask and can specify which not to evaluate.

Problem it solves or use case:

Example use-case where a message describing a resource elided a constraint for a field and had the RPC request object use a CEL expression to validate that nested field.

Proposed implementation or solution:

# validate.proto

# <snip>

message FieldConstraints {
  # <snip>
  oneof skip {
    bool skipped = 24;
    google.protobuf.FieldMask skipped_fields = XX;
  }
  # <snip>
}

# <snip>
# example.proto

message Foo {
  string id   = 1 [(buf.validate.field).string.uuid = true];
  bytes  data = 2 [(buf.validate.field).bytes.min_len = 42];
}

message Bar {
  Foo foo = 1 [(buf.validate.field).skipped_fields.paths = "id" ];
}

In the example above, Foo by itself will verify the value of the id field to be a UUID and the data field to be of a minimum length. However, if validate is called on a Bar containing a Foo, only the data field will be evaluated as id has been skipped.

Activity

  1. changed the title [-][Feature Request] [/-] [+][Feature Request] Add skipped_fields field constraint for finer-grained skipped behavior[/+] on Oct 17, 2023
  2. czabaj commented on Mar 3, 2025

    @czabaj

    This will work for Google AIP update methods, where the input message is typically just a partial message, and unset values should not trigger validation if not specified in FieldMask update_mask.

    The only drawback is that the update_mask is dynamic and must be applied in runtime via a reflection. For that reason, it would be handy to have MessageConstraints to set skipped_fields on the target message, not on its parent. Maybe define a new well-known function for skipping validation that could be used in the MessageConstraints.cel?

  3. smthpickboy commented on May 17, 2025

    @smthpickboy

    This will work for Google AIP update methods, where the input message is typically just a partial message, and unset values should not trigger validation if not specified in FieldMask update_mask.

    The only drawback is that the update_mask is dynamic and must be applied in runtime via a reflection. For that reason, it would be handy to have MessageConstraints to set skipped_fields on the target message, not on its parent. Maybe define a new well-known function for skipping validation that could be used in the MessageConstraints.cel?

    This is essential for building an API that conforms to Google's AIP spec.

  4. juanmarin-co commented on Jul 23, 2025

    @juanmarin-co

    ¿Is there a workaround to implement dynamic validation given an update_mask? As @smthpickboy said, it is required for complying with Google's AIP

  5. voidfoxy commented on Sep 2, 2025

    @voidfoxy

    This will work for Google AIP update methods, where the input message is typically just a partial message, and unset values should not trigger validation if not specified in FieldMask update_mask.

    The only drawback is that the update_mask is dynamic and must be applied in runtime via a reflection. For that reason, it would be handy to have MessageConstraints to set skipped_fields on the target message, not on its parent. Maybe define a new well-known function for skipping validation that could be used in the MessageConstraints.cel?

    Agreed. We need a way to validate only the fields specified in google.protobuf.FieldMask update_mask.

  6. DolceTriade commented on Mar 11, 2026

    @DolceTriade

    Do we think it might be possible to update the Filter API to pass in a field path? That would make it possible to support field masks using the filter API. Today, we only pass in a message and a field/oneof/message descriptor, which make it extremely difficult to know how we got to a particular field.

    I believe this is possible in a backwards compatible way... Is this a viable option?

  7. jeffsawatzky commented on May 27, 2026

    @jeffsawatzky

    Would something like this work?

    # example.proto
    
    message Foo {
      string id   = 1 [(buf.validate.field).string.uuid = true];
      bytes  data = 2 [(buf.validate.field).bytes.min_len = 42];
    }
    
    message UpdateFooRequest {
      option (buf.validate.message).field_mask.field = "update_mask";
    
      Foo foo = 1;
      google.protobuf.FieldMask update_mask = 2;
    }

    Then, if an update_mask is provided it will be used to validate only those fields? You could also do something like:
    option (buf.validate.message).field_mask.paths = "foo.data"; to specify a hard coded list instead of having one provided in the call. I'm not sure what the paths are supposed to be relative to (would you specify foo.data or just data?), but I assume there is a standard for this and it would need to handle nested messages.

  8. jeffsawatzky commented on May 27, 2026

    @jeffsawatzky

    Maybe it would be better to have the field mask on the field itself like so:

    # example.proto
    
    message Foo {
      string id   = 1 [(buf.validate.field).string.uuid = true];
      bytes  data = 2 [(buf.validate.field).bytes.min_len = 42];
    }
    
    message UpdateFooRequest {
      Foo foo = 1 [buf.validate.field).field_mask.field = "update_mask"];
      google.protobuf.FieldMask update_mask = 2;
    }

    That way it is clearer that the field mask paths are relative to the foo resource which then aligns with google's AIP 134.

  9. rodaine commented on May 27, 2026

    @rodaine
    MemberAuthor

    Do we think it might be possible to update the Filter API to pass in a field path? That would make it possible to support field masks using the filter API. Today, we only pass in a message and a field/oneof/message descriptor, which make it extremely difficult to know how we got to a particular field.

    I believe this is possible in a backwards compatible way... Is this a viable option?

    @DolceTriade the filter behavior is really only exposed by pv-go for buf lint support. It's not considered part of the actual protovalidate API. Since the filtering is not carried by the proto definitions themselves, it doesn't fit into the intent of the protovalidate annotations which are intended to be self contained and portable. Effectively, validation would be different across implementations, which we want to avoid building in as a footgun.


    Maybe it would be better to have the field mask on the field itself like so:

    That way it is clearer that the field mask paths are relative to the foo resource which then aligns with google's AIP 134.

    @jeffsawatzky the primary issue here is that field rules right now (intentionally) don't carry a reference to the parent message or sibling fields. This would need to be on the message level, mapping a mask to its target field. There's prior art for this pattern in the synthetic message.one_of rules.

  10. DolceTriade commented on May 27, 2026

    @DolceTriade

    @DolceTriade the filter behavior is really only exposed by pv-go for buf lint support. It's not considered part of the actual protovalidate API. Since the filtering is not carried by the proto definitions themselves, it doesn't fit into the intent of the protovalidate annotations which are intended to be self contained and portable. Effectively, validation would be different across implementations, which we want to avoid building in as a footgun.

    I see. It would be nice to support selective validation (which i don't think would be different between different implementations of protovalidate...)

  11. jeffsawatzky commented on May 27, 2026

    @jeffsawatzky

    @DolceTriade woild something like what is described in #486 be useful? Having validations run only under certain contexts and those contexts can be specified in the root request/response message options?

  12. rodaine commented on May 28, 2026

    @rodaine
    MemberAuthor

    I see. It would be nice to support selective validation (which i don't think would be different between different implementations of protovalidate...)

    Yep! That's the intent of this issue 😁 It's not just that the implementations have to support it; the protos (schema and/or data) themselves have to carry this information.

  13. DolceTriade commented on May 30, 2026

    @DolceTriade

    @DolceTriade woild something like what is described in #486 be useful? Having validations run only under certain contexts and those contexts can be specified in the root request/response message options?

    A non-declarative variant would be better to be more universally useful. For example, a context needs to be declared ahead of time whereas a field mask could allow fields in a patch request to be skipped entirely (cuz they're empty and not meant to be modified).

    our use cases are very similar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions