Pydantic refuse to validate my schema #6750
-
DescriptionHi, I'm pretty new with Pydantic validation but it makes me think a lot to Golang structures. {
"target": "https://google.fr",
"link": "BHDH",
"extras": {
"code": "ABCDE"
}
}Here is my from typing import Optional, Any, Dict
from pydantic import BaseModel, HttpUrl, Json
class Link(BaseModel):
link: str
target: httpUrl
extras: Json[Dict[str, Any]]
print(Link(link='BHDH', target='https://google.com, extras='{"code": "ABCDE"}'))Here is the And this is the error I got: Thanks for your help :) Environment |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
This will work for you: curl -X POST "http://127.0.0.1:8000/" -H "accept: application/json" -H "Content-Type: application/json" -d "{\"link\":\"BHDH\",\"target\":\"https://google.fr\",\"extras\":\"{\\\"code\\\": \\\"ABCDE\\\"}\"}"Code to test: from fastapi import FastAPI
from typing import Optional, Any, Dict
from pydantic import BaseModel, HttpUrl, Json
class Link(BaseModel):
link: str
target: HttpUrl
extras: Json[Dict[str, Any]]
app = FastAPI()
@app.post("/")
async def home(link: Link):
return linkSwagger input: {
"link": "BHDH",
"target": "https://google.fr",
"extras": "{\"code\": \"ABCDE\"}"
}But... I do believe that you don't want to do this 😅 |
Beta Was this translation helpful? Give feedback.
-
|
@Kludex ahah indeed, I prefer to avoid the |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the help here @Kludex ! 👏 🙇 Thanks for reporting back and closing the issue @goldyfruit 👍
|
Beta Was this translation helpful? Give feedback.
This will work for you:
Code to test:
Swagger input:
{ "link": "BHDH", "target": "https://google.fr", "extras": "{\"code\": \"ABCDE\"}" }But... I do believe that you don't want to do this 😅
That whole…