Skip to content
Merged
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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

setup(
name='webthing',
version='0.9.1',
version='0.9.2',
description='HTTP Web Thing implementation',
long_description=long_description,
url='https://github.com/mozilla-iot/webthing-python',
Expand Down
42 changes: 6 additions & 36 deletions webthing/property.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""High-level Property base class implementation."""

from copy import copy
from jsonschema import validate
from jsonschema.exceptions import ValidationError

from .errors import PropertyError

Expand Down Expand Up @@ -35,45 +37,13 @@ def validate_value(self, value):

value -- New value
"""
if 'type' in self.metadata:
t = self.metadata['type']

if t == 'null':
if t is not None:
raise PropertyError('Value must be null')
elif t == 'boolean':
if type(value) is not bool:
raise PropertyError('Value must be a boolean')
elif t == 'object':
if type(value) is not dict:
raise PropertyError('Value must be an object')
elif t == 'array':
if type(value) is not list:
raise PropertyError('Value must be an array')
elif t == 'number':
if type(value) not in [float, int]:
raise PropertyError('Value must be a number')
elif t == 'integer':
if type(value) is not int:
raise PropertyError('Value must be an integer')
elif t == 'string':
if type(value) is not str:
raise PropertyError('Value must be a string')

if 'readOnly' in self.metadata and self.metadata['readOnly']:
raise PropertyError('Read-only property')

if 'minimum' in self.metadata and value < self.metadata['minimum']:
raise PropertyError('Value less than minimum: {}'
.format(self.metadata['minimum']))

if 'maximum' in self.metadata and value > self.metadata['maximum']:
raise PropertyError('Value greater than maximum: {}'
.format(self.metadata['maximum']))

if 'enum' in self.metadata and len(self.metadata['enum']) > 0 and \
value not in self.metadata['enum']:
raise PropertyError('Invalid enum value')
try:
validate(value, self.metadata)
except ValidationError:
raise PropertyError('Invalid property value')

def as_property_description(self):
"""
Expand Down