~sthagen/csaf#6: 
Pydantic V2 small issue

Hi Stefan,

When testing the csaf module with pydantic V2 we noticed the following:

on this line in our code:

from csaf.document import Tracking

we get an error:

AttributeError: 'FieldInfo' object has no attribute 'min_length'

We think this can be resolved by removing line #43 from csaf/document.py (https://git.sr.ht/~sthagen/csaf/tree/default/item/csaf/document.py#L43)

Jacco

Status
RESOLVED FIXED
Submitter
~jaccol
Assigned to
Submitted
1 year, 6 days ago
Updated
7 months ago
Labels
Bug

~sthagen 1 year, 6 days ago*

Thank you for reporting the bug (implementation error).

The fix though should not be removing the constraint, but instead implement the correct one (https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#321121-document-property---tracking---aliases).

The min_length attribute in Pydantic is only valid for strings and aliases is a list (JSON array) of Alias elements.

So, I will add a validator "function" instead to the class. Something like:

class Tracking(BaseModel):
    # ...
    @classmethod
    @no_type_check
    @field_validator('aliases')
    def check_when_present_not_empty(cls, v):
        if not v:
            raise ValueError('optional element if present must not be empty')
        return v

~sthagen REPORTED FIXED 1 year, 6 days ago

Fixed with version 2023.11.27

~jaccol 1 year, 5 days ago

Thanks!

Works for us.

Register here or Log in to comment, or comment via email.