Skip to content

Commit

Permalink
chore(email-validator): only business emails are allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
meysam81 committed Nov 21, 2024
1 parent 8faba01 commit 7939bd0
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions services/email-validator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,58 @@ async def index():
)


@app.get("/validate")
async def validate(email: str):
"""
Sending the following validation error in case of invalid email:
{
"messages": [
{
"instance_ptr": "#/traits/foo/bar", // Points to the field that failed validation.
"messages": [
{
"id": 123, // Unique numeric ID of the error that helps the frontend to interpret this message, doesn't have to be unique.
"text": "field must be longer than 12 characters",
"type": "error", // One of "error", "info", "success".
"context": {
// Contextual information about the error. Is schemaless, and can be used to provide additional information to the frontend.
// Typically used to provide fields that are used in internationalization messages.
"value": "short value",
"any": "additional information"
}
}
]
}
]
}
"""
@app.post("/validate")
async def validate(request: fastapi.Request):
body = await request.json()
email = body.get("email")

valid = await is_valid_domain(email) and await is_valid_email(email)

error_payload = {
"messages": [
{
"instance_ptr": "#/traits/email",
"messages": [
{
"id": 1,
"text": "Only business emails are allowed.",
"type": "error",
"context": {"value": email},
}
],
}
]
}

if valid:
content = {"valid": True, "msg": "All good."}
status_code = http.HTTPStatus.OK
else:
content = {"valid": False, "msg": "Non-business email not allowed."}
content = error_payload
status_code = http.HTTPStatus.BAD_REQUEST

return fastapi.responses.JSONResponse(content=content, status_code=status_code)
Expand Down

0 comments on commit 7939bd0

Please sign in to comment.