Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand buf lint PROTOVALIDATE rule to lint example and predefined rules #3317

Merged
merged 39 commits into from
Oct 3, 2024

Conversation

oliversun9
Copy link
Contributor

@oliversun9 oliversun9 commented Sep 13, 2024

This updates the PROTOVALIDATE rule in buf lint to provide checks for two new features:

  • For the protovalidate examples feature, check:
    • example values satisfy the constraints (builtin + cel)
    • if no constraint is defined on the field, an example value should not be present
  • For the predefined rules feature, check:
    • a predefined rule compiles (the expression defined on buf.validate.*Rules compiles)
    • example values also satisfy any predefined rules

The protovalidate test dependency has also been updated and some previous integration tests have also been updated to reflect this.

Copy link
Contributor

github-actions bot commented Sep 13, 2024

The latest Buf updates on your PR. Results from workflow Buf CI / buf (pull_request).

BuildFormatLintBreakingUpdated (UTC)
✅ passed✅ passed✅ passed✅ passedOct 3, 2024, 2:39 PM

Comment on lines +104 to +106
func celTypeForStandardRuleMessageDescriptor(
ruleMessageDescriptor protoreflect.MessageDescriptor,
) *cel.Type {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rodaine Just wondering if there is a better way to do this and/or if protovalidate-go exposes some mechanism for me to resolve the "type of rule" based on the rule message descriptor (e.g. StringRules). The context of this is that, we are parsing predefined rules from our files, and then checking their CEL expressions. And so we need the type for the extension itself (which is easy, we use the celext function you and @oliversun9 exported a while ago) and we also need the type for the rule that the extension is extending.

@doriable doriable marked this pull request as ready for review October 1, 2024 15:32
make/buf/all.mk Show resolved Hide resolved
exampleValues []protoreflect.Value,
extensionTypeResolver protoencoding.Resolver,
) error {
if err := protoencoding.ReparseExtensions(extensionTypeResolver, typeRulesMessage); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like a pretty deep level to be calling protoencoding.ReparseExtensions, to do this per field seems unnecessary, can you provide some documentation as to why we're calling this here, and why it can't be called at a higher level?

Copy link
Member

@doriable doriable Oct 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment to clarify this, but basically it's to ensure that predefined rules from imported files are properly resolved.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment doesn't make sense to me as a reader - I am still confused why I need to call ReparseExtensions for every field. Can you make sure this comment would make sense to another reader without context by verifying with someone else what their understanding of it is?

return r.StandardConstraintResolver.ResolveFieldConstraints(desc)
}

func (r *constraintsResolverForTargetField) ResolveMessageConstraints(desc protoreflect.MessageDescriptor) *validate.MessageConstraints {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: no need to have r name for struct, and no need for desc either

return nil
}

func (r *constraintsResolverForTargetField) ResolveOneofConstraints(desc protoreflect.OneofDescriptor) *validate.OneofConstraints {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

// for marshalling and unmarshalling. We also added error handling for marshal/unmarshal.
//
// This resolves the given extension and returns the constraints for the extension.
func resolveExt[C proto.Message](
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other methods called *Extension, call this resolveExtension

resolver protoencoding.Resolver,
) (constraints C, retErr error) {
num := extType.TypeDescriptor().Number()
var msg proto.Message
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

message

@bufdev bufdev changed the title Expand buf lint PROTOVALIDATE rule to lint example and shared rules Expand buf lint PROTOVALIDATE rule to lint example and predefined rules Oct 3, 2024
@bufdev bufdev merged commit 23c7df4 into main Oct 3, 2024
11 checks passed
@bufdev bufdev deleted the osun/lint-examples branch October 3, 2024 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants