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 3 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 97.14%. Comparing base (37fcbee) to head (ac59dc3).

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #74   +/-   ##
=======================================
  Coverage   97.14%   97.14%           
=======================================
  Files          16       16           
  Lines         946      946           
=======================================
  Hits          919      919           
  Misses         27       27           
Files with missing lines Coverage Δ
zyte_spider_templates/spiders/serp.py 85.18% <ø> (ø)

@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).

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.

2 participants