Commits

Anonymous committed 296f511

add a NumericRangeValidator and associated docs

  • Participants
  • Parent commits b15f545

Comments (0)

Files changed (4)

 *.cache
 .svn*
 .git*
-Thumbs.db
+*.egg-info
+Thumbs.db
 recursive-include docs *
 include LICENSE
-include README
+include README.md
 
 class MyModel(models.Model):
     even_field = models.IntegerField(validators=[FileTypeValidator(['zip', 'txt', 'pdf'])])
-```
+```
+
+###NumericRangeValidator
+Ensure the field value falls within the specified range.
+
+```python
+from django.db import models
+from model_validators.validators import NumericRangeValidator
+
+class MyModel(models.Model):
+    rating = models.PositiveIntegerField(validators=[NumbericRangeValidator(1, 10)])
+```

model_validators/validators.py

         file_extension = splitext(value.name)[-1][1:].lower()
         if file_extension not in self.file_extensions:
             raise ValidationError(u'File type %s not allowed.' % file_extension, code='invalid')
+
+
+class NumericRangeValidator(object):
+    def __init__(self, min_value, max_value):
+        assert(min_value <= max_value)
+        self.min_value = min_value
+        self.max_value = max_value
+
+    def __call__(self, value):
+        if value < self.min_value or value > self.max_value:
+            raise ValidationError(u'Value is out of range [%s - %s].' % (self.min_value, self.max_value))