Skip to content

Commit

Permalink
Add list order insensitive validation mode
Browse files Browse the repository at this point in the history
Apply mode to identifier, creator, date, format, subject, and temporal fields
  • Loading branch information
amywieliczka committed Dec 6, 2023
1 parent 0e03c4f commit 7d9f7e3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
5 changes: 1 addition & 4 deletions metadata_mapper/mappers/oai/cca_vault_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from .oai_mapper import OaiRecord, OaiVernacular
from ..mapper import Validator
from ...validator import ValidationLogLevel, ValidationMode
from ...validator import ValidationLogLevel


class CcaVaultRecord(OaiRecord):
Expand Down Expand Up @@ -60,9 +60,6 @@ def setup(self):
}
)

self.modify_validatable_fields("temporal", "date", "creator", "format",
validation_mode=ValidationMode.LAX)

@staticmethod
def str_match_ignore_url_protocol(validation_def: dict,
rikolti_value: Any,
Expand Down
9 changes: 8 additions & 1 deletion metadata_mapper/validator/validation_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ class StrictMode(Mode):
pass


class ListOrderInsensitiveMode(Mode):
@staticmethod
def list_compare(one: list, two: list) -> bool:
return sorted(one) == sorted(two)


class LaxMode(Mode):
"""
Lax validation mode generally permits case- and order-insensitive comparisons.
Expand All @@ -54,4 +60,5 @@ def any_compare(self, one: Any, two: Any) -> bool:


STRICT = StrictMode()
LAX = LaxMode()
LAX = LaxMode()
ORDER_INSENSITIVE_IF_LIST = ListOrderInsensitiveMode()
18 changes: 12 additions & 6 deletions metadata_mapper/validator/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,8 @@ def _ensure_is_list(value: Any) -> list[Any]:
"validations": [
Validator.content_match,
Validator.verify_type(Validator.list_of(str))
]
],
"validation_mode": ValidationMode.ORDER_INSENSITIVE_IF_LIST
},
{
"field": "title",
Expand Down Expand Up @@ -665,12 +666,14 @@ def _ensure_is_list(value: Any) -> list[Any]:
{
"field": "creator",
"validations": [Validator.content_match],
"level": ValidationLogLevel.WARNING
"level": ValidationLogLevel.WARNING,
"validation_mode": ValidationMode.ORDER_INSENSITIVE_IF_LIST
},
{
"field": "date",
"validations": [Validator.content_match],
"level": ValidationLogLevel.WARNING
"level": ValidationLogLevel.WARNING,
"validation_mode": ValidationMode.ORDER_INSENSITIVE_IF_LIST
},
{
"field": "description",
Expand All @@ -685,7 +688,8 @@ def _ensure_is_list(value: Any) -> list[Any]:
{
"field": "format",
"validations": [Validator.content_match],
"level": ValidationLogLevel.WARNING
"level": ValidationLogLevel.WARNING,
"validation_mode": ValidationMode.ORDER_INSENSITIVE_IF_LIST
},
{
"field": "genre",
Expand Down Expand Up @@ -745,12 +749,14 @@ def _ensure_is_list(value: Any) -> list[Any]:
{
"field": "subject",
"validations": [Validator.content_match],
"level": ValidationLogLevel.WARNING
"level": ValidationLogLevel.WARNING,
"validation_mode": ValidationMode.ORDER_INSENSITIVE_IF_LIST
},
{
"field": "temporal",
"validations": [Validator.content_match],
"level": ValidationLogLevel.WARNING
"level": ValidationLogLevel.WARNING,
"validation_mode": ValidationMode.ORDER_INSENSITIVE_IF_LIST
},
{
"field": "transcription",
Expand Down

0 comments on commit 7d9f7e3

Please sign in to comment.