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 a non-whitespace pattern to SearchQueriesParam. #74

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

Conversation

wRAR
Copy link
Member

@wRAR wRAR commented Oct 18, 2024

No description provided.

@wRAR wRAR marked this pull request as draft October 18, 2024 10:19
Copy link

codecov bot commented Oct 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.93%. Comparing base (3f0a47c) to head (405fecc).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #74      +/-   ##
==========================================
+ Coverage   95.36%   95.93%   +0.57%     
==========================================
  Files          26       26              
  Lines        2609     2609              
==========================================
+ Hits         2488     2503      +15     
+ Misses        121      106      -15     
Files with missing lines Coverage Δ
zyte_spider_templates/spiders/serp.py 94.69% <ø> (+0.88%) ⬆️

... and 3 files with indirect coverage changes

@Gallaecio
Copy link
Contributor

Wouldn’t this prevent multi-word queries, like searching for “foo bar”?

@wRAR
Copy link
Member Author

wRAR commented Oct 18, 2024

Hmm, apparently it tries to match the entire value even without ^$

tests/test_serp.py Outdated Show resolved Hide resolved
@wRAR wRAR marked this pull request as ready for review October 23, 2024 11:23
@@ -18,6 +18,7 @@ class SearchQueriesParam(BaseModel):
description="Input 1 search query per line (e.g. foo bar).",
json_schema_extra={
"widget": "textarea",
"pattern": r"(.|\r?\n)*\S+(.|\r?\n)*",
Copy link
Contributor

@Gallaecio Gallaecio Oct 24, 2024

Choose a reason for hiding this comment

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

When defining the regular expressions, it's important to note that the string is considered valid if the expression matches anywhere within the string.
https://json-schema.org/understanding-json-schema/reference/string#regexp

So, I’m thinking this should do the trick:

Suggested change
"pattern": r"(.|\r?\n)*\S+(.|\r?\n)*",
"pattern": r"(.|\r?\n)*?\S",

Or maybe even (your original?):

Suggested change
"pattern": r"(.|\r?\n)*\S+(.|\r?\n)*",
"pattern": r"\S",

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, the initial one didn't work on a multi-word input (neither does r"\S") so I don't know if the quote is true. Or maybe the frontend code uses it incorrectly.

Copy link
Contributor

Choose a reason for hiding this comment

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

Or maybe the frontend code uses it incorrectly.

Oh, right, the frontend is most likely not using some JSON schema library, and instead uses its own implementation, which might be assuming an initial ^ (like Python’s re.match vs re.search).

Copy link
Contributor

Choose a reason for hiding this comment

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

This pattern is not super straightforward - would it be possible to add a test for it? I.e. check that the validation passes with input A, and fails with input B?

Copy link
Contributor

Choose a reason for hiding this comment

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

Does \S pass those tests?

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