Skip to content

Validate Project-URL metadata #2667

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

Merged
merged 5 commits into from
Dec 12, 2017
Merged

Validate Project-URL metadata #2667

merged 5 commits into from
Dec 12, 2017

Conversation

ewdurbin
Copy link
Member

RE: #2666

for datum in field.data:
_validate_project_url(datum)
if isinstance(field.data, str):
_validate_project_url(field.data) # Or raise ValidationError?
Copy link
Member Author

Choose a reason for hiding this comment

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

@dstufft i'm not sure if we want to just accept a string or not here.

Copy link
Member

Choose a reason for hiding this comment

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

Project-URL is a multiple-use field, so this should always be a list. I think it should just fail validation if it isn't.

Copy link
Member Author

Choose a reason for hiding this comment

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

@ewdurbin ewdurbin requested a review from dstufft December 12, 2017 15:13
@di
Copy link
Member

di commented Dec 12, 2017

Wouldn't this be an issue for any ListField-type field, not just project_urls?

@ewdurbin
Copy link
Member Author

yeah, let's see if I can put a validator on the ListField itself, just now realized we own that class :-D

@di
Copy link
Member

di commented Dec 12, 2017

I think we might want a failing test like this on TestListField:

@pytest.mark.parametrize(
    ("value", "expected"),
    [
        ("", []),
        ("foobar", ["foobar"]),
    ]
)
def test_coerce_string_into_list(self, value, expected):
    class MyForm(Form):
        test = legacy.ListField()

    form = MyForm(MultiDict({'test': value}))

    assert form.test.data == expected

The issue seems to be that process_formdata isn't throwing away values that end up as empty strings.

@ewdurbin
Copy link
Member Author

yep! I'm retooling to give ListField a pre_validate method that should do the job nicely.

@ewdurbin
Copy link
Member Author

@di good call! your approach ended up being better and leading me to the simple solution 742c240#diff-dbf9e293f2b07f35422689148e11b039R267

I left a bunch of empty string and validation test cases in place just to keep us from regressing!

@@ -264,7 +264,7 @@ def _construct_dependencies(form, types):
class ListField(wtforms.Field):

def process_formdata(self, valuelist):
self.data = [v.strip() for v in valuelist]
self.data = [v.strip() for v in valuelist if v]
Copy link
Member

Choose a reason for hiding this comment

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

This still could give us empty strings if one of the values is just whitespace:

>>> [v.strip() for v in ['', ' ', 'a'] if v]
['', 'a']

@ewdurbin ewdurbin merged commit d12b7c1 into master Dec 12, 2017
@ewdurbin ewdurbin deleted the issue-2666 branch December 12, 2017 20:21
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