def _validate_package_type()

in samtranslator/model/sam_resources.py [0:0]


    def _validate_package_type(self, lambda_function: LambdaFunction) -> None:
        """
        Validates Function based on the existence of Package type
        """
        packagetype = lambda_function.PackageType or ZIP

        if packagetype not in [ZIP, IMAGE]:
            raise InvalidResourceException(
                lambda_function.logical_id,
                f"PackageType needs to be `{ZIP}` or `{IMAGE}`",
            )

        def _validate_package_type_zip() -> None:
            if not all([lambda_function.Runtime, lambda_function.Handler]):
                raise InvalidResourceException(
                    lambda_function.logical_id,
                    f"Runtime and Handler needs to be present when PackageType is of type `{ZIP}`",
                )

            if any([lambda_function.Code.get("ImageUri", False), lambda_function.ImageConfig]):
                raise InvalidResourceException(
                    lambda_function.logical_id,
                    f"ImageUri or ImageConfig cannot be present when PackageType is of type `{ZIP}`",
                )

        def _validate_package_type_image() -> None:
            if any([lambda_function.Handler, lambda_function.Runtime, lambda_function.Layers]):
                raise InvalidResourceException(
                    lambda_function.logical_id,
                    f"Runtime, Handler, Layers cannot be present when PackageType is of type `{IMAGE}`",
                )
            if not lambda_function.Code.get("ImageUri"):
                raise InvalidResourceException(
                    lambda_function.logical_id,
                    f"ImageUri needs to be present when PackageType is of type `{IMAGE}`",
                )

        _validate_per_package_type = {ZIP: _validate_package_type_zip, IMAGE: _validate_package_type_image}

        # Call appropriate validation function based on the package type.
        return _validate_per_package_type[packagetype]()