def validate(self, value): if self.choices and value not in EMPTY_VALUES and not value_in_choices(value, self.choices): msg = self.error_messages['invalid_choice'] % value raise exceptions.ValidationError(msg) if not self.null and value is None: raise exceptions.ValidationError(self.error_messages['null'])
def test_value_in_choices(self): self.assertTrue(utils.value_in_choices('foo', self.CHOICES)) self.assertTrue(utils.value_in_choices(123, self.CHOICES)) self.assertTrue(utils.value_in_choices(True, self.CHOICES)) self.assertTrue(utils.value_in_choices(None, self.CHOICES)) self.assertTrue(utils.value_in_choices('eek', self.CHOICES)) self.assertFalse(utils.value_in_choices('xyz', self.CHOICES)) self.assertFalse(utils.value_in_choices(False, self.CHOICES)) self.assertFalse(utils.value_in_choices(321, self.CHOICES))
def test_value_in_choices(self): assert utils.value_in_choices('foo', self.CHOICES) assert utils.value_in_choices(123, self.CHOICES) assert utils.value_in_choices(True, self.CHOICES) assert utils.value_in_choices(None, self.CHOICES) assert utils.value_in_choices('eek', self.CHOICES) assert not utils.value_in_choices('xyz', self.CHOICES) assert not utils.value_in_choices(False, self.CHOICES) assert not utils.value_in_choices(321, self.CHOICES)
def test_value_in_choices(self): self.assertTrue(utils.value_in_choices('foo', self.CHOICES)) self.assertTrue(utils.value_in_choices(123, self.CHOICES)) self.assertTrue(utils.value_in_choices(True, self.CHOICES)) self.assertTrue(utils.value_in_choices(None, self.CHOICES)) self.assertTrue(utils.value_in_choices('eek', self.CHOICES)) self.assertFalse(utils.value_in_choices('xyz', self.CHOICES)) self.assertFalse(utils.value_in_choices(False, self.CHOICES)) self.assertFalse(utils.value_in_choices(321, self.CHOICES))
def test_value_in_choices(self): assert utils.value_in_choices('foo', self.CHOICES) assert utils.value_in_choices(123, self.CHOICES) assert utils.value_in_choices(True, self.CHOICES) assert utils.value_in_choices(None, self.CHOICES) assert utils.value_in_choices('eek', self.CHOICES) assert not utils.value_in_choices('xyz', self.CHOICES) assert not utils.value_in_choices(False, self.CHOICES) assert not utils.value_in_choices(321, self.CHOICES)
def test_value_in_choices(self): assert utils.value_in_choices("foo", self.CHOICES) assert utils.value_in_choices(123, self.CHOICES) assert utils.value_in_choices(True, self.CHOICES) assert utils.value_in_choices(None, self.CHOICES) assert utils.value_in_choices("eek", self.CHOICES) assert not utils.value_in_choices("xyz", self.CHOICES) assert not utils.value_in_choices(False, self.CHOICES) assert not utils.value_in_choices(321, self.CHOICES)
def validate(self, value): if self.choices and value not in EMPTY_VALUES and not value_in_choices( value, self.choices): msg = self.error_messages['invalid_choice'] % value raise exceptions.ValidationError(msg) if not self.null and value is None: raise exceptions.ValidationError(self.error_messages['null'])
def _process_dict(self, value_dict, method): values = {} errors = {} key_choices = self.key_choices for key, value in six.iteritems(value_dict): if key_choices and not value_in_choices(key, key_choices): msg = self.error_messages['invalid_key'] % value raise exceptions.ValidationError(msg) try: values[key] = method(value) except exceptions.ValidationError as ve: errors[key] = ve.error_messages if errors: raise exceptions.ValidationError(errors) return values
def _process_dict(self, value_dict, method): values = {} errors = {} key_choices = self.key_choices for key, value in six.iteritems(value_dict): if key_choices and not value_in_choices(key, key_choices): msg = self.error_messages['invalid_key'] % value raise exceptions.ValidationError(msg) try: values[key] = method(value) except exceptions.ValidationError as ve: errors[key] = ve.error_messages if errors: raise exceptions.ValidationError(errors) return values