Skip to content

Conversation

@ghost
Copy link

@ghost ghost commented Dec 4, 2024

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.

@ghost ghost requested review from Alfus and stefanvanburen December 4, 2024 16:41
_violations: list[Violation]

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
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
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
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.

@ghost ghost merged commit 5e6ac7b into main Dec 4, 2024
12 checks passed
@ghost ghost deleted the jchadwick/rule-field-values branch December 4, 2024 21:12
This pull request was closed.
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.

1 participant