Skip to content

Commit

Permalink
FIX: Replace invalid JSON schema types
Browse files Browse the repository at this point in the history
- use of `Path` and `uuid` in model types yields an invalid JSON schema;
- replaced both types in model classes with `str`;
- added `str(path)` and hex representation of `uuid` in the respective classes; and
- fixed tests.
  • Loading branch information
carlwilson committed Sep 25, 2024
1 parent 34343eb commit d5f8baa
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
11 changes: 6 additions & 5 deletions eark_validator/infopacks/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def from_file_path(root: Path, entry_path: Path,
algorithm: ChecksumAlg = ChecksumAlg.from_string(checksum_algorithm)
checksums = [ Checksummer.from_file(abs_path, algorithm) ] if checksum_algorithm else []
return ManifestEntry.model_validate({
'path': entry_path,
'path': str(entry_path),
'size': os.path.getsize(abs_path),
'checksums': checksums
})
Expand Down Expand Up @@ -167,7 +167,7 @@ def from_directory(source: Path | str, checksum_algorithm: ChecksumAlg=None) ->
entry_path,
checksum_algorithm=checksum_algorithm))
return Manifest.model_validate({
'root': path,
'root': str(path),
'source': SourceType.PACKAGE,
'summary': None,
'entries': entries
Expand All @@ -182,7 +182,7 @@ def from_mets_file(source: Path | str) -> Manifest:
entries: list[ManifestEntry] = list(map(ManifestEntries.from_file_entry,
mets_file.file_entries))
return Manifest.model_validate({
'root': path,
'root': str(path),
'source': SourceType.METS,
'summary': None,
'entries': entries
Expand All @@ -198,8 +198,9 @@ def _test_checksums(path: Path, checksums: list[Checksum]) -> list[str]:
return issues

def _resolve_manifest_root(manifest: Manifest) -> Path:
root: Path = Path(manifest.root)
if manifest.source == SourceType.PACKAGE:
return manifest.root
return root
if manifest.source == SourceType.METS:
return manifest.root.parent
return root.parent
raise ValueError(f'Unknown source type {manifest.source}')
4 changes: 2 additions & 2 deletions eark_validator/model/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from .constants import METS, UNKNOWN, PACKAGE # pylint: disable=W0611

class ManifestEntry(BaseModel):
path : Path | str
path : str
size : int = 0
checksums : List[Checksum] = []

Expand All @@ -52,7 +52,7 @@ class SourceType(str, Enum):

class Manifest(BaseModel):
source: SourceType = SourceType.UNKNOWN
root: Path
root: str
summary: Optional[ManifestSummary] = None
entries: List[ManifestEntry] = []

Expand Down
3 changes: 1 addition & 2 deletions eark_validator/model/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
# under the License.
#
from enum import Enum
from pathlib import Path
from typing import Annotated, List

from pydantic import BaseModel, StringConstraints
Expand All @@ -37,7 +36,7 @@ class EntryType(str, Enum):
METADATA = 'metadata'

class FileEntry(BaseModel):
path : Path | str
path : str
type: EntryType = EntryType.FILE
size : int = 0
checksum : Checksum
Expand Down
6 changes: 3 additions & 3 deletions eark_validator/model/validation_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"""

from enum import Enum, unique
from typing import Any, List, Optional
import uuid
from typing import Any, List, Optional, Annotated
from uuid import uuid4

from pydantic import BaseModel, ConfigDict, Field, model_validator

Expand Down Expand Up @@ -139,7 +139,7 @@ class MetatdataResultSet(BaseModel):
schematron_results: MetadataResults = Field(validation_alias='schematronResults')

class ValidationReport(BaseModel):
uid: uuid.UUID = uuid.uuid4()
uid: Annotated[str, Field(default_factory=lambda: uuid4().hex)]
structure: Optional[StructResults] = None
metadata: Optional[MetatdataResultSet] = None
package: Optional[InformationPackage] = None
Expand Down
4 changes: 2 additions & 2 deletions tests/manifests_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def test_from_file_entry(self):
entry: ManifestEntry = ManifestEntries.from_file_entry(_parse_file_entry(ET.fromstring(FILE_XML)))
self.assertEqual(entry.checksums[0].algorithm, ChecksumAlg.SHA256)
self.assertEqual(entry.checksums[0].value, 'F37E90511B5DDE2E9C60378A0F0A0A1CF07145C8F12651E0E19731892C608DA7')
self.assertEqual(entry.path, 'representations/rep1/METS.xml')
self.assertEqual(str(entry.path), 'representations/rep1/METS.xml')
self.assertEqual(entry.size, 3554)

class ManifestTest(unittest.TestCase):
Expand Down Expand Up @@ -255,7 +255,7 @@ def test_from_file(self):

def test_resolve_manifest_bad_source(self):
manifest = Manifest.model_validate({
'root': Path(files(UNPACKED).joinpath('733dc055-34be-4260-85c7-5549a7083031')),
'root': str(files(UNPACKED).joinpath('733dc055-34be-4260-85c7-5549a7083031')),
'source': SourceType.UNKNOWN
})
with self.assertRaises(ValueError):
Expand Down

0 comments on commit d5f8baa

Please sign in to comment.