-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.py
More file actions
92 lines (76 loc) · 2.78 KB
/
validators.py
File metadata and controls
92 lines (76 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import pycountry
import rest_framework.exceptions
import rest_framework.serializers
import user.models
class UniqueEmailValidator:
def __init__(self, default_detail=None, default_code=None):
self.status_code = 409
self.default_detail = (
default_detail or 'This email address is already registered.'
)
self.default_code = default_code or 'email_conflict'
def __call__(self, value):
if user.models.User.objects.filter(email=value).exists():
exc = rest_framework.exceptions.APIException(
detail={
'status': 'error',
'message': self.default_detail,
'code': self.default_code,
},
)
exc.status_code = self.status_code
raise exc
class OtherFieldValidator(rest_framework.serializers.Serializer):
"""
Validates JSON fields:
- age (required, 0-100)
- country (required, valid ISO 3166-1 alpha-2)
"""
country_codes = {c.alpha_2 for c in pycountry.countries}
age = rest_framework.serializers.IntegerField(
required=True,
min_value=0,
max_value=100,
error_messages={
'required': 'This field is required.',
'invalid': 'Must be an integer.',
'min_value': 'Must be between 0 and 100.',
'max_value': 'Must be between 0 and 100.',
},
)
country = rest_framework.serializers.CharField(
required=True,
max_length=2,
min_length=2,
error_messages={
'required': 'This field is required.',
'blank': 'Must be a 2-letter ISO code.',
'max_length': 'Must be a 2-letter ISO code.',
'min_length': 'Must be a 2-letter ISO code.',
},
)
def validate_country(self, value):
country = value.upper()
if country not in self.country_codes:
raise rest_framework.serializers.ValidationError(
'Invalid ISO 3166-1 alpha-2 country code.',
)
return country
def __call__(self, value):
if not isinstance(value, dict):
raise rest_framework.serializers.ValidationError(
{'non_field_errors': ['Must be a JSON object']},
)
missing_fields = [
field
for field in self.fields
if field not in value or value.get(field) in (None, '')
]
if missing_fields:
raise rest_framework.serializers.ValidationError(
dict.fromkeys(missing_fields, 'This field is required.'),
)
serializer = self.__class__(data=value)
if not serializer.is_valid():
raise rest_framework.serializers.ValidationError(serializer.errors)
return value