Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor validation #307

Closed
armintaenzertng opened this issue Nov 22, 2022 · 2 comments
Closed

Refactor validation #307

armintaenzertng opened this issue Nov 22, 2022 · 2 comments
Assignees
Labels
validation An issue concerning the validation of SPDX documents

Comments

@armintaenzertng
Copy link
Collaborator

armintaenzertng commented Nov 22, 2022

We want a refactored validation using the new data model (available on https://github.com/spdx/tools-python/tree/refactor-python-tools). The main goal is to keep it completely separated from the parsing/writing layers.

Validation error messages could be instances of a class that tracks their context like this:

class ErrorMessage:
    element_type: SpdxElementType # an enum with values document, package, file, snippet, relationship, annotation, ...
    id: Optional[str] # not every type has an id or it might be missing
    validation_messages: List[str]

Validation can be performed by calling validation methods that return List[ErrorMessage]. Keeping them in separate classes allows for better unit testing by using mocks. The spdx version will be passed to validators that differ for different versions.

class DocumentValidator:
    spdx_version: str
    creation_info_validator = CreationInfoValidator()
    package_validator = PackageValidator(spdx_version)
    file_validator = FileValidator(spdx_version)
    ...

    def validate_document(self, document: Document) -> List[ErrorMessage]:
        error_messages = []
        error_messages.extend(creation_info_validator.validate_creation_info(document))
        error_messages.extend(package_validator.validate_packages(document.packages))
        ...
        return error_messages
class CreationInfoValidator()
    def validate_creation_info(self, document: Document) -> List[ErrorMessage]:
        error_messages = []
        spdx_id: Optional[str]

        ... # check and set the spdx_id
        
        if document.namespace is None:
            error_messages.append(ErrorMessage(SpdxElementType.Document, spdx_id, 'Missing required field "namespace."'))
@armintaenzertng armintaenzertng added the validation An issue concerning the validation of SPDX documents label Nov 22, 2022
@armintaenzertng armintaenzertng self-assigned this Nov 23, 2022
@armintaenzertng
Copy link
Collaborator Author

armintaenzertng commented Dec 12, 2022

Completion overview:

@armintaenzertng
Copy link
Collaborator Author

This has been implemented in 0.8.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
validation An issue concerning the validation of SPDX documents
Projects
None yet
Development

No branches or pull requests

1 participant