-
Notifications
You must be signed in to change notification settings - Fork 44
Changing util's validation to raise on exceptions #117
Conversation
Added logic for Also, fixed my email to use the one I signed the CLA with. |
Last update pending review (I promise). I noticed multiple functions were missing complete docstrings for the new return_error param, and one had no param or return at all. Added those in. This was a purely cosmetic update. |
Thanks for contributing @insequent! I'm sorry we got this wrong from the start. I'll make any detailed comments against the code, but first cover the general form of the validation functions. I had a chat with some of the team, and we think the least clunky (but still back compatible) approach is probably to rename the functions to verify_, change them to return nothing and raise exceptions when needed, and then add the validate_ functions back in to point to them. Maybe we could decorate the old validate_ functions to log a deprecation warning, call the new verify_ equivalent and return accordingly? What do you think? |
|
||
if not valid and return_error: | ||
err = ValueError("ICMP type is invalid. Value must be between 0 and " | ||
"255 ('{0}' given).".format(icmp_type)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The range includes 0 (but not 255).
Detail-wise, I found a couple of off-by-ones in the messages, but otherwise looks great - thanks :). |
That makes sense and sounds much cleaner. I'll get right on it! |
590c9a6
to
c95cb34
Compare
This commit should be closer in line with what you're looking for. However, I did take a few liberties this time:
|
Oh, as to deprecated decorators, I love the idea, but wasn't sure how you all would want them. As you can see in the code, I just put |
in_range = False | ||
if not in_range: | ||
break | ||
return return_bool(validate_asn, asn) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should call verify_, not validate_.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh! I didn't even notice there's no unit tests for validate_asn
. I'll fix that as well.
Added unit tests for |
|
||
:param cidr: IP addr and associated routing prefix | ||
:type cidr: str | ||
:return: True if valid IP, False if invalid |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be returns None.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is invalid now, although Github's page doesn't seem to think so. Thought it was my browser caching it, but it shows the same way in others. shrug
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the comment might have moved from verify_ to validate_ as the diff changed underneath it? Either that or it was just a bogus comment in the first place.
Weird, but you're right - it's definitely fixed.
OK - I promise this is the last round of comments! Thanks for a very comprehensive PR! |
a6ed38f
to
b9f58a5
Compare
Whew! Ok, so I took things even further, so definitely double check me.
|
raise RangeValidationError(err_mess) | ||
|
||
# NOTE: The real limit in DNS is 255 octets (253 chars) or 254 chars | ||
# if you include the root domain (i.e. a period on the end). RFC1035 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice.
|
That makes perfect sense. I'll add single base class. |
To retain compatability with legacy code, a new method was created with the name changed from 'validate_<object>' to 'verify_<object>'
The catch-all exception class is now |
Sorry it took me so long to get back to this: all looks great now. Thanks for the contribution :) |
These validation functions are used by clients such as calico-containers, and when they fail, users may want to know why such validations are failing. With this feature, pre-existing code will continue to work as is, however clients now have the option of returning the error (exception) for further diagnostics.
It feels a touch clunky with the way this is implemented, but I didn't want to break any existing code, and this seemed the simplest way. I'm definitely open to any ideas on alternatives.