Raises an exception if the input_values are invalid Args: validations (dict): the validation dictionary. input_variable_path (tuple): the path to the input variable. input_values (list/str/int/float/date/datetime): the values that we are checking. con
(
validations, input_variable_path, input_values, configuration=None
)
| 886 | |
| 887 | |
| 888 | def check_validations( |
| 889 | validations, input_variable_path, input_values, configuration=None |
| 890 | ): |
| 891 | """Raises an exception if the input_values are invalid |
| 892 | |
| 893 | Args: |
| 894 | validations (dict): the validation dictionary. |
| 895 | input_variable_path (tuple): the path to the input variable. |
| 896 | input_values (list/str/int/float/date/datetime): the values that we |
| 897 | are checking. |
| 898 | configuration (Configuration): the configuration class. |
| 899 | """ |
| 900 | |
| 901 | if input_values is None: |
| 902 | return |
| 903 | |
| 904 | current_validations = validations[input_variable_path] |
| 905 | if ( |
| 906 | is_json_validation_enabled("multipleOf", configuration) |
| 907 | and "multiple_of" in current_validations |
| 908 | and isinstance(input_values, (int, float)) |
| 909 | and not (float(input_values) / current_validations["multiple_of"]).is_integer() |
| 910 | ): |
| 911 | # Note 'multipleOf' will be as good as the floating point arithmetic. |
| 912 | raise ApiValueError( |
| 913 | "Invalid value for `%s`, value must be a multiple of " |
| 914 | "`%s`" % (input_variable_path[0], current_validations["multiple_of"]) |
| 915 | ) |
| 916 | |
| 917 | if ( |
| 918 | is_json_validation_enabled("maxLength", configuration) |
| 919 | and "max_length" in current_validations |
| 920 | and len(input_values) > current_validations["max_length"] |
| 921 | ): |
| 922 | raise ApiValueError( |
| 923 | "Invalid value for `%s`, length must be less than or equal to " |
| 924 | "`%s`" % (input_variable_path[0], current_validations["max_length"]) |
| 925 | ) |
| 926 | |
| 927 | if ( |
| 928 | is_json_validation_enabled("minLength", configuration) |
| 929 | and "min_length" in current_validations |
| 930 | and len(input_values) < current_validations["min_length"] |
| 931 | ): |
| 932 | raise ApiValueError( |
| 933 | "Invalid value for `%s`, length must be greater than or equal to " |
| 934 | "`%s`" % (input_variable_path[0], current_validations["min_length"]) |
| 935 | ) |
| 936 | |
| 937 | if ( |
| 938 | is_json_validation_enabled("maxItems", configuration) |
| 939 | and "max_items" in current_validations |
| 940 | and len(input_values) > current_validations["max_items"] |
| 941 | ): |
| 942 | raise ApiValueError( |
| 943 | "Invalid value for `%s`, number of items must be less than or " |
| 944 | "equal to `%s`" % (input_variable_path[0], current_validations["max_items"]) |
| 945 | ) |
no test coverage detected