0

Currently i am working on a sample project using pydantic and starlette. This is code from main file:

async def submit(request):
response = await request.json()
resume = Resume(**response)
JsonResponse({"Hello":"World"})

Code from Validation file:

class Basics(BaseModel):
name: str
label: str
image: str
email: str
phone: str
url: str
summary: str
location: Location
profiles: List[Profiles]

@validator('email')
def email_validation(cls, v):
    regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
    if (re.fullmatch(regex, v)):
        return True
    else: 
        raise ValueError("Email not valid")

Class Basics is a child class of class Resume. When the regex doesnt match it correctly raises a value error and internal server error is displayed in postman. But how do I return this error or some statement to postman other and prevent internal server error?

  • Is your `async def submit` function a route function? And is it intentional that it has no `return` statement. It only has the `JsonResponse({"Hello":"World"})` line which doesn't do anything. It is something meant to be `return`-ed as a response of a route function. – Gino Mempin Apr 27 '22 at 12:49
  • Postman here is irrelevant. An internal server error means your server-side code is doing something wrong. Normally, validation errors are returned as part of the response. You can probably replicate the internal error with `curl` or `requests` or other API testing tool. – Gino Mempin Apr 27 '22 at 12:52

0 Answers0