def validate(self, document): """Validates year month strings Args: document: User input to questionary prompt Raises: ValidationError if year and month is not in valid format OR range between start month and end month is less than 12 months. Returns: True if date is in valid format """ ok = False try: # datetime.datetime.strptime(document.text, '%Y-%m') import builtins start_year_month = builtins.start_year_month start_date = datetime.datetime.strptime(start_year_month + "-1", "%Y-%m-%d").date() end_date = datetime.datetime.strptime(document.text + "-1", "%Y-%m-%d").date() except ValueError: raise ValidationError( message='Please enter a valid date in YYYY-MM-DD format', cursor_position=len(document.text)) # Move cursor to end delta = relativedelta.relativedelta(end_date, start_date) num_months = delta.years * 12 + delta.months + 1 # Pandas datetime conversion only works for certain time period if num_months >= 12 and (2012 <= end_date.year <= 2112): ok = True if not ok: raise ValidationError( message='The time period for data extraction must be >= 12 months, and year must be between 2012 and 2112', cursor_position=len(document.text)) # Move cursor to end
def validate(self, document): """Validates shipment weight Args: document: User input to questionary prompt Raises: ValidationError if weight is not valid Returns: True if weight is a number and below 150 lbs. According to FedEx and UPS, air shipment weight cannot exceed 150 lbs. """ ok = False try: weight = float(document.text) except ValueError: raise ValidationError( message='Please enter a number', cursor_position=len(document.text)) # Move cursor to end if 0 <= weight <= 150: ok = True if not ok: raise ValidationError( message='Shipment weight for air services cannot exceed 150 lbs', cursor_position=len(document.text)) # Move cursor to end
def validate(self, document): """Validates fractions. Args: document: User input to questionary prompt Raises: ValidationError if fraction is not valid Returns: True if fraction is valid. """ ok = False try: fraction = float(document.text) except ValueError: raise ValidationError( message='Please enter a number', cursor_position=len(document.text)) # Move cursor to end if 0 <= fraction <= 1: ok = True if not ok: raise ValidationError( message='Please enter a fraction between 0 and 1', cursor_position=len(document.text)) # Move cursor to end
def validate(self, document): """Validates percentiles Args: document: User input to questionary prompt Raises: ValidationError if percentile is not valid Returns: True if percentile is a number and between 0 and 100. """ ok = False try: percentile = int(document.text) except ValueError: raise ValidationError( message='Please enter a number', cursor_position=len(document.text)) # Move cursor to end if 0 <= percentile <= 100: ok = True if not ok: raise ValidationError( message='Please enter a number between 0 and 100', cursor_position=len(document.text)) # Move cursor to end
def validate(self, document): """Validates year month strings Args: document: User input to questionary prompt Raises: ValidationError if year and month is not in valid format Returns: True if date is in valid format """ ok = False try: date = datetime.datetime.strptime(document.text + "-1", "%Y-%m-%d").date() except ValueError: raise ValidationError( message='Please enter a valid date in YYYY-MM-DD format', cursor_position=len(document.text)) # Move cursor to end # Pandas datetime conversion only works for certain time period if 2012 <= date.year <= 2112: ok = True if not ok: raise ValidationError( message='Year must be between 2012 and 2112', cursor_position=len(document.text)) # Move cursor to end
def validate(self, document): if len(document.text) == 0: raise ValidationError( message = "Please enter a value", cursor_position = len(document.text), ) if not document.text.isnumeric(): raise ValidationError( message = "Only numeric value is allowed", cursor_position = len(document.text), )
def validate(self, document): if ' ' in document.text: raise ValidationError( message='The JIRA/branch reference cannot contain spaces', cursor_position=len(document.text)) if any([c.isupper() for c in document.text]): raise ValidationError( message='The JIRA/branch reference must be all lower case', cursor_position=len(document.text)) if len(document.text) == 0: raise ValidationError( message= 'A value must be provided for the JIRA/branch reference', cursor_position=len(document.text))
def validate(self, document): if len(document.text) == 0: raise ValidationError( message = "Please enter a value", cursor_position = len(document.text), ) if not document.text.isnumeric() or len(document.text) != 4: raise ValidationError( message = "Please enter a valid zipcode", cursor_position = len(document.text), ) if int(document.text) < 1001 or int(document.text) > 9990: raise ValidationError( message = "This zipcode is not allowed" )
def validate(self,document): if len(document.text)== 0: raise ValidationError( message = "Indtast venligst kun numre", cursor_position = len(document.text), ) if not document.text.isnumeric() or len(document.text) != 4: raise ValidationError( message = "Indtast venligst et byggeår på 4 cifre" ) if int(document.text) <1400 or int(document.text) >2030: raise ValidationError( message = "Indtast venligst et gyldigt byggeår " )
def validate(self, document): ok = "Polyergus" in document.text if not ok: raise ValidationError( message="Please choose a Polyergus Ant", cursor_position=len(document.text), ) # Move cursor to end
def validate(self, document): try: int(document.text) except ValueError: raise ValidationError(message='Please enter a number', cursor_position=len( document.text)) # Move cursor to end
def validate(self, value): if len(value.text): return True else: raise ValidationError( message="Esse campo é de preenchimento obrigatório.", cursor_position=len(value.text))
def autoc_validator(text, cursor, query): """ Checks if the car details being inputted are valid, does not let user submit if it isn't in the local database. Parameters: text (str): user's input, updates as it changes allowing for real time validation, unlike python's input() cursor (obj): cursor for database we will be querying query (str): checked to determine what car detail to query for validation, the make or the model and year Returns: bool: True if valid Raises: ValidationError: Raises instead of returning False, this allows for questionary to properly display validator msg """ if query == 'make': exists_query = 'SELECT EXISTS (SELECT 1 FROM vehicles WHERE make = ?)' elif query == 'm_y': exists_query = ('SELECT EXISTS (SELECT 1 FROM vehicles ' 'WHERE model || " " || year = ?)') exists = cursor.execute(exists_query, (text, )).fetchone()[0] if not exists: raise ValidationError(message='Current entry does not match database!') return True
def validate(self, document): ok = re.match( r'^([01])?[-.\s]?\(?(\d{3})\)?' r'[-.\s]?(\d{3})[-.\s]?(\d{4})\s?' r'((?:#|ext\.?\s?|x\.?\s?)(?:\d+)?)?$', document.text) if not ok: raise ValidationError(message='Please enter a valid phone number', cursor_position=len( document.text)) # Move cursor to end
def validate(self, document): ok = re.match( r"^([01])?[-.\s]?\(?(\d{3})\)?" r"[-.\s]?(\d{3})[-.\s]?(\d{4})\s?" r"((?:#|ext\.?\s?|x\.?\s?)(?:\d+)?)?$", document.text, ) if not ok: raise ValidationError( message="Please enter a valid phone number", cursor_position=len(document.text), ) # Move cursor to end
def txt_validator(text): """ Checks if use_miles is a non-negative number for validation. Parameters: text (str): user's input, updates as it changes allowing for real time validation, unlike python's input() Returns: bool: True if valid Raises: ValidationError: Raises instead of returning False, this allows for questionary to properly display validator msg """ try: if float(text) < 0: raise ValidationError(message='Entry must be non-negative!') except ValueError: raise ValidationError( message='Entry must be a number!') from ValueError return True
def validate(self, document): """Validates number Args: document: User input to questionary prompt. Raises: ValidationError if number is not valid. Returns: True if number is valid. """ try: float(document.text) except ValueError: raise ValidationError( message='Please enter a number', cursor_position=len(document.text)) # Move cursor to end
def validate(self, document): """Validates business SID against similarity matrix provided. Args: document: User input to questionary prompt Raises: ValidationError if business SID is not found. Returns: True if SID is found in columns of similarity matrix provided. """ import builtins ok = False if document.text.upper() in builtins.sid_list: ok = True if not ok: raise ValidationError( message='SID not found in selected similarity matrix.', cursor_position=len(document.text)) # Move cursor to end
def validate(self, document): """Validates zipcodes Args: document: User input to questionary prompt References: Zipcodes: https://pypi.org/project/zipcodes/ Returns: True if user zipcode is valid number with len=5 and according to zipcodes package """ ok = False if document.text.isdigit() and len(document.text) == 5: # Only take non-Hawaii and non-Alaska zipcodes ok = zipcodes.is_real(document.text) and \ (zipcodes.matching(document.text)[0]['state'] != 'HI') and \ (zipcodes.matching(document.text)[0]['state'] != 'AK') if not ok: raise ValidationError( message='Please enter a valid zipcode', cursor_position=len(document.text)) # Move cursor to end
def validate(self, document): ok = regex.match('^(2[0-3]|[01]?[0-9]):([0-5]?[0-9])$', document.text) if not ok: raise ValidationError( message='Please enter a valid time in 24 hour format', cursor_position=len(document.text)) # Move cursor to end
def validate(self, document): if len(document.text) == 0: raise ValidationError(message="Please enter a value")
def validate(self, document): if len(document.text) != 64 or not all(c in string.hexdigits for c in document.text): raise ValidationError(message='Enter a valid private key (64 hexadecimal characters)')