From df9e0b3b4ae25cac0cbd6a8347f675262aa19047 Mon Sep 17 00:00:00 2001 From: Mike Bennett Date: Fri, 10 Mar 2023 01:59:59 +0000 Subject: [PATCH 1/2] Proof-of-concept validators for hwd --- iiif_prezi3/skeleton.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/iiif_prezi3/skeleton.py b/iiif_prezi3/skeleton.py index ccc860c..37e6703 100644 --- a/iiif_prezi3/skeleton.py +++ b/iiif_prezi3/skeleton.py @@ -7,7 +7,8 @@ from datetime import datetime from typing import Any, Dict, List, Optional, Union -from pydantic import AnyUrl, Extra, Field, PositiveFloat, PositiveInt, constr +from pydantic import (AnyUrl, Extra, Field, PositiveFloat, PositiveInt, constr, + root_validator) from .base import Base @@ -288,9 +289,9 @@ class Annotation(Class): class Canvas(Class): type: constr(regex=r'^Canvas$') = 'Canvas' - height: Optional[Dimension] = None - width: Optional[Dimension] = None - duration: Optional[Duration] = None + height: Optional[Dimension] + width: Optional[Dimension] + duration: Optional[Duration] metadata: Optional[Metadata] = None summary: Optional[LngString] = None requiredStatement: Optional[KeyValueString] = None @@ -310,6 +311,21 @@ class Canvas(Class): items: List[AnnotationPage] annotations: Optional[List[AnnotationPage]] = None + @root_validator + def hw_andor_d(cls, values): + """Validates that height/width and/or duration are set""" + assert (values['height'] is not None or values['width'] is not None or values['duration'] is not None), 'height/width and/or duration must be present' + return values + + @root_validator + def both_hw(cls, values): + """Validates that height and width are both set if one is""" + if values['height'] is not None: + assert values['width'] is not None, 'width must be present if height is present' + if values['width'] is not None: + assert values['height'] is not None, 'height must be present if width is present' + return values + class PlaceholderCanvas(Class): type: constr(regex=r'^Canvas$') = 'Canvas' From 65a88aff1c805c281ea941f322864b57ce003704 Mon Sep 17 00:00:00 2001 From: Mike Bennett Date: Fri, 10 Mar 2023 02:08:40 +0000 Subject: [PATCH 2/2] Add tests for the validator proof of concept --- tests/test_validators.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 tests/test_validators.py diff --git a/tests/test_validators.py b/tests/test_validators.py new file mode 100644 index 0000000..4cab04d --- /dev/null +++ b/tests/test_validators.py @@ -0,0 +1,25 @@ +import unittest + +from pydantic import ValidationError + +from iiif_prezi3 import Canvas + + +class CanvasValidatorsTests(unittest.TestCase): + def test_hw_andor_d(self): + """Test the height&width and/or duration validator.""" + with self.assertRaises(ValidationError): + Canvas() + c = Canvas(height=100, width=100) + self.assertEqual(c.height, 100) + c = Canvas(duration=100.0) + self.assertEqual(c.duration, 100.0) + + def test_h_and_w(self): + """Test that including height or width requires the other.""" + with self.assertRaises(ValidationError): + Canvas(height=100) + with self.assertRaises(ValidationError): + Canvas(width=100) + with self.assertRaises(ValidationError): + Canvas(height=100, duration=100.0)