1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
20 from .formatting import string_list_as_string
23 class ValidatorMixin(object):
25 A mix0in that should be added to classes that require validating user input.
28 _ARGUMENT_TYPE_MESSAGE = '{name} argument must be {type} based, got {arg!r}'
29 _ARGUMENT_CHOICE_MESSAGE = '{name} argument must be in {choices}, got {arg!r}'
32 def validate_in_choice(cls, name, argument, choices):
34 Validate ``argument`` is in ``choices``
36 if argument not in choices:
37 raise TypeError(cls._ARGUMENT_CHOICE_MESSAGE.format(
38 name=name, choices=choices, arg=argument))
41 def validate_type(cls, argument_name, argument, expected_type):
43 Validate ``argument`` is a subclass of ``expected_type``
45 if not issubclass(argument, expected_type):
46 raise TypeError(cls._ARGUMENT_TYPE_MESSAGE.format(
47 name=argument_name, type=expected_type, arg=argument))
50 def validate_instance(cls, argument_name, argument, expected_type):
52 Validate ``argument`` is a instance of ``expected_type``
54 if not isinstance(argument, expected_type):
55 raise TypeError(cls._ARGUMENT_TYPE_MESSAGE.format(
56 name=argument_name, type=expected_type, arg=argument))
59 def validate_callable(cls, argument_name, argument):
61 Validate ``argument`` is callable
63 if not callable(argument):
64 raise TypeError(cls._ARGUMENT_TYPE_MESSAGE.format(
65 name=argument_name, type='callable', arg=argument))
68 def validate_function_arguments(func, func_kwargs):
70 Validates all required arguments are supplied to ``func`` and that no additional arguments are
76 has_kwargs = func.func_code.co_flags & _kwargs_flags != 0
77 args_count = func.func_code.co_argcount
79 # all args without the ones with default values
80 args = func.func_code.co_varnames[:args_count]
81 non_default_args = args[:len(args) - len(func.func_defaults)] if func.func_defaults else args
83 # Check if any args without default values is missing in the func_kwargs
84 for arg in non_default_args:
85 if arg not in func_kwargs:
87 'The argument "{arg}" is not provided and does not have a default value for '
88 'function "{func.__name__}"'.format(arg=arg, func=func))
90 # check if there are any extra kwargs
91 extra_kwargs = [arg for arg in func_kwargs.keys() if arg not in args]
93 # assert that the function has kwargs
94 if extra_kwargs and not has_kwargs:
95 raise ValueError("The following extra kwargs were supplied: {extra_kwargs}".format(
96 extra_kwargs=string_list_as_string(extra_kwargs)