Ensure no invalid min_length/min_value/max_value arguments. Closes #2369.

This commit is contained in:
Tom Christie 2015-01-05 10:52:18 +00:00
parent f8e310fdbb
commit 8cf3744971

View File

@ -10,6 +10,11 @@ from rest_framework.validators import UniqueValidator
import inspect import inspect
NUMERIC_FIELD_TYPES = (
models.IntegerField, models.FloatField, models.DecimalField
)
class ClassLookupDict(object): class ClassLookupDict(object):
""" """
Takes a dictionary with classes as keys. Takes a dictionary with classes as keys.
@ -119,7 +124,7 @@ def get_field_kwargs(field_name, model_field):
validator.limit_value for validator in validator_kwarg validator.limit_value for validator in validator_kwarg
if isinstance(validator, validators.MinLengthValidator) if isinstance(validator, validators.MinLengthValidator)
), None) ), None)
if min_length is not None: if min_length is not None and isinstance(model_field, models.CharField):
kwargs['min_length'] = min_length kwargs['min_length'] = min_length
validator_kwarg = [ validator_kwarg = [
validator for validator in validator_kwarg validator for validator in validator_kwarg
@ -132,7 +137,7 @@ def get_field_kwargs(field_name, model_field):
validator.limit_value for validator in validator_kwarg validator.limit_value for validator in validator_kwarg
if isinstance(validator, validators.MaxValueValidator) if isinstance(validator, validators.MaxValueValidator)
), None) ), None)
if max_value is not None: if max_value is not None and isinstance(model_field, NUMERIC_FIELD_TYPES):
kwargs['max_value'] = max_value kwargs['max_value'] = max_value
validator_kwarg = [ validator_kwarg = [
validator for validator in validator_kwarg validator for validator in validator_kwarg
@ -145,7 +150,7 @@ def get_field_kwargs(field_name, model_field):
validator.limit_value for validator in validator_kwarg validator.limit_value for validator in validator_kwarg
if isinstance(validator, validators.MinValueValidator) if isinstance(validator, validators.MinValueValidator)
), None) ), None)
if min_value is not None: if min_value is not None and isinstance(model_field, NUMERIC_FIELD_TYPES):
kwargs['min_value'] = min_value kwargs['min_value'] = min_value
validator_kwarg = [ validator_kwarg = [
validator for validator in validator_kwarg validator for validator in validator_kwarg