@@ -510,5 +510,47 @@ def build_spdx_license_expression(license_expression, licensing=None):
510510 """
511511 if not licensing :
512512 licensing = get_licensing ()
513+ validate_spdx_license_keys (license_expression = license_expression , licensing = licensing )
513514 parsed = licensing .parse (license_expression )
514515 return parsed .render (template = '{symbol.wrapped.spdx_license_key}' )
516+
517+
518+ def validate_spdx_license_keys (license_expression , licensing ):
519+ """
520+ Raise InvalidLicenseKeyError for the cases where the there is no corresponding :
521+
522+ """
523+ from licensedcode .models import load_licenses
524+
525+ license_keys = licensing .license_keys (license_expression )
526+ license_db = get_licenses_db ()
527+
528+ messages = []
529+
530+ for key in license_keys :
531+ if not type (key ) == str :
532+ msg = f"Invalid license key: { key } of type { type (key )} , license key should be a string"
533+ messages .append (msg )
534+
535+ lic = license_db .get (key , None )
536+ if not lic :
537+ licenses = load_licenses (with_deprecated = True )
538+ if licenses .get (key , None ):
539+ msg = f"License key: { key } is deprecated license key in LicenseDB"
540+ else :
541+ msg = f"License key: { key } is not a valid license key from LicenseDB"
542+ messages .append (msg )
543+
544+ parsed = licensing .parse (key )
545+ try :
546+ parsed .render (template = '{symbol.wrapped.spdx_license_key}' )
547+ except AttributeError :
548+ messages .append (msg )
549+ pass
550+
551+ if messages :
552+ raise InvalidLicenseKeyError (messages )
553+
554+
555+ class InvalidLicenseKeyError (Exception ):
556+ pass
0 commit comments