Skip to content

Commit c1a1291

Browse files
committed
Use objects instead of dictionaries for testing
Signed-off-by: Tushar Goel <[email protected]>
1 parent 723a68c commit c1a1291

File tree

3 files changed

+278
-179
lines changed

3 files changed

+278
-179
lines changed

src/packageurl/__init__.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -524,21 +524,38 @@ def to_string(self, encode: bool | None = True) -> str:
524524

525525
return "".join(purl)
526526

527-
def validate(self, strict: bool = False) -> list[str]:
527+
def validate(self, strict: bool = False) -> list:
528528
"""
529529
Validate this PackageURL object and return a list of validation error messages.
530530
"""
531-
from packageurl.validate import VALIDATORS_BY_TYPE
532-
533-
if self:
534-
try:
535-
validator_class = VALIDATORS_BY_TYPE.get(self.type)
536-
if not validator_class:
537-
return [f"Given type: {self.type} can not be validated"]
538-
messages = list(validator_class.validate(self, strict)) # type: ignore[no-untyped-call]
539-
return messages
540-
except NoRouteAvailable:
541-
return [f"Given type: {self.type} can not be validated"]
531+
from packageurl.validate import DEFINITIONS_BY_TYPE
532+
from packageurl.validate import ValidationMessage
533+
from packageurl.validate import ValidationSeverity
534+
535+
validator_class = DEFINITIONS_BY_TYPE.get(self.type)
536+
if not validator_class:
537+
return [ValidationMessage(
538+
severity=ValidationSeverity.ERROR,
539+
message=f"Unexpected purl type: expected {self.type!r}",
540+
)]
541+
return list(validator_class.validate(purl=self, strict=strict)) # type: ignore[no-untyped-call]
542+
543+
@classmethod
544+
def validate_string(cls, purl: str, strict: bool = False) -> list:
545+
"""
546+
Validate a PURL string and return a list of validation error messages.
547+
"""
548+
from packageurl.validate import ValidationMessage
549+
from packageurl.validate import ValidationSeverity
550+
551+
try:
552+
purl = cls.from_string(purl, normalize_purl=not strict)
553+
except ValueError as e:
554+
return [ValidationMessage(
555+
severity=ValidationSeverity.ERROR,
556+
message=str(e),
557+
)]
558+
return purl.validate(strict=strict)
542559

543560
@classmethod
544561
def from_string(cls, purl: str, normalize_purl: bool = True) -> Self:

0 commit comments

Comments
 (0)