add validaiton to parsed fields

This commit is contained in:
JONEMI19 2023-07-08 10:40:48 +00:00
parent 3ee26432fa
commit cac66df220
4 changed files with 19 additions and 29 deletions

View File

@ -238,7 +238,7 @@ And when you open your browser at <a href="http://127.0.0.1:8000/docs" class="ex
<img src="/img/tutorial/path-params/image01.png">
However, since the query parameters are decaled in the request as
However, since the query parameters are decaled in the request as
```
http://127.0.0.1:8000/query/mixed-type-params?query=1&foo=bar&foo=baz
@ -264,4 +264,4 @@ http://127.0.0.1:8000/query/mixed-type-params?query=1&foo=bar&foo=baz
}
}
```
As you can see the `query` parameter is added to every `Query` argument for which it is valid. This is because **FastAPI** does not know which `Query` argument you want to add the `query` parameter to, and `1` validates as an `int`, `str`, and `List[str]`. `foo` is only added to the `string_mapping` and `sequence_mapping_queries` arguments because it is not a valid `int`.
As you can see the `query` parameter is added to every `Query` argument for which it is valid. This is because **FastAPI** does not know which `Query` argument you want to add the `query` parameter to, and `1` validates as an `int`, `str`, and `List[str]`. `foo` is only added to the `string_mapping` and `sequence_mapping_queries` arguments because it is not a valid `int`.

View File

@ -1,4 +1,4 @@
from typing import Mapping, List
from typing import List, Mapping
from fastapi import FastAPI, Query
@ -13,8 +13,8 @@ def get_mixed_mapping_mixed_type_query_params(
sequence_mapping_queries: Mapping[str, List[int]] = Query({}),
):
return {
"query":query,
"string_mapping": mapping_query_str,
"mapping_query_int": mapping_query_int,
"sequence_mapping_queries": sequence_mapping_queries
}
"query": query,
"string_mapping": mapping_query_str,
"mapping_query_int": mapping_query_int,
"sequence_mapping_queries": sequence_mapping_queries,
}

View File

@ -694,7 +694,8 @@ def request_params_to_args(
value[key][index] = marker
for key in value:
value[key] = [x for x in value[key] if x != marker]
values[field.name] = value
v_, _ = field.validate(value, values, loc=loc)
values[field.name] = v_
elif (
isinstance(errors_, list)
and is_scalar_mapping_field(field)
@ -704,7 +705,8 @@ def request_params_to_args(
# remove all invalid parameters
for _, _, key in [error["loc"] for error in new_errors]:
value.pop(key)
values[field.name] = value
v_, _ = field.validate(value, values, loc=loc)
values[field.name] = v_
elif isinstance(errors_, list):
new_errors = _regenerate_error_with_loc(errors=errors_, loc_prefix=())
errors.extend(new_errors)

View File

@ -1,33 +1,21 @@
import pytest
from dirty_equals import IsDict
from fastapi.testclient import TestClient
from fastapi.utils import match_pydantic_error_url
@pytest.fixture(name="client")
def get_client():
from docs_src.query_params.tutorial006 import app
from docs_src.query_params.tutorial007_py310 import app
c = TestClient(app)
return c
def test_foo_needy_very(client: TestClient):
response = client.get("/items/foo?needy=very")
response = client.get("/query/mixed-type-params?query=1&query=2&foo=bar&foo=baz")
assert response.status_code == 200
assert response.json() == {
"query": 1,
"string_mapping": {
"query": "1",
"foo": "baz"
},
"mapping_query_int": {
"query": 1
},
"sequence_mapping_queries": {
"query": [
"1"
],
"foo": []
}
}
"query": 2,
"string_mapping": {"query": "2", "foo": "baz"},
"mapping_query_int": {"query": 2},
"sequence_mapping_queries": {"query": [1, 2], "foo": []},
}