Skip to content

Instantly share code, notes, and snippets.

@decbr1
Created February 24, 2026 15:55
Show Gist options
  • Select an option

  • Save decbr1/04297a64f027e156e25ef2da2868e53f to your computer and use it in GitHub Desktop.

Select an option

Save decbr1/04297a64f027e156e25ef2da2868e53f to your computer and use it in GitHub Desktop.
input validation class with a flexible 'add_rules(condition, msg)`
class InputValidator:
def __init__(self, prompt, cast=str, error_msg="Invalid input."):
self.prompt = prompt
self.cast = cast
self.error_msg = error_msg
self.rules = []
def add_rule(self, condition, message=None):
self.rules.append((condition, message or self.error_msg))
return self # allows method chaining
def get(self):
while True:
try:
value = self.cast(input(self.prompt))
failed = [msg for check, msg in self.rules if not check(value)]
if not failed:
return value
for msg in failed:
print(msg)
except (ValueError, TypeError):
print(self.error_msg)
# examples:
score = (
InputValidator("Enter score: ", cast=int, error_msg="Must be a number.")
.add_rule(lambda userinput: userinput >= 0, "Score cannot be negative.")
.add_rule(lambda userinput: userinput <= 100, "Score cannot exceed 100.")
.get()
)
# of course, 'userinput' can be replaced with 'x' as standard:
# `.add_rule(lambda x: "@" in x, "Email must contain @.")`
email = (
InputValidator("Enter email: ", cast=str, error_msg="Invalid input.")
.add_rule(lambda userinput: "@" in userinput, "Email must contain @.")
.add_rule(lambda userinput: "." in userinput.split("@")[-1], "Email must have a valid domain.")
.add_rule(lambda userinput: len(userinput) >= 5, "Email is too short.")
.get()
)
# of course, 'userinput' can be replaced with 'x' as standard:
# `.add_rule(lambda x: len(x) >= 8, "Password must be at least 8 characters.")`
# or 'character' can be replaced with 'c':
# `.add_rule(lambda x: any(c.isupper() for c in x), "Must contain an uppercase letter.")`
password = (
InputValidator("Enter password: ", cast=str, error_msg="Invalid input.")
.add_rule(lambda userinput: len(userinput) >= 8, "Password must be at least 8 characters.")
.add_rule(lambda userinput: any(character.isupper() for character in userinput), "Must contain an uppercase letter.")
.add_rule(lambda userinput: any(character.islower() for character in userinput), "Must contain a lowercase letter.")
.add_rule(lambda userinput: any(character.isdigit() for character in userinput), "Must contain a number.")
.add_rule(lambda userinput: any(character in "!@#$%^&*()-_=+" for character in userinput), "Must contain a special character.")
.get()
)
# to save time, this would do the same thing:
password = (
InputValidator("Enter password: ", cast=str, error_msg="Invalid input.")
.add_rule(lambda x: len(x) >= 8, "Password must be at least 8 characters.")
.add_rule(lambda x: any(c.isupper() for c in x), "Must contain an uppercase letter.")
.add_rule(lambda x: any(c.islower() for c in x), "Must contain a lowercase letter.")
.add_rule(lambda x: any(c.isdigit() for c in x), "Must contain a number.")
.add_rule(lambda x: any(c in "!@#$%^&*()-_=+" for c in x), "Must contain a special character.")
.get()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment