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

Add rule and field value to violations #224

Merged
merged 4 commits into from
Dec 4, 2024

Conversation

jchadwick-buf
Copy link
Member

Adds the ability to access the captured rule and field value from a Violation.

This is a breaking change. The API changes in the following ways:

  • ValidationError has changed:
    • Old accesses to ValidationError.violations should call ValidationError.to_proto() instead, to get a buf.validate.Violations message.
    • ValidationError.errors was removed. Switch to using ValidationError.violations instead.
    • ValidationError.violations provides a list of the new Violation wrapper type instead of a list of buf.validate.Violation.
    • The new Violation wrapper type contains the buf.validate.Violation message under the proto field, as well as field_value and rule_value properties that capture the field and rule values, respectively.
  • Validator.collect_violations now operates on and returns list[Violation] instead of the protobuf buf.validate.Violations message.

This API mirrors the changes being made in protovalidate-go in bufbuild/protovalidate-go#154.


def __init__(self, msg: str, violations: validate_pb2.Violations):
def __init__(self, msg: str, violations: list[validate_pb2.Violations]):
Copy link
Member

Choose a reason for hiding this comment

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

something about the types here doesn't quite match up, we're passing in the list of proto messages (which is returned raw from def violations) + _violations is typed as list[Violation]. I think they all need to match up?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, you're right. It's unfortunate how easy it is to have extremely wrong types pass type checking. Oh well.

Should be fixed now. (It's a little awkward because Mypy doesn't support "aliasing" a type. Pylance/etc. don't seem to mind.)

Copy link
Member

@stefanvanburen stefanvanburen left a comment

Choose a reason for hiding this comment

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

looks good on my end, although my python is getting rustier all the time :). nothing blocking, just questions

Protobuf-specific collection type used by Violations.
"""
return list(self.violations.violations)
return self._violations
Copy link
Member

Choose a reason for hiding this comment

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

I think this makes sense, because you've aliased _constraints.Violation to Violation; assuming that the constructor takes a _constraints.Violation as it's supposed to be initialized internally, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, this is supposed to return protovalidate.Violation and not buf.validate.Violation. It's probably a bit confusing due to the diff in the API but the naming scheme here now matches closer to the other runtimes.

ctx.add(Violation(constraint_id="unimplemented", message="Unimplemented"))


class CelRunner:
Copy link
Member

Choose a reason for hiding this comment

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

could be a @dataclasses.dataclass, I think?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, this could just be a dataclass.

protovalidate/internal/constraints.py Outdated Show resolved Hide resolved
@jchadwick-buf jchadwick-buf merged commit 5e6ac7b into main Dec 4, 2024
12 checks passed
@jchadwick-buf jchadwick-buf deleted the jchadwick/rule-field-values branch December 4, 2024 21:12
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.

2 participants