-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move API validation into validators.py module
- Loading branch information
Showing
6 changed files
with
60 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# -*- coding: utf-8 -*- | ||
import pytest | ||
|
||
from h.api import validators | ||
|
||
|
||
def test_Annotation_raises_if_document_link_is_None(): | ||
annotation = validators.Annotation( | ||
data={'document': {'link': None}}) # Invalid link. | ||
|
||
with pytest.raises(validators.Error): | ||
annotation.validate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Classes for validating data passed to the annotations API.""" | ||
|
||
|
||
class Error(Exception): | ||
|
||
"""Base exception class for all exceptions raised by this module.""" | ||
|
||
pass | ||
|
||
|
||
class Annotation(object): | ||
|
||
"""An annotation validator.""" | ||
|
||
def __init__(self, data): | ||
self.data = data | ||
|
||
def validate(self): | ||
"""Raise h.api.validation.Error if this annotation is invalid.""" | ||
if 'document' in self.data and 'link' in self.data['document']: | ||
if not isinstance(self.data['document']['link'], list): | ||
raise Error("document.link must be an array") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters