# Path: lepo/excs.py
# class InvalidParameterDefinition(ImproperlyConfigured):
#     pass
# 
# class MissingHandler(ValueError):
#     pass
# 
# class RouterValidationError(Exception):
#     def __init__(self, error_map):
#         self.errors = error_map
#         self.description = '\n'.join(f'{operation.id}: {error}' for (operation, error) in self.flat_errors)
#         super().__init__(f'Router validation failed:\n{self.description}')
# 
#     @property
#     def flat_errors(self):
#         for operation, errors in sorted(self.errors.items(), key=str):
#             for error in errors:
#                 yield (operation, error)


from collections import defaultdict
from lepo.excs import InvalidParameterDefinition, MissingHandler, RouterValidationError



def validate_router(router):
    errors = defaultdict(list)
    for path in router.get_paths():
        for operation in path.get_operations():
            # Check the handler exists.
            try:
                router.get_handler(operation.id)
            except MissingHandler as e:
                errors[operation].append(e)

            for param in operation.parameters:
                if param.location == 'header' and '_' in param.name:  # See https://github.com/akx/lepo/issues/23
                    ipd = InvalidParameterDefinition(
                        f'{param.name}: Header parameter names may not contain underscores (Django bug 25048)'
                    )
                    errors[operation].append(ipd)

    if errors:

# Path: lepo/excs.py
# class InvalidParameterDefinition(ImproperlyConfigured):
#     pass
# 
# class MissingHandler(ValueError):
#     pass
# 
# class RouterValidationError(Exception):
#     def __init__(self, error_map):
#         self.errors = error_map
#         self.description = '\n'.join(f'{operation.id}: {error}' for (operation, error) in self.flat_errors)
#         super().__init__(f'Router validation failed:\n{self.description}')
# 
#     @property
#     def flat_errors(self):
#         for operation, errors in sorted(self.errors.items(), key=str):
#             for error in errors:
#                 yield (operation, error)


# Path: lepo/validate.py
from collections import defaultdict
from lepo.excs import InvalidParameterDefinition, MissingHandler, RouterValidationError




def validate_router(router):
    errors = defaultdict(list)
    for path in router.get_paths():
        for operation in path.get_operations():
            # Check the handler exists.
            try:
                router.get_handler(operation.id)
            except MissingHandler as e:
                errors[operation].append(e)

            for param in operation.parameters:
                if param.location == 'header' and '_' in param.name:  # See https://github.com/akx/lepo/issues/23
                    ipd = InvalidParameterDefinition(
                        f'{param.name}: Header parameter names may not contain underscores (Django bug 25048)'
                    )
                    errors[operation].append(ipd)

    if errors:

        raise RouterValidationError(errors)