Conversation
protovalidate/validator.py
Outdated
| _violations: list[Violation] | ||
|
|
||
| def __init__(self, msg: str, violations: validate_pb2.Violations): | ||
| def __init__(self, msg: str, violations: list[validate_pb2.Violations]): |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.)
stefanvanburen
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
could be a @dataclasses.dataclass, I think?
There was a problem hiding this comment.
Yeah, this could just be a dataclass.
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:
ValidationErrorhas changed:ValidationError.violationsshould callValidationError.to_proto()instead, to get abuf.validate.Violationsmessage.ValidationError.errorswas removed. Switch to usingValidationError.violationsinstead.ValidationError.violationsprovides a list of the newViolationwrapper type instead of a list ofbuf.validate.Violation.Violationwrapper type contains thebuf.validate.Violationmessage under theprotofield, as well asfield_valueandrule_valueproperties that capture the field and rule values, respectively.Validator.collect_violationsnow operates on and returnslist[Violation]instead of the protobufbuf.validate.Violationsmessage.This API mirrors the changes being made in protovalidate-go in bufbuild/protovalidate-go#154.