Drupal provides regular expression validation against route parameters. If the parameter fails the regular expression validation, a 404 page will be returned. Using an example route, we can add the validation to ensure that only alphabetical characters are used in the route parameter:
mymodule.cats:
path: '/cat/{name}'
defaults:
_controller: '\Drupal\mymodule\Controller\MyPageController::cats'
requirements:
_permission: 'access content'
name: '[a-zA-z]+'
Under the requirements key, you can add a new value that matches the name of the placeholder. You can then set it to have the value of the regular expression you would like to use. This would prevent c@ts or cat! from being valid parameters.