-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21.DesignAFormValidatorUtility.py
More file actions
96 lines (73 loc) · 2.5 KB
/
21.DesignAFormValidatorUtility.py
File metadata and controls
96 lines (73 loc) · 2.5 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
93
94
95
96
# Design a Form Validator Utility in Python that allows adding multiple input fields,
# each with customizable validation rules (like required, min/max length, email format, and
# age check). The utility should validate all fields and return descriptive error messages
# for invalid inputs.
import re
class Field:
def __init__(self, name, value, validators=None):
self.name = name
self.value = value
self.validators = validators or []
def validate(self):
errors = []
for v in self.validators:
result = v(self.value)
if result is not True:
errors.append(result)
return errors
def required(value):
if value is None or str(value).strip() == "":
return "Fiels is required"
return True
def min_length(n):
def inner(value):
if len(str(value)) < n:
return f"Minimum length should be {n}"
return True
return inner
def max_length(n):
def inner(value):
if len(str(value)) > n:
return f"Maximum length should be {n}"
return True
return inner
def email_validator(value):
pattern = r"^[\w\.-]+@[\w\.-]+\.\w+$"
if not re.match(pattern, str(value)):
return "Invalid email format"
return True
def age_validator(value):
if not isinstance(value, int) or value <= 0:
return "Age must be a positive Integer"
return True
class FormValidator:
def __init__(self):
self.fields = []
def add_field(self, field):
self.fields.append(field)
def validate(self):
errors = {}
for f in self.fields:
result = f.validate()
if result:
errors[f.name] = result
return errors
if __name__ == "__main__":
# Create an instance of FormValidator
form = FormValidator()
# Add several fields with their respective validators
form.add_field(Field("username", "Manu", [required, min_length(3)]))
form.add_field(Field("email", "manu@yt", [required, email_validator]))
form.add_field(Field("age", 31, [required, age_validator]))
form.add_field(
Field("password", "pw", [required, min_length(6), max_length(12)]))
# Run validation across all fields
result = form.validate()
# Display results neatly
if not result:
print("✅ All validations passed!")
else:
print("❌ Validation Errors Found:")
for field, errs in result.items():
for e in errs:
print(f"- {field}: {e}")