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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/test_serp.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ def test_metadata():
{"type": "null"},
],
"description": "Input 1 search query per line (e.g. foo bar).",
"pattern": r"(.|\r?\n)*\S+(.|\r?\n)*",
"title": "Search Queries",
"widget": "textarea",
},
Expand Down
1 change: 1 addition & 0 deletions zyte_spider_templates/spiders/serp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).

},
)

Expand Down