Skip to content

Commit

Permalink
feat: add color code validation
Browse files Browse the repository at this point in the history
  • Loading branch information
gsinghjay committed Jan 2, 2025
1 parent 001a3c9 commit 32c497f
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app/api/v1/qr_code.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""QR Code generation endpoint module."""

from fastapi import APIRouter, HTTPException
from pydantic import BaseModel, HttpUrl, Field
from pydantic import BaseModel, HttpUrl, Field, validator
import qrcode
from io import BytesIO
import base64
import re

router = APIRouter()

Expand All @@ -28,6 +29,13 @@ class QRCodeRequest(BaseModel):
description="Border size in boxes",
)

@validator("fill_color", "background_color")
def validate_color(cls, v):
"""Validate color hex codes."""
if not re.match(r"^#[0-9A-Fa-f]{6}$", v):
raise ValueError("Color must be a valid hex code (e.g., #FF0000)")
return v


@router.post("/generate")
async def generate_qr_code(request: QRCodeRequest):
Expand Down

0 comments on commit 32c497f

Please sign in to comment.