summaryrefslogtreecommitdiff
path: root/day9/task5_vue/backend/database/validators.py
diff options
context:
space:
mode:
authorAndrew <saintruler@gmail.com>2019-07-21 12:14:09 +0400
committerAndrew <saintruler@gmail.com>2019-07-21 12:14:09 +0400
commit2305ced85888a23f86ecfcdfb64a3b69c4997a4c (patch)
tree9d5cc4ad1c322ca2bf46b990498ad0a79d508aff /day9/task5_vue/backend/database/validators.py
parent8c8712f0e6b165f6967f5d2958f300df6182296c (diff)
Переписан бекенд. Добавлена валидация при обновлении значений в ряду.
Удалена предыдущая версия приложения без vue.
Diffstat (limited to 'day9/task5_vue/backend/database/validators.py')
-rw-r--r--day9/task5_vue/backend/database/validators.py96
1 files changed, 96 insertions, 0 deletions
diff --git a/day9/task5_vue/backend/database/validators.py b/day9/task5_vue/backend/database/validators.py
new file mode 100644
index 0000000..e4b7310
--- /dev/null
+++ b/day9/task5_vue/backend/database/validators.py
@@ -0,0 +1,96 @@
+from abc import ABC, abstractmethod
+import re
+
+
+class ValidationError(Exception):
+ pass
+
+
+class Validator(ABC):
+ @staticmethod
+ @abstractmethod
+ def validate(value, field_object):
+ pass
+
+
+class ValidateNull(Validator):
+ @staticmethod
+ def validate(value, field_object):
+ if value is None and not field_object.nullable:
+ raise ValidationError('Value cannot be NULL')
+
+
+class ValidateType(Validator):
+ @staticmethod
+ def validate(value, field_object):
+ if field_object.nullable and value is None:
+ return
+ if not isinstance(value, field_object.data_type):
+ raise ValidationError('Value is of wrong type')
+
+
+class ValidateLength(Validator):
+ @staticmethod
+ def validate(value, field_object):
+ if len(str(value)) > field_object.max_length:
+ raise ValidationError('Value has too many digits')
+
+
+class ValidateTime(Validator):
+ @staticmethod
+ def validate(value, field_object):
+ match = re.fullmatch(r'(\d\d):(\d\d):(\d\d)', value)
+ if not match:
+ raise ValidationError('Wrong time format')
+ else:
+ hour, minute, second = map(int, match.groups())
+ if hour not in range(0, 24):
+ raise ValidationError('Wrong hour value')
+
+ if minute not in range(0, 60):
+ raise ValidationError('Wrong minute value')
+
+ if second not in range(0, 60):
+ raise ValidationError('Wrong second value')
+
+
+class ValidateDate(Validator):
+ @staticmethod
+ def validate(value, field_object):
+ match = re.fullmatch(r'(\d\d\d\d)-(\d\d)-(\d\d)', value)
+ if not match:
+ raise ValidationError('Wrong date format')
+ else:
+ year, month, day = map(int, match.groups())
+ if year < 0:
+ raise ValidationError('Wrong year value')
+
+ if month not in range(1, 12):
+ raise ValidationError('Wrong month value')
+
+ if month == 2:
+ if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
+ febr_range = range(1, 29)
+ else:
+ febr_range = range(1, 28)
+ if day not in febr_range:
+ raise ValidationError('Wrong day value')
+ else:
+ days_count = {
+ 1: 31, 3: 31, 4: 30, 5: 31,
+ 6: 30, 7: 31, 8: 31, 9: 30,
+ 10: 31, 11: 30, 12: 31
+ }.get(month)
+ if day not in range(1, days_count):
+ raise ValidationError('Wrong day value')
+
+
+class ValidateDatetime(Validator):
+ @staticmethod
+ def validate(value, field_object):
+ datetime = value.split()
+ if len(datetime) != 2:
+ raise ValidationError('Wrong datetime format')
+ else:
+ ValidateDate.validate(datetime[0], {})
+ ValidateTime.validate(datetime[1], {}) \ No newline at end of file