mirror of https://github.com/tiangolo/fastapi.git
add validaiton to parsed fields
This commit is contained in:
parent
3ee26432fa
commit
cac66df220
|
|
@ -1,4 +1,4 @@
|
||||||
from typing import Mapping, List
|
from typing import List, Mapping
|
||||||
|
|
||||||
from fastapi import FastAPI, Query
|
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({}),
|
sequence_mapping_queries: Mapping[str, List[int]] = Query({}),
|
||||||
):
|
):
|
||||||
return {
|
return {
|
||||||
"query":query,
|
"query": query,
|
||||||
"string_mapping": mapping_query_str,
|
"string_mapping": mapping_query_str,
|
||||||
"mapping_query_int": mapping_query_int,
|
"mapping_query_int": mapping_query_int,
|
||||||
"sequence_mapping_queries": sequence_mapping_queries
|
"sequence_mapping_queries": sequence_mapping_queries,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -694,7 +694,8 @@ def request_params_to_args(
|
||||||
value[key][index] = marker
|
value[key][index] = marker
|
||||||
for key in value:
|
for key in value:
|
||||||
value[key] = [x for x in value[key] if x != marker]
|
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 (
|
elif (
|
||||||
isinstance(errors_, list)
|
isinstance(errors_, list)
|
||||||
and is_scalar_mapping_field(field)
|
and is_scalar_mapping_field(field)
|
||||||
|
|
@ -704,7 +705,8 @@ def request_params_to_args(
|
||||||
# remove all invalid parameters
|
# remove all invalid parameters
|
||||||
for _, _, key in [error["loc"] for error in new_errors]:
|
for _, _, key in [error["loc"] for error in new_errors]:
|
||||||
value.pop(key)
|
value.pop(key)
|
||||||
values[field.name] = value
|
v_, _ = field.validate(value, values, loc=loc)
|
||||||
|
values[field.name] = v_
|
||||||
elif isinstance(errors_, list):
|
elif isinstance(errors_, list):
|
||||||
new_errors = _regenerate_error_with_loc(errors=errors_, loc_prefix=())
|
new_errors = _regenerate_error_with_loc(errors=errors_, loc_prefix=())
|
||||||
errors.extend(new_errors)
|
errors.extend(new_errors)
|
||||||
|
|
|
||||||
|
|
@ -1,33 +1,21 @@
|
||||||
import pytest
|
import pytest
|
||||||
from dirty_equals import IsDict
|
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
from fastapi.utils import match_pydantic_error_url
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="client")
|
@pytest.fixture(name="client")
|
||||||
def get_client():
|
def get_client():
|
||||||
from docs_src.query_params.tutorial006 import app
|
from docs_src.query_params.tutorial007_py310 import app
|
||||||
|
|
||||||
c = TestClient(app)
|
c = TestClient(app)
|
||||||
return c
|
return c
|
||||||
|
|
||||||
|
|
||||||
def test_foo_needy_very(client: TestClient):
|
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.status_code == 200
|
||||||
assert response.json() == {
|
assert response.json() == {
|
||||||
"query": 1,
|
"query": 2,
|
||||||
"string_mapping": {
|
"string_mapping": {"query": "2", "foo": "baz"},
|
||||||
"query": "1",
|
"mapping_query_int": {"query": 2},
|
||||||
"foo": "baz"
|
"sequence_mapping_queries": {"query": [1, 2], "foo": []},
|
||||||
},
|
|
||||||
"mapping_query_int": {
|
|
||||||
"query": 1
|
|
||||||
},
|
|
||||||
"sequence_mapping_queries": {
|
|
||||||
"query": [
|
|
||||||
"1"
|
|
||||||
],
|
|
||||||
"foo": []
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue