diff --git a/metadata/fields/custom/license.py b/metadata/fields/custom/license.py index f70b2d21d..5661e92af 100644 --- a/metadata/fields/custom/license.py +++ b/metadata/fields/custom/license.py @@ -11,6 +11,9 @@ from typing import List, Tuple, Optional _THIS_DIR = os.path.abspath(os.path.dirname(__file__)) # The repo's root directory. _ROOT_DIR = os.path.abspath(os.path.join(_THIS_DIR, "..", "..", "..")) +# Bad delimiter characters. +BAD_DELIMITERS = ["/", ";", " and ", " or "] +BAD_DELIMITERS_REGEX = re.compile("|".join(re.escape(delimiter) for delimiter in BAD_DELIMITERS)) # Add the repo's root directory for clearer imports. sys.path.insert(0, _ROOT_DIR) @@ -92,6 +95,15 @@ class LicenseField(field_types.SingleLineTextField): if util.is_empty(license): return vr.ValidationError( reason=f"{self._name} has an empty value.") + if BAD_DELIMITERS_REGEX.search(license): + return vr.ValidationError( + reason=f"{self._name} contains a bad license separator. " + "Separate licenses by commas only.", + # Try and preemptively address the root cause of this behaviour, + # which is having multiple choices for a license. + additional=[f"When given a choice of licenses, chose the most " + "permissive one, do not list all options."] + ) if not allowed: not_allowlisted.append(license) diff --git a/metadata/tests/fields_test.py b/metadata/tests/fields_test.py index 78a8ff323..30f449309 100644 --- a/metadata/tests/fields_test.py +++ b/metadata/tests/fields_test.py @@ -123,10 +123,17 @@ class FieldValidationTest(unittest.TestCase): "APSL-2.0, MIT", "APSL-2.0 ,MIT", ], - error_values=["", "\n", ",", "Apache 2.0 ,"], + error_values=[ + "", + "\n", + ",", + "Apache 2.0 ,", + "Custom / MIT", + "Apache-2.0 and MIT", + "Apache-2.0; MIT; BSD-2-Clause", + ], warning_values=[ "Custom license", - "Custom / MIT", "Custom, MIT", ], )