0

I have a method to validate input:

def validate_user_input(*args):
    for item in args:
        if not re.match('^[a-zA-Z0-9_-]+$', item):

And I'm calling it like this:

validate_user_input(var1, var2, ..., var7)

But those are generated from user input, and some of those can be missing. What would be the proper way to do that, without creating tons of if statements?

Variables are assigned from a json input like so, and json input might not have some of the needed properties:

var1 = request.json.get('var1')

I assume they are <class 'NoneType'>

Here's the error: TypeError: expected string or buffer

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • so is, for example, `var2` not defined? How is it that these variables are getting their values? – Patrick Haugh Dec 15 '16 at 18:19
  • 2
    When you say 'missing', do you mean 'undefined' or `None`? – DYZ Dec 15 '16 at 18:19
  • Possible duplicate of http://stackoverflow.com/questions/919680/can-a-variable-number-of-arguments-be-passed-to-a-function. – Connor Gurney Dec 15 '16 at 18:20
  • in what case would you need a ton of if statements? you already have a loop to do a check for each argument, when would you need additional `if` statements? – Tadhg McDonald-Jensen Dec 15 '16 at 18:23
  • 1
    wouldn't it make sense to just have a list/set of the `'var1'` like strings and add them to a list under the condition they are actually present in `request.json` like: `variables = [request.json[v] for v in VAR_LIST if v in request.json]` – Tadhg McDonald-Jensen Dec 15 '16 at 18:26
  • This question is unclear. If all the arguments just need to match that alphanumeric regex (there are shorter ways) what does it mater if they are contained in args or not? You can just say 'bad input in given argument 3' or whatever. – kabanus Dec 15 '16 at 18:31

3 Answers3

1

If your request.json object is a dict or dict-like you can just pass a default value as second argument to get

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
1

If I understand correctly you are generating var_ variables by request.json.get('var_') which will either return a string which you want to validate or None if the field was missing.

If this is the case then you can just add a special case to validate_user_input for a None value:

def validate_user_input(*args):
    for item in args:
        if item is None:
            continue #this is acceptable, don't do anything with it
        elif not re.match('^[a-zA-Z0-9_-]+$', item):
            ...

Or it may make more sense to store all of the values you are interested in in a dictionary:

wanted_keys = {'var1','var2','var3'}
  ## set intersection works in python3
present_keys = wanted_keys & response.json.keys() 
  ## or for python 2 use a basic list comp
#present_keys = [key for key in response.json.keys() if key in wanted_keys] 
actual_data = {key: response.json[key] for key in present_keys}

Then you would pass actual_data.values() as the argument list to validate_user_input.

Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
  • thanks, I'm probably gonna use the get variant, but this one is nice also – 4c74356b41 Dec 15 '16 at 19:23
  • The get variant will still need a check for `None` in your validation function which is my primary suggested solution, the dictionary is how I'd do it since it gives you access to all the values as `actual_data.values()` and all the keys that were present as `actual_data.keys()` so it works more intuitively. Of course this is based on not knowing what the rest of your code looks like ;) – Tadhg McDonald-Jensen Dec 15 '16 at 19:26
-1

If it really is possible that some var-variables are undefined when you call validate_user_input, why not just initialize them all (e.g. to the empty string '' so that your regex fails) before actually defining them?

jmd_dk
  • 12,125
  • 9
  • 63
  • 94