Skip to content
This repository has been archived by the owner on Jun 2, 2022. It is now read-only.

Commit

Permalink
Add validator for authors field
Browse files Browse the repository at this point in the history
This checks to make sure the authors field does not contain an empty
list or a list of things that are not Authors.
  • Loading branch information
Mike Graves committed Oct 8, 2020
1 parent e530580 commit a3b6626
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion hoard/models.py
Expand Up @@ -3,6 +3,21 @@
import attr


def list_of(item_type):
return attr.validators.and_(
attr.validators.deep_iterable(
member_validator=attr.validators.instance_of(item_type),
iterable_validator=attr.validators.instance_of(list),
),
not_empty,
)


def not_empty(instance, attribute, value):
if len(value) == 0:
raise ValueError(f"'{attribute.name}' cannot be empty. Got '{value}'.")


@attr.s(auto_attribs=True)
class Author:
authorName: str
Expand Down Expand Up @@ -81,7 +96,7 @@ class TimePeriodCovered:

@attr.s(auto_attribs=True)
class Dataset:
authors: List[Author]
authors: List[Author] = attr.ib(validator=list_of(Author))
contacts: List[Contact]
description: List[Description]
subjects: List[str]
Expand Down

0 comments on commit a3b6626

Please sign in to comment.