Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Height/Width/Duration Validator POC #169

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions iiif_prezi3/skeleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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'
Expand Down
25 changes: 25 additions & 0 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
@@ -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)