From 2305ced85888a23f86ecfcdfb64a3b69c4997a4c Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 21 Jul 2019 12:14:09 +0400 Subject: =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BF=D0=B8=D1=81=D0=B0=D0=BD?= =?UTF-8?q?=20=D0=B1=D0=B5=D0=BA=D0=B5=D0=BD=D0=B4.=20=D0=94=D0=BE=D0=B1?= =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=B2=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=B4=D0=B0=D1=86=D0=B8=D1=8F=20=D0=BF=D1=80=D0=B8=20=D0=BE?= =?UTF-8?q?=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B8=20=D0=B7?= =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=B2=20=D1=80?= =?UTF-8?q?=D1=8F=D0=B4=D1=83.=20=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BF=D1=80=D0=B5=D0=B4=D1=8B=D0=B4=D1=83=D1=89=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8F=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B8=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B1=D0=B5?= =?UTF-8?q?=D0=B7=20vue.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day9/task5_vue/backend/database/validators.py | 96 +++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 day9/task5_vue/backend/database/validators.py (limited to 'day9/task5_vue/backend/database/validators.py') 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 -- cgit v1.2.3