Skip to content

Conversation

@smaye81
Copy link
Contributor

@smaye81 smaye81 commented Mar 27, 2025

This beefs up the validation for the following:

IP address (v4 and v6)
IP prefix (v4 and v6)
Hostname / Port

The validation here adheres to the defined RFC standards for each entity and passes conformance for all new conformance tests defined in bufbuild/protovalidate#320.

@smaye81 smaye81 requested review from rodaine and timostamm March 27, 2025 17:51
@rodaine rodaine requested a review from stefanvanburen March 27, 2025 17:53
return celtypes.BoolType(_is_ip_prefix(val, version, strict=strict))


def _is_ip_prefix(string: str, version: int, *, strict=False) -> bool:
Copy link
Contributor Author

@smaye81 smaye81 Mar 27, 2025

Choose a reason for hiding this comment

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

Not sure of the best way to handle this. Other validation methods call this internally and if we stick with just is_ip_prefix then they have to pass cel values and handle cel types.

We have a similar scenario with Java (and C++). We could go back to the is_email and validate_email pattern that was in use before. FWIW, Java has that pattern now also. 🤷

Copy link
Member

Choose a reason for hiding this comment

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

Yes, looks like a good reason for a separate function to me.

is_ vs. validate_ is not self-explanatory. What if we prefix the type converting function with cel_?

def cel_is_ip(val: celtypes.Value, ver: typing.Optional[celtypes.Value] = None) -> celpy.Result:
   ...
   return celtypes.BoolType(is_ip(...))

def is_ip(val: str, ver: int) -> bool:
   ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated. I also updated the other functions that don't require an internal implementation just to keep things consistent.

Comment on lines +12 to +18
# The below tests are failing due to a bug in the conformance runner. These
# proto messages are marked as IGNORE_ALWAYS which means they should always
# pass, which this implementation does correctly. However, the runner is
# expecting them to fail.
# See:
# https://github.com/bufbuild/protovalidate/blob/main/proto/protovalidate-testing/buf/validate/conformance/cases/required_field_proto2.proto#L24
# https://github.com/bufbuild/protovalidate/blob/main/proto/protovalidate-testing/buf/validate/conformance/cases/required_field_proto2.proto#L31
Copy link
Member

Choose a reason for hiding this comment

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

Fixing this in bufbuild/protovalidate#340.

@smaye81 smaye81 merged commit 8a0b929 into sayers/validation_upgrades Mar 28, 2025
7 checks passed
@smaye81 smaye81 deleted the sayers/is_ip branch March 28, 2025 18:04
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