Skip to content

Commit

Permalink
Create input_validator.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Sep 18, 2024
1 parent 15652c3 commit b93c95f
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions input_validation/input_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import re

class InputValidator:
def __init__(self):
self.patterns = {}

def add_pattern(self, field, pattern):
self.patterns[field] = re.compile(pattern)

def validate(self, data):
for field, pattern in self.patterns.items():
if field in data:
if not pattern.match(data[field]):
return False
return True

# Example usage:
validator = InputValidator()
validator.add_pattern("username", r"^[a-zA-Z0-9_]+$")
validator.add_pattern("email", r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$")

data = {"username": "invalid_username!", "email": "invalid_email"}
if not validator.validate(data):
print("Invalid input")

0 comments on commit b93c95f

Please sign in to comment.