|
1 |
| -from typing import Optional |
2 | 1 | import re
|
3 | 2 | from flask.wrappers import Request
|
4 | 3 |
|
5 | 4 |
|
6 |
| -def validate_int(req: Request, field: str, *, default: int = 0, required: bool = False) -> int: |
7 |
| - """Validate an integer, returns the integer if valid, otherwise the default. |
8 |
| -
|
9 |
| - Raises ValueError if the field is required and the valid is not valid. |
10 |
| - """ |
| 5 | +def validate_int(req: Request, field: str, default: int = 0) -> int: |
| 6 | + """Validate an integer, returns the integer if valid, otherwise the default.""" |
11 | 7 | value = req.args.get(field, "")
|
12 |
| - if value == "" and required: |
13 |
| - raise ValueError(f"Required parameter '{field}' is missing") |
14 | 8 | try:
|
15 | 9 | return int(value)
|
16 | 10 | except ValueError:
|
17 |
| - if required: |
18 |
| - raise ValueError(f"{field} expects an integer but got '{value}'") |
19 | 11 | return default
|
20 | 12 |
|
21 | 13 |
|
22 |
| -def validate_color( |
23 |
| - req: Request, field: str, *, default: str = "#ffffff", required: bool = False |
24 |
| -) -> str: |
25 |
| - """Validate a color, returns the color if valid hex code (3, 4, 6, or 8 characters), otherwise the default. |
26 |
| -
|
27 |
| - Raises ValueError if the field is required and the valid is not valid. |
28 |
| - """ |
| 14 | +def validate_color(req: Request, field: str, default: str = "#ffffff") -> str: |
| 15 | + """Validate a color, returns the color if it's a valid hex code (3, 4, 6, or 8 characters), otherwise the default.""" |
29 | 16 | value = req.args.get(field, "")
|
30 |
| - if value == "" and required: |
31 |
| - raise ValueError(f"Required parameter '{field}' is missing") |
32 | 17 | hex_digits = re.sub(r"[^a-fA-F0-9]", "", value)
|
33 | 18 | if len(hex_digits) not in (3, 4, 6, 8):
|
34 |
| - if required: |
35 |
| - raise ValueError(f"{field} expects a hex color but got '{value}'") |
36 | 19 | return default
|
37 | 20 | return f"#{hex_digits}"
|
38 | 21 |
|
39 | 22 |
|
40 |
| -def validate_video_id( |
41 |
| - req: Request, field: str, *, default: str = "", required: bool = False |
42 |
| -) -> str: |
43 |
| - """Validate a video ID, returns the video ID if valid, otherwise the default. |
| 23 | +def validate_video_id(req: Request, field: str) -> str: |
| 24 | + """Validate a video ID, returns the video ID if valid. |
44 | 25 |
|
45 |
| - Raises ValueError if the field is required and the valid is not valid. |
46 |
| - """ |
| 26 | + Raises ValueError if the field is not provided or fails the validation regex.""" |
47 | 27 | value = req.args.get(field, "")
|
48 |
| - if value == "" and required: |
| 28 | + if value == "": |
49 | 29 | raise ValueError(f"Required parameter '{field}' is missing")
|
50 | 30 | if not re.match(r"^[a-zA-Z0-9_-]+$", value):
|
51 |
| - if required: |
52 |
| - raise ValueError(f"{field} expects a video ID but got '{value}'") |
53 |
| - return default |
| 31 | + raise ValueError(f"{field} expects a video ID but got '{value}'") |
54 | 32 | return value
|
55 | 33 |
|
56 | 34 |
|
57 |
| -def validate_string(req: Request, field: str, *, required: bool = False) -> str: |
58 |
| - """Validate a string, returns the string if valid, otherwise the default. |
59 |
| -
|
60 |
| - Raises ValueError if the field is required and the valid is not valid. |
61 |
| - """ |
62 |
| - value = req.args.get(field, "") |
63 |
| - if value == "" and required: |
64 |
| - raise ValueError(f"Required parameter '{field}' is missing") |
65 |
| - return value |
| 35 | +def validate_string(req: Request, field: str, default: str = "") -> str: |
| 36 | + """Validate a string, returns the string if valid, otherwise the default.""" |
| 37 | + return req.args.get(field, default) |
0 commit comments