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 basic Validation for URITemplates #36

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Add basic Validation for URITemplates #36

wants to merge 2 commits into from

Conversation

sigmavirus24
Copy link
Collaborator

Closes #33

@Julian
Copy link

Julian commented Jul 31, 2017

Nice! That API certainly looks like it'd satisfy what jsonschema needs. Not sure if you mean to solicit feedback on anything specific here, but leaving a few comments just in case. Take all of em with a grain of salt obviously, just pointing em out in case anything wasn't intentionally done exactly as-is.

more, less = 'left', 'right'
if left_braces_count < right_braces_count:
more, less = less, more
super(UnbalancedBraces, self).__init__(uri, more, less)
Copy link

Choose a reason for hiding this comment

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

Do you mean uri, more, less? The stuff you super for an exception will end up on e.args as the stuff that was passed to __init__, but the "user" of the exception here didn't pass those, they were intermediate values. It looks like you're using that to handle formatting them into your message, could always just define __str__ instead though obviously if you wanted to.


def __init__(self):
"""Initialize our validator."""
self.enforcing_unbalanced_braces = True
Copy link

Choose a reason for hiding this comment

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

Are you intentionally leaving this public? Will someone want to query one of these to find out whether it's enforcing things or not?

(Also could be a class attribute and ditch the method if you'd like)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We don't document it, so instead of adding an underscore, I just left it public. If people want to try to mutate it, then oh well. Turning this into a property with a setter will mean that people might try to assign weird values and it breaks chaining.

Returns the validator instance.
"""
self.enforcing_unbalanced_braces = False
return self
Copy link

Choose a reason for hiding this comment

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

I take it you intentionally like chaining, even in cases where the instance itself has been mutated?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I want to encourage chaining, yes. I've waffled back on having the instance be mutable or not, but in this case, I don't think it needs to be treated as immutable. I'm open to being convinced otherwise.

:raises: uritemplate.exceptions.InvalidTemplate
:raises: uritemplate.exceptions.UnbalancedBraces
"""
if self.enforcing_unbalanced_braces:
Copy link

Choose a reason for hiding this comment

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

Could get rid of this if if you wanted by instead having enforce_unbalanced_braces be a function rather than a boolean (and making it do nothing when you disable validation), although whether you'd do that probably depends heavily on your opinion above (about whether someone should want to write if somevalidator.enforcing_unbalanced_braces in external code bases)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

(about whether someone should want to write if somevalidator.enforcing_unbalanced_braces in external code bases)

I don't think it would hurt if someone wanted to print out the configuration in a log message for themselves, especially if their validation logic is somewhat dynamic.



def _enforce_balanced_braces(template):
uri = template.uri
Copy link

Choose a reason for hiding this comment

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

I still haven't read the spec unfortunately, so apologies, but is this actually enough? E.g., is }{}{}{ valid? Or do the braces need to be properly nested too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That is not valid, no. I'll have to work on some better validation for that situation.

Julian added a commit to python-jsonschema/jsonschema that referenced this pull request Oct 29, 2017
Julian added a commit to python-jsonschema/jsonschema that referenced this pull request Oct 29, 2017
@Julian
Copy link

Julian commented Nov 5, 2017

@sigmavirus24 hey! Polite ping here, I think the only major issue here was about matching braces -- anything I can do to help there?

@kaka2507
Copy link

Hi guys, there is no change for this PR get merged?

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.

Provide an API for validating URI Templates
3 participants