ae.valid

data validation helper functions

this module is pure Python and has no dependencies.

the two slightly bigger helper functions provided by this namespace portion are correct_email() and correct_phone(), which are useful to check if a string contains a valid email address or phone number.

they also allow you to automatically correct an email address or a phone number to a valid format. more sophisticated helpers for the validation of email addresses, phone numbers and post addresses are available in the ae.validation namespace portion.

Functions

correct_email(email[, changed, removed])

check and correct email address from a user input (removing all comments).

correct_phone(phone[, changed, removed, ...])

check and correct phone number from a user input (removing all invalid characters including spaces).

correct_email(email, changed=False, removed=None)[source]

check and correct email address from a user input (removing all comments).

special conversions that are not returned as changed/corrected are: the domain part of an email will be corrected to lowercase characters, additionally emails with all letters in uppercase will be converted into lowercase.

regular expressions are not working for all edge cases (see the answer to this SO question: https://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address) because RFC822 is very complex (even the reg expression recommended by RFC 5322 is not complete; there is also a more readable form given in the informational RFC 3696). additionally a regular expression does not allow corrections. therefore this function is using a procedural approach (using recommendations from RFC 822 and https://en.wikipedia.org/wiki/Email_address).

Parameters:
  • email (str) – email address to check and correct.

  • changed (bool) – optional flag if email address got changed (before calling this function) - will be returned unchanged if email did not get corrected.

  • removed (Optional[List[str]]) – optional list declared by caller to pass back all the removed characters including the index in the format “<index>:<removed_character(s)>”.

Return type:

Tuple[str, bool]

Returns:

tuple of (possibly corrected email address, flag if email got changed/corrected).

correct_phone(phone, changed=False, removed=None, keep_1st_hyphen=False)[source]

check and correct phone number from a user input (removing all invalid characters including spaces).

Parameters:
  • phone (str) – phone number to check and correct.

  • changed (bool) – optional flag if phone got changed (before calling this function) - will be returned unchanged if phone did not get corrected.

  • removed (Optional[List[str]]) – optional list declared by caller to pass back all the removed characters including the index in the format “<index>:<removed_character(s)>”.

  • keep_1st_hyphen (bool) – pass True to keep at least the first occurring hyphen character.

Return type:

Tuple[str, bool]

Returns:

tuple of (possibly corrected phone number, flag if phone got changed/corrected).