question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Query(regex) does not work for date types

See original GitHub issue

Describe the bug I would like to be able to make some data filter by GET parameter which is date

/api/foo?from_date=2019-01-01

And I want specifically it to be in YYYY-MM-DD format

To Reproduce Steps to reproduce the behavior: 1. I made the following parameter with query and regex: def filter_order(from_date: date = Query(..., regex=r'\d{4}-\d{2}-\d{2}')

Expected behavior when i query /api/foo?from_date=1 I expect a validation error

instead my date is valid and equal to date(1970, 1, 1)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
tiangolocommented, Feb 12, 2020

@vitalik if you use a date or datetime type, then Pydantic uses it to handle data parsing. And anything you pass to Query() is used only for documentation.

If you use str, then the regex in Query(..., regex="") is used for documentation AND regex.

If you want to have something constrained to some types of dates only, you could declare the parameter as str with the regex. And then if you want to have a date from it, you could put that in a dependency.

E.g.

from datetime import datetime, date

from fastapi import FastAPI, Query, Depends

app = FastAPI()


def strict_date(
    from_date: str = Query(..., regex=r"\d{4}-\d{2}-\d{2}", format="date")
) -> date:
    dt = datetime.strptime(from_date, "%Y-%m-%d")
    return dt.date()


@app.get("/")
def filter_order(from_date: date = Depends(strict_date)):
    return from_date.isoformat()
0reactions
github-actions[bot]commented, Feb 25, 2020

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is my regular expression for a valid date not working?
Currently, I am creating my own regular expression using JavaScript to test for a date in the format of MM-DD-YYYY. Here is my...
Read more >
BigQuery Regex and Pattern Matching: 6 Major Things
2) BigQuery Regex: RegexP_EXTRACT​​ If the expression doesn't find any match, it returns NULL. RegexP_EXTRACT returns an error if:
Read more >
String functions | BigQuery - Google Cloud
In the following query, an error is thrown because the search value cannot be a literal NULL . ... DATE, FORMAT('%t', date '2015-09-01'),...
Read more >
GREL functions - OpenRefine
If a function can take more than one kind of data as input or can output ... Takes any value type (string, number,...
Read more >
Regular expressions in grep ( regex ) with examples - nixCraft
How do I use grep and regular expressions (regex)to search for text/ ... Regular Expressions is nothing but a pattern to match for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found