Repository: oppia/oppia
File: core/domain/app_feedback_report_domain.py (Python)

Code before:
            raise utils.ValidationError(
                'No device %s locale code supplied.' % locale_type
            )
        if not isinstance(locale_code, str):
            raise utils.ValidationError(
                'The device\'s %s locale code must be an string, '
                'received: %r.' % (locale_type, locale_code)
            )
        if not cls._match_locale_code_string(locale_code):
            raise utils.ValidationError(
                'The device\'s %s locale code is not a valid string, '
                'received: %s.' % (locale_type, locale_code)
            )

    @classmethod
    def _match_locale_code_string(cls, code: str) -> Optional[Match[str]]:
        """Helper that checks whether the given locale code is a valid code.

        Args:
            code: str. The device's country locale code that sent the report.

        Returns:
            bool. Whether the given code is valid. Valid codes are alphabetic
            strings (typically 2 letters long) that may optionally include a
            hyphen followed by another alphabetic string.
        """
        regex_string = r'^[a-z]+[-]?[a-z]+$'
        return re.compile(regex_string).match(code.lower())

    @classmethod
    def require_valid_sdk_version(cls, sdk_version: int) -> None:
        """Checks that the Android device's SDK version is a positive integer.

        Args:
            sdk_version: int. The SDK version of the device sending this report.

        Raises:
            ValidationError. The given SDK version  is not valid.
        """
        if sdk_version is None:
            raise utils.ValidationError('No SDK version supplied.')
        if not isinstance(sdk_version, int):
            raise utils.ValidationError(
                'SDK version must be an int, received: %r.' % sdk_version
            )
        if sdk_version < (
            app_feedback_report_constants.MINIMUM_ANDROID_SDK_VERSION
        ):
            raise utils.ValidationError(
                'Invalid SDK version, received: %s.' % sdk_version
            )

Review comment (suggestion):
Per the PR description, we want to validate language codes. Must they be BCP-47 codes? If that is not being verified elsewhere, then strings like:
"----"
"1-2-3"
"abc---xyz"
would still be considered valid? 

However, if the initial validation was already done, then this regex should be correct.

Code after:
            raise utils.ValidationError(
                'No device %s locale code supplied.' % locale_type
            )
        if not isinstance(locale_code, str):
            raise utils.ValidationError(
                'The device\'s %s locale code must be an string, '
                'received: %r.' % (locale_type, locale_code)
            )
        if not cls._match_locale_code_string(locale_code):
            raise utils.ValidationError(
                'The device\'s %s locale code is not a valid string, '
                'received: %s.' % (locale_type, locale_code)
            )

    @classmethod
    def _match_locale_code_string(cls, code: str) -> Optional[Match[str]]:
        """Helper that checks whether the given locale code is a valid code.

        Args:
            code: str. The device's country locale code that sent the report.

        Returns:
            bool. Whether the given code is valid. Valid codes contain only
            letters, digits, and hyphens.
        """
        regex_string = r'^[a-zA-Z0-9-]+$'
        return re.compile(regex_string).match(code.lower())

    @classmethod
    def require_valid_sdk_version(cls, sdk_version: int) -> None:
        """Checks that the Android device's SDK version is a positive integer.

        Args:
            sdk_version: int. The SDK version of the device sending this report.

        Raises:
            ValidationError. The given SDK version  is not valid.
        """
        if sdk_version is None:
            raise utils.ValidationError('No SDK version supplied.')
        if not isinstance(sdk_version, int):
            raise utils.ValidationError(
                'SDK version must be an int, received: %r.' % sdk_version
            )
        if sdk_version < (
            app_feedback_report_constants.MINIMUM_ANDROID_SDK_VERSION
        ):
            raise utils.ValidationError(
                'Invalid SDK version, received: %s.' % sdk_version
            )