Revised constraint validation logic

This commit is contained in:
tengqm 2015-03-08 19:16:55 +08:00
parent 9c3f999879
commit c6cd4a7cef

View File

@ -28,14 +28,9 @@ class BaseConstraint(collections.Mapping):
'''Utility method for generating schema docs.''' '''Utility method for generating schema docs.'''
return self.desc() return self.desc()
def validate(self, value, context=None): def validate(self, value, schema=None, context=None):
'''Base entry for validation. '''Base entry for validation.'''
:param value: value for validation. if not self._validate(value, schema, context):
:param schema: the schema that may provide customized validation.
:param context: optional argument in case validation needs a context.
'''
# The actual validation is implemented by subclasses
if not self._validate(value, context):
raise ValueError(self._error(value)) raise ValueError(self._error(value))
@classmethod @classmethod
@ -81,10 +76,15 @@ class AllowedValues(BaseConstraint):
return _('"%(value)s" must be one of the allowed values: ' return _('"%(value)s" must be one of the allowed values: '
'%(allowed)s') % dict(value=value, allowed=values) '%(allowed)s') % dict(value=value, allowed=values)
def _validate(self, value, context=None): def _validate(self, value, schema, context=None):
if isinstance(value, collections.Sequence): if isinstance(value, list):
return all(v in self.allowed for v in value) return all(v in self.allowed for v in value)
# try implicit type conversion
if schema is not None:
_allowed = tuple(schema.to_schema_type(v)
for v in self.allowed)
return schema.to_schema_type(value) in _allowed
return value in self.allowed return value in self.allowed
def _constraint(self): def _constraint(self):