Verify if the route is valid, and throw an error if not. Args: route: The route that need to be checked Raises: ValueError: If the route is invalid.
(route: str)
| 10 | |
| 11 | |
| 12 | def verify_route_validity(route: str) -> None: |
| 13 | """Verify if the route is valid, and throw an error if not. |
| 14 | |
| 15 | Args: |
| 16 | route: The route that need to be checked |
| 17 | |
| 18 | Raises: |
| 19 | ValueError: If the route is invalid. |
| 20 | """ |
| 21 | route_parts = route.removeprefix("/").split("/") |
| 22 | for i, part in enumerate(route_parts): |
| 23 | if constants.RouteRegex.SLUG.fullmatch(part): |
| 24 | continue |
| 25 | if not part.startswith("[") or not part.endswith("]"): |
| 26 | msg = ( |
| 27 | f"Route part `{part}` is not valid. Reflex only supports " |
| 28 | "alphabetic characters, underscores, and hyphens in route parts. " |
| 29 | ) |
| 30 | raise ValueError(msg) |
| 31 | if part.startswith(("[[...", "[...")): |
| 32 | if part != constants.RouteRegex.SPLAT_CATCHALL: |
| 33 | msg = f"Catchall pattern `{part}` is not valid. Only `{constants.RouteRegex.SPLAT_CATCHALL}` is allowed." |
| 34 | raise ValueError(msg) |
| 35 | if i != len(route_parts) - 1: |
| 36 | msg = f"Catchall pattern `{part}` must be at the end of the route." |
| 37 | raise ValueError(msg) |
| 38 | continue |
| 39 | if part.startswith("[["): |
| 40 | if constants.RouteRegex.OPTIONAL_ARG.fullmatch(part): |
| 41 | continue |
| 42 | msg = ( |
| 43 | f"Route part `{part}` with optional argument is not valid. " |
| 44 | "Reflex only supports optional arguments that start with an alphabetic character or underscore, " |
| 45 | "followed by alphanumeric characters or underscores." |
| 46 | ) |
| 47 | raise ValueError(msg) |
| 48 | if not constants.RouteRegex.ARG.fullmatch(part): |
| 49 | msg = ( |
| 50 | f"Route part `{part}` with argument is not valid. " |
| 51 | "Reflex only supports argument names that start with an alphabetic character or underscore, " |
| 52 | "followed by alphanumeric characters or underscores." |
| 53 | ) |
| 54 | raise ValueError(msg) |
| 55 | |
| 56 | |
| 57 | def get_route_args(route: str) -> dict[str, str]: |