2013-12-12 23:47:12 +01:00
|
|
|
'''
|
|
|
|
This module sets up a scheme for validating that arbitrary Python
|
|
|
|
objects are correctly typed. It is totally decoupled from Django,
|
|
|
|
composable, easily wrapped, and easily extended.
|
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
A validator takes two parameters--var_name and val--and raises an
|
2013-12-12 23:47:12 +01:00
|
|
|
error if val is not the correct type. The var_name parameter is used
|
2020-06-21 02:36:20 +02:00
|
|
|
to format error messages. Validators return the validated value when
|
|
|
|
there are no errors.
|
2013-12-12 23:47:12 +01:00
|
|
|
|
|
|
|
Example primitive validators are check_string, check_int, and check_bool.
|
|
|
|
|
|
|
|
Compound validators are created by check_list and check_dict. Note that
|
|
|
|
those functions aren't directly called for validation; instead, those
|
|
|
|
functions are called to return other functions that adhere to the validator
|
|
|
|
contract. This is similar to how Python decorators are often parameterized.
|
|
|
|
|
|
|
|
The contract for check_list and check_dict is that they get passed in other
|
|
|
|
validators to apply to their items. This allows you to build up validators
|
|
|
|
for arbitrarily complex validators. See ValidatorTestCase for example usage.
|
|
|
|
|
|
|
|
A simple example of composition is this:
|
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
check_list(check_string)('my_list', ['a', 'b', 'c'])
|
2013-12-12 23:47:12 +01:00
|
|
|
|
|
|
|
To extend this concept, it's simply a matter of writing your own validator
|
|
|
|
for any particular type of object.
|
2020-06-21 02:36:20 +02:00
|
|
|
|
2013-12-12 23:47:12 +01:00
|
|
|
'''
|
2019-01-14 07:28:04 +01:00
|
|
|
import re
|
2020-06-11 00:54:34 +02:00
|
|
|
from datetime import datetime
|
2020-06-21 02:36:20 +02:00
|
|
|
from typing import (
|
|
|
|
Any,
|
|
|
|
Callable,
|
|
|
|
Dict,
|
|
|
|
Iterable,
|
|
|
|
List,
|
|
|
|
Optional,
|
|
|
|
Set,
|
|
|
|
Tuple,
|
|
|
|
TypeVar,
|
|
|
|
Union,
|
|
|
|
cast,
|
|
|
|
overload,
|
|
|
|
)
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2018-04-08 09:50:05 +02:00
|
|
|
import ujson
|
2019-12-11 12:03:20 +01:00
|
|
|
from django.conf import settings
|
2017-04-10 08:06:10 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.core.validators import URLValidator, validate_email
|
|
|
|
from django.utils.translation import ugettext as _
|
2017-04-10 08:06:10 +02:00
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
from zerver.lib.request import JsonableError, ResultT
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.types import ProfileFieldData, Validator
|
2013-12-12 23:47:12 +01:00
|
|
|
|
2019-12-11 12:03:20 +01:00
|
|
|
FuncT = Callable[..., Any]
|
|
|
|
TypeStructure = TypeVar("TypeStructure")
|
|
|
|
|
2020-06-19 19:15:40 +02:00
|
|
|
USING_TYPE_STRUCTURE = settings.LOG_API_EVENT_TYPES
|
|
|
|
|
2019-12-11 12:03:20 +01:00
|
|
|
# The type_structure system is designed to support using the validators in
|
|
|
|
# test_events.py to create documentation for our event formats.
|
|
|
|
#
|
|
|
|
# Ultimately, it should be possible to do this with mypy rather than a
|
|
|
|
# parallel system.
|
|
|
|
def set_type_structure(type_structure: TypeStructure) -> Callable[[FuncT], Any]:
|
|
|
|
def _set_type_structure(func: FuncT) -> FuncT:
|
2020-06-19 19:15:40 +02:00
|
|
|
if USING_TYPE_STRUCTURE:
|
2020-04-22 04:13:37 +02:00
|
|
|
func.type_structure = type_structure # type: ignore[attr-defined] # monkey-patching
|
2019-12-11 12:03:20 +01:00
|
|
|
return func
|
|
|
|
return _set_type_structure
|
|
|
|
|
|
|
|
@set_type_structure("str")
|
2020-06-21 02:36:20 +02:00
|
|
|
def check_string(var_name: str, val: object) -> str:
|
2017-09-27 10:06:17 +02:00
|
|
|
if not isinstance(val, str):
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError(_('{var_name} is not a string').format(var_name=var_name))
|
|
|
|
return val
|
2013-12-12 23:47:12 +01:00
|
|
|
|
2019-12-11 12:03:20 +01:00
|
|
|
@set_type_structure("str")
|
2020-06-21 02:36:20 +02:00
|
|
|
def check_required_string(var_name: str, val: object) -> str:
|
|
|
|
s = check_string(var_name, val)
|
|
|
|
if not s.strip():
|
|
|
|
raise ValidationError(_("{item} cannot be blank.").format(item=var_name))
|
|
|
|
return s
|
2018-04-08 09:50:05 +02:00
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
def check_string_in(possible_values: Union[Set[str], List[str]]) -> Validator[str]:
|
2020-03-24 20:36:45 +01:00
|
|
|
@set_type_structure("str")
|
2020-06-21 02:36:20 +02:00
|
|
|
def validator(var_name: str, val: object) -> str:
|
|
|
|
s = check_string(var_name, val)
|
|
|
|
if s not in possible_values:
|
|
|
|
raise ValidationError(_("Invalid {var_name}").format(var_name=var_name))
|
|
|
|
return s
|
2020-03-24 20:36:45 +01:00
|
|
|
|
|
|
|
return validator
|
|
|
|
|
2019-12-11 12:03:20 +01:00
|
|
|
@set_type_structure("str")
|
2020-06-21 02:36:20 +02:00
|
|
|
def check_short_string(var_name: str, val: object) -> str:
|
2018-04-03 14:54:35 +02:00
|
|
|
return check_capped_string(50)(var_name, val)
|
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
def check_capped_string(max_length: int) -> Validator[str]:
|
2019-12-11 12:03:20 +01:00
|
|
|
@set_type_structure("str")
|
2020-06-21 02:36:20 +02:00
|
|
|
def validator(var_name: str, val: object) -> str:
|
|
|
|
s = check_string(var_name, val)
|
|
|
|
if len(s) > max_length:
|
|
|
|
raise ValidationError(_("{var_name} is too long (limit: {max_length} characters)").format(
|
|
|
|
var_name=var_name, max_length=max_length,
|
|
|
|
))
|
|
|
|
return s
|
2019-12-11 12:03:20 +01:00
|
|
|
|
2018-04-03 14:54:35 +02:00
|
|
|
return validator
|
2018-04-02 15:16:21 +02:00
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
def check_string_fixed_length(length: int) -> Validator[str]:
|
2019-12-11 12:03:20 +01:00
|
|
|
@set_type_structure("str")
|
2018-05-03 23:22:05 +02:00
|
|
|
def validator(var_name: str, val: object) -> Optional[str]:
|
2020-06-21 02:36:20 +02:00
|
|
|
s = check_string(var_name, val)
|
|
|
|
if len(s) != length:
|
|
|
|
raise ValidationError(_("{var_name} has incorrect length {length}; should be {target_length}").format(
|
|
|
|
var_name=var_name, target_length=length, length=len(s),
|
|
|
|
))
|
|
|
|
return s
|
|
|
|
|
2018-05-03 23:22:05 +02:00
|
|
|
return validator
|
|
|
|
|
2019-12-11 12:03:20 +01:00
|
|
|
@set_type_structure("str")
|
2020-06-21 02:36:20 +02:00
|
|
|
def check_long_string(var_name: str, val: object) -> str:
|
2018-04-03 14:54:35 +02:00
|
|
|
return check_capped_string(500)(var_name, val)
|
2018-04-02 15:16:21 +02:00
|
|
|
|
2019-12-11 12:03:20 +01:00
|
|
|
@set_type_structure("date")
|
2020-06-21 02:36:20 +02:00
|
|
|
def check_date(var_name: str, val: object) -> str:
|
2018-04-03 18:06:13 +02:00
|
|
|
if not isinstance(val, str):
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError(_('{var_name} is not a string').format(var_name=var_name))
|
2018-04-03 18:06:13 +02:00
|
|
|
try:
|
|
|
|
datetime.strptime(val, '%Y-%m-%d')
|
|
|
|
except ValueError:
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError(_('{var_name} is not a date').format(var_name=var_name))
|
|
|
|
return val
|
2018-04-03 18:06:13 +02:00
|
|
|
|
2019-12-11 12:03:20 +01:00
|
|
|
@set_type_structure("int")
|
2020-06-21 02:36:20 +02:00
|
|
|
def check_int(var_name: str, val: object) -> int:
|
2013-12-12 23:47:12 +01:00
|
|
|
if not isinstance(val, int):
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError(_('{var_name} is not an integer').format(var_name=var_name))
|
|
|
|
return val
|
2013-12-12 23:47:12 +01:00
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
def check_int_in(possible_values: List[int]) -> Validator[int]:
|
2019-12-11 12:03:20 +01:00
|
|
|
@set_type_structure("int")
|
2020-06-21 02:36:20 +02:00
|
|
|
def validator(var_name: str, val: object) -> int:
|
|
|
|
n = check_int(var_name, val)
|
|
|
|
if n not in possible_values:
|
|
|
|
raise ValidationError(_("Invalid {var_name}").format(var_name=var_name))
|
|
|
|
return n
|
2019-11-16 17:38:27 +01:00
|
|
|
|
|
|
|
return validator
|
|
|
|
|
2019-12-11 12:03:20 +01:00
|
|
|
@set_type_structure("float")
|
2020-06-21 02:36:20 +02:00
|
|
|
def check_float(var_name: str, val: object) -> float:
|
2017-03-24 05:23:41 +01:00
|
|
|
if not isinstance(val, float):
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError(_('{var_name} is not a float').format(var_name=var_name))
|
|
|
|
return val
|
2017-03-24 05:23:41 +01:00
|
|
|
|
2019-12-11 12:03:20 +01:00
|
|
|
@set_type_structure("bool")
|
2020-06-21 02:36:20 +02:00
|
|
|
def check_bool(var_name: str, val: object) -> bool:
|
2013-12-12 23:47:12 +01:00
|
|
|
if not isinstance(val, bool):
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError(_('{var_name} is not a boolean').format(var_name=var_name))
|
|
|
|
return val
|
2013-12-12 23:47:12 +01:00
|
|
|
|
2019-12-11 12:03:20 +01:00
|
|
|
@set_type_structure("str")
|
2020-06-21 02:36:20 +02:00
|
|
|
def check_color(var_name: str, val: object) -> str:
|
|
|
|
s = check_string(var_name, val)
|
2019-03-11 19:36:00 +01:00
|
|
|
valid_color_pattern = re.compile(r'^#([a-fA-F0-9]{3,6})$')
|
2020-06-21 02:36:20 +02:00
|
|
|
matched_results = valid_color_pattern.match(s)
|
2019-01-14 07:28:04 +01:00
|
|
|
if not matched_results:
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError(_('{var_name} is not a valid hex color code').format(var_name=var_name))
|
|
|
|
return s
|
2019-01-14 07:28:04 +01:00
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
def check_none_or(sub_validator: Validator[ResultT]) -> Validator[ResultT]:
|
2020-06-19 19:15:40 +02:00
|
|
|
if USING_TYPE_STRUCTURE:
|
2020-04-22 04:13:37 +02:00
|
|
|
type_structure = 'none_or_' + sub_validator.type_structure # type: ignore[attr-defined] # monkey-patching
|
2019-12-11 12:03:20 +01:00
|
|
|
else:
|
|
|
|
type_structure = None
|
|
|
|
|
|
|
|
@set_type_structure(type_structure)
|
2020-06-21 02:36:20 +02:00
|
|
|
def f(var_name: str, val: object) -> Optional[ResultT]:
|
2014-02-26 00:12:14 +01:00
|
|
|
if val is None:
|
2020-06-21 02:36:20 +02:00
|
|
|
return val
|
2014-02-26 00:12:14 +01:00
|
|
|
else:
|
|
|
|
return sub_validator(var_name, val)
|
|
|
|
return f
|
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
@overload
|
|
|
|
def check_list(sub_validator: None, length: Optional[int]=None) -> Validator[List[object]]:
|
|
|
|
...
|
|
|
|
@overload
|
|
|
|
def check_list(sub_validator: Validator[ResultT], length: Optional[int]=None) -> Validator[List[ResultT]]:
|
|
|
|
...
|
|
|
|
def check_list(sub_validator: Optional[Validator[ResultT]]=None, length: Optional[int]=None) -> Validator[List[ResultT]]:
|
2020-06-19 19:15:40 +02:00
|
|
|
if USING_TYPE_STRUCTURE:
|
2019-12-11 12:03:20 +01:00
|
|
|
if sub_validator:
|
2020-04-22 04:13:37 +02:00
|
|
|
type_structure = [sub_validator.type_structure] # type: ignore[attr-defined] # monkey-patching
|
2019-12-11 12:03:20 +01:00
|
|
|
else:
|
2020-04-22 04:13:37 +02:00
|
|
|
type_structure = 'list' # type: ignore[assignment] # monkey-patching
|
2019-12-11 12:03:20 +01:00
|
|
|
else:
|
2020-04-22 04:13:37 +02:00
|
|
|
type_structure = None # type: ignore[assignment] # monkey-patching
|
2019-12-11 12:03:20 +01:00
|
|
|
|
|
|
|
@set_type_structure(type_structure)
|
2020-06-21 02:36:20 +02:00
|
|
|
def f(var_name: str, val: object) -> List[ResultT]:
|
2013-12-12 23:47:12 +01:00
|
|
|
if not isinstance(val, list):
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError(_('{var_name} is not a list').format(var_name=var_name))
|
2013-12-12 23:47:12 +01:00
|
|
|
|
2013-12-18 17:59:02 +01:00
|
|
|
if length is not None and length != len(val):
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError(_('{container} should have exactly {length} items').format(
|
2020-06-15 23:22:24 +02:00
|
|
|
container=var_name, length=length,
|
|
|
|
))
|
2013-12-18 18:47:32 +01:00
|
|
|
|
2014-02-14 16:25:31 +01:00
|
|
|
if sub_validator:
|
|
|
|
for i, item in enumerate(val):
|
2020-06-13 08:59:37 +02:00
|
|
|
vname = f'{var_name}[{i}]'
|
2020-06-21 02:36:20 +02:00
|
|
|
valid_item = sub_validator(vname, item)
|
|
|
|
assert item is valid_item # To justify the unchecked cast below
|
2013-12-12 23:47:12 +01:00
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
return cast(List[ResultT], val)
|
2013-12-12 23:47:12 +01:00
|
|
|
return f
|
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
@overload
|
|
|
|
def check_dict(required_keys: Iterable[Tuple[str, Validator[object]]]=[],
|
|
|
|
optional_keys: Iterable[Tuple[str, Validator[object]]]=[],
|
|
|
|
*,
|
|
|
|
_allow_only_listed_keys: bool=False) -> Validator[Dict[str, object]]:
|
|
|
|
...
|
|
|
|
@overload
|
|
|
|
def check_dict(required_keys: Iterable[Tuple[str, Validator[ResultT]]]=[],
|
|
|
|
optional_keys: Iterable[Tuple[str, Validator[ResultT]]]=[],
|
|
|
|
*,
|
|
|
|
value_validator: Validator[ResultT],
|
|
|
|
_allow_only_listed_keys: bool=False) -> Validator[Dict[str, ResultT]]:
|
|
|
|
...
|
|
|
|
def check_dict(required_keys: Iterable[Tuple[str, Validator[ResultT]]]=[],
|
|
|
|
optional_keys: Iterable[Tuple[str, Validator[ResultT]]]=[],
|
|
|
|
*,
|
|
|
|
value_validator: Optional[Validator[ResultT]]=None,
|
|
|
|
_allow_only_listed_keys: bool=False) -> Validator[Dict[str, ResultT]]:
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
type_structure: Dict[str, Any] = {}
|
2019-12-11 12:03:20 +01:00
|
|
|
|
|
|
|
@set_type_structure(type_structure)
|
2020-06-21 02:36:20 +02:00
|
|
|
def f(var_name: str, val: object) -> Dict[str, ResultT]:
|
2013-12-12 23:47:12 +01:00
|
|
|
if not isinstance(val, dict):
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError(_('{var_name} is not a dict').format(var_name=var_name))
|
|
|
|
|
|
|
|
for k in val:
|
|
|
|
check_string(f'{var_name} key', k)
|
2013-12-12 23:47:12 +01:00
|
|
|
|
|
|
|
for k, sub_validator in required_keys:
|
|
|
|
if k not in val:
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError(_('{key_name} key is missing from {var_name}').format(
|
2020-06-15 23:22:24 +02:00
|
|
|
key_name=k, var_name=var_name,
|
|
|
|
))
|
2020-06-10 06:41:04 +02:00
|
|
|
vname = f'{var_name}["{k}"]'
|
2020-06-21 02:36:20 +02:00
|
|
|
sub_validator(vname, val[k])
|
2020-06-19 19:15:40 +02:00
|
|
|
if USING_TYPE_STRUCTURE:
|
2020-04-22 04:13:37 +02:00
|
|
|
type_structure[k] = sub_validator.type_structure # type: ignore[attr-defined] # monkey-patching
|
2013-12-12 23:47:12 +01:00
|
|
|
|
2019-01-22 19:03:21 +01:00
|
|
|
for k, sub_validator in optional_keys:
|
|
|
|
if k in val:
|
2020-06-10 06:41:04 +02:00
|
|
|
vname = f'{var_name}["{k}"]'
|
2020-06-21 02:36:20 +02:00
|
|
|
sub_validator(vname, val[k])
|
2020-06-19 19:15:40 +02:00
|
|
|
if USING_TYPE_STRUCTURE:
|
2020-04-22 04:13:37 +02:00
|
|
|
type_structure[k] = sub_validator.type_structure # type: ignore[attr-defined] # monkey-patching
|
2019-01-22 19:03:21 +01:00
|
|
|
|
2018-01-07 18:55:39 +01:00
|
|
|
if value_validator:
|
|
|
|
for key in val:
|
2020-06-10 06:41:04 +02:00
|
|
|
vname = f'{var_name} contains a value that'
|
2020-06-21 02:36:20 +02:00
|
|
|
valid_value = value_validator(vname, val[key])
|
|
|
|
assert val[key] is valid_value # To justify the unchecked cast below
|
2020-06-19 19:15:40 +02:00
|
|
|
if USING_TYPE_STRUCTURE:
|
2020-04-22 04:13:37 +02:00
|
|
|
type_structure['any'] = value_validator.type_structure # type: ignore[attr-defined] # monkey-patching
|
2018-01-07 18:55:39 +01:00
|
|
|
|
2017-03-26 08:11:45 +02:00
|
|
|
if _allow_only_listed_keys:
|
2020-04-09 21:51:58 +02:00
|
|
|
required_keys_set = {x[0] for x in required_keys}
|
|
|
|
optional_keys_set = {x[0] for x in optional_keys}
|
2019-01-22 19:03:21 +01:00
|
|
|
delta_keys = set(val.keys()) - required_keys_set - optional_keys_set
|
2017-03-26 08:11:45 +02:00
|
|
|
if len(delta_keys) != 0:
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError(_("Unexpected arguments: {}").format(", ".join(list(delta_keys))))
|
2017-03-26 08:11:45 +02:00
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
return cast(Dict[str, ResultT], val)
|
2013-12-12 23:47:12 +01:00
|
|
|
|
|
|
|
return f
|
2014-02-04 20:52:02 +01:00
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
def check_dict_only(required_keys: Iterable[Tuple[str, Validator[ResultT]]],
|
|
|
|
optional_keys: Iterable[Tuple[str, Validator[ResultT]]]=[]) -> Validator[Dict[str, ResultT]]:
|
|
|
|
return cast(
|
|
|
|
Validator[Dict[str, ResultT]],
|
|
|
|
check_dict(required_keys, optional_keys, _allow_only_listed_keys=True),
|
|
|
|
)
|
2017-03-26 08:11:45 +02:00
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
def check_union(allowed_type_funcs: Iterable[Validator[ResultT]]) -> Validator[ResultT]:
|
2014-02-14 19:55:35 +01:00
|
|
|
"""
|
|
|
|
Use this validator if an argument is of a variable type (e.g. processing
|
|
|
|
properties that might be strings or booleans).
|
|
|
|
|
|
|
|
`allowed_type_funcs`: the check_* validator functions for the possible data
|
|
|
|
types for this variable.
|
|
|
|
"""
|
2019-12-11 12:03:20 +01:00
|
|
|
|
2020-06-19 19:15:40 +02:00
|
|
|
if USING_TYPE_STRUCTURE:
|
2020-06-10 06:41:04 +02:00
|
|
|
type_structure = f'any("{[x.type_structure for x in allowed_type_funcs]}")' # type: ignore[attr-defined] # monkey-patching
|
2019-12-11 12:03:20 +01:00
|
|
|
else:
|
2020-04-22 04:13:37 +02:00
|
|
|
type_structure = None # type: ignore[assignment] # monkey-patching
|
2019-12-11 12:03:20 +01:00
|
|
|
|
|
|
|
@set_type_structure(type_structure)
|
2020-06-21 02:36:20 +02:00
|
|
|
def enumerated_type_check(var_name: str, val: object) -> ResultT:
|
2014-02-14 19:55:35 +01:00
|
|
|
for func in allowed_type_funcs:
|
2020-06-21 02:36:20 +02:00
|
|
|
try:
|
|
|
|
return func(var_name, val)
|
|
|
|
except ValidationError:
|
|
|
|
pass
|
|
|
|
raise ValidationError(_('{var_name} is not an allowed_type').format(var_name=var_name))
|
2014-02-14 19:55:35 +01:00
|
|
|
return enumerated_type_check
|
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
def equals(expected_val: ResultT) -> Validator[ResultT]:
|
2020-06-10 06:41:04 +02:00
|
|
|
@set_type_structure(f'equals("{str(expected_val)}")')
|
2020-06-21 02:36:20 +02:00
|
|
|
def f(var_name: str, val: object) -> ResultT:
|
2014-02-04 20:52:02 +01:00
|
|
|
if val != expected_val:
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError(_('{variable} != {expected_value} ({value} is wrong)').format(
|
2020-06-15 23:22:24 +02:00
|
|
|
variable=var_name, expected_value=expected_val, value=val,
|
|
|
|
))
|
2020-06-21 02:36:20 +02:00
|
|
|
return cast(ResultT, val)
|
2014-02-04 20:52:02 +01:00
|
|
|
return f
|
2017-04-10 08:06:10 +02:00
|
|
|
|
2019-12-11 12:03:20 +01:00
|
|
|
@set_type_structure('str')
|
2018-05-11 01:40:23 +02:00
|
|
|
def validate_login_email(email: str) -> None:
|
2017-04-10 08:06:10 +02:00
|
|
|
try:
|
|
|
|
validate_email(email)
|
|
|
|
except ValidationError as err:
|
|
|
|
raise JsonableError(str(err.message))
|
2017-06-11 09:11:59 +02:00
|
|
|
|
2019-12-11 12:03:20 +01:00
|
|
|
@set_type_structure('str')
|
2020-06-21 02:36:20 +02:00
|
|
|
def check_url(var_name: str, val: object) -> str:
|
2018-03-16 06:22:14 +01:00
|
|
|
# First, ensure val is a string
|
2020-06-21 02:36:20 +02:00
|
|
|
s = check_string(var_name, val)
|
2018-03-16 06:22:14 +01:00
|
|
|
# Now, validate as URL
|
2017-06-11 09:11:59 +02:00
|
|
|
validate = URLValidator()
|
|
|
|
try:
|
2020-06-21 02:36:20 +02:00
|
|
|
validate(s)
|
|
|
|
return s
|
2018-05-24 16:41:34 +02:00
|
|
|
except ValidationError:
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError(_('{var_name} is not a URL').format(var_name=var_name))
|
2018-04-08 09:50:05 +02:00
|
|
|
|
2019-12-11 12:03:20 +01:00
|
|
|
@set_type_structure('str')
|
2020-06-21 02:36:20 +02:00
|
|
|
def check_external_account_url_pattern(var_name: str, val: object) -> str:
|
|
|
|
s = check_string(var_name, val)
|
|
|
|
|
|
|
|
if s.count('%(username)s') != 1:
|
|
|
|
raise ValidationError(_('Malformed URL pattern.'))
|
|
|
|
url_val = s.replace('%(username)s', 'username')
|
|
|
|
|
|
|
|
check_url(var_name, url_val)
|
|
|
|
return s
|
|
|
|
|
|
|
|
def validate_choice_field_data(field_data: ProfileFieldData) -> Dict[str, Dict[str, str]]:
|
2018-04-08 09:50:05 +02:00
|
|
|
"""
|
|
|
|
This function is used to validate the data sent to the server while
|
|
|
|
creating/editing choices of the choice field in Organization settings.
|
|
|
|
"""
|
|
|
|
validator = check_dict_only([
|
|
|
|
('text', check_required_string),
|
|
|
|
('order', check_required_string),
|
|
|
|
])
|
|
|
|
|
|
|
|
for key, value in field_data.items():
|
|
|
|
if not key.strip():
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError(_("'{item}' cannot be blank.").format(item='value'))
|
2018-04-08 09:50:05 +02:00
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
valid_value = validator('field_data', value)
|
|
|
|
assert value is valid_value # To justify the unchecked cast below
|
2018-04-08 09:50:05 +02:00
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
return cast(Dict[str, Dict[str, str]], field_data)
|
2018-04-08 09:50:05 +02:00
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
def validate_choice_field(var_name: str, field_data: str, value: object) -> str:
|
2018-04-08 09:50:05 +02:00
|
|
|
"""
|
|
|
|
This function is used to validate the value selected by the user against a
|
|
|
|
choice field. This is not used to validate admin data.
|
|
|
|
"""
|
2020-06-21 02:36:20 +02:00
|
|
|
s = check_string(var_name, value)
|
2018-04-08 09:50:05 +02:00
|
|
|
field_data_dict = ujson.loads(field_data)
|
2020-06-21 02:36:20 +02:00
|
|
|
if s not in field_data_dict:
|
2018-04-08 09:50:05 +02:00
|
|
|
msg = _("'{value}' is not a valid choice for '{field_name}'.")
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError(msg.format(value=value, field_name=var_name))
|
|
|
|
return s
|
2018-05-21 15:23:46 +02:00
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
def check_widget_content(widget_content: object) -> Dict[str, Any]:
|
2018-05-21 15:23:46 +02:00
|
|
|
if not isinstance(widget_content, dict):
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError('widget_content is not a dict')
|
2018-05-21 15:23:46 +02:00
|
|
|
|
|
|
|
if 'widget_type' not in widget_content:
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError('widget_type is not in widget_content')
|
2018-05-21 15:23:46 +02:00
|
|
|
|
|
|
|
if 'extra_data' not in widget_content:
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError('extra_data is not in widget_content')
|
2018-05-21 15:23:46 +02:00
|
|
|
|
|
|
|
widget_type = widget_content['widget_type']
|
|
|
|
extra_data = widget_content['extra_data']
|
|
|
|
|
|
|
|
if not isinstance(extra_data, dict):
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError('extra_data is not a dict')
|
2018-05-21 15:23:46 +02:00
|
|
|
|
|
|
|
if widget_type == 'zform':
|
|
|
|
|
|
|
|
if 'type' not in extra_data:
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError('zform is missing type field')
|
2018-05-21 15:23:46 +02:00
|
|
|
|
|
|
|
if extra_data['type'] == 'choices':
|
|
|
|
check_choices = check_list(
|
|
|
|
check_dict([
|
|
|
|
('short_name', check_string),
|
|
|
|
('long_name', check_string),
|
|
|
|
('reply', check_string),
|
|
|
|
]),
|
|
|
|
)
|
|
|
|
|
|
|
|
checker = check_dict([
|
|
|
|
('heading', check_string),
|
|
|
|
('choices', check_choices),
|
|
|
|
])
|
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
checker('extra_data', extra_data)
|
2018-05-21 15:23:46 +02:00
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
return widget_content
|
2018-05-21 15:23:46 +02:00
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError('unknown zform type: ' + extra_data['type'])
|
2018-05-21 15:23:46 +02:00
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError('unknown widget type: ' + widget_type)
|
2019-02-09 23:57:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Converter functions for use with has_request_variables
|
2019-12-11 12:03:20 +01:00
|
|
|
@set_type_structure('int')
|
2019-02-09 23:57:54 +01:00
|
|
|
def to_non_negative_int(s: str, max_int_size: int=2**32-1) -> int:
|
|
|
|
x = int(s)
|
|
|
|
if x < 0:
|
|
|
|
raise ValueError("argument is negative")
|
|
|
|
if x > max_int_size:
|
2020-06-10 06:41:04 +02:00
|
|
|
raise ValueError(f'{x} is too large (max {max_int_size})')
|
2019-02-09 23:57:54 +01:00
|
|
|
return x
|
2019-06-08 23:19:56 +02:00
|
|
|
|
2020-05-07 13:19:54 +02:00
|
|
|
def to_positive_or_allowed_int(allowed_integer: int) -> Callable[[str], int]:
|
|
|
|
@set_type_structure('int')
|
|
|
|
def convertor(s: str) -> int:
|
|
|
|
x = int(s)
|
|
|
|
if x == allowed_integer:
|
|
|
|
return x
|
|
|
|
if x == 0:
|
|
|
|
raise ValueError("argument is 0")
|
|
|
|
return to_non_negative_int(s)
|
|
|
|
return convertor
|
|
|
|
|
2019-12-11 12:03:20 +01:00
|
|
|
@set_type_structure('any(List[int], str)]')
|
2020-06-21 02:36:20 +02:00
|
|
|
def check_string_or_int_list(var_name: str, val: object) -> Union[str, List[int]]:
|
2019-06-08 23:19:56 +02:00
|
|
|
if isinstance(val, str):
|
2020-06-21 02:36:20 +02:00
|
|
|
return val
|
2019-06-08 23:19:56 +02:00
|
|
|
|
|
|
|
if not isinstance(val, list):
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError(_('{var_name} is not a string or an integer list').format(var_name=var_name))
|
2019-06-08 23:19:56 +02:00
|
|
|
|
|
|
|
return check_list(check_int)(var_name, val)
|
2019-07-13 01:48:04 +02:00
|
|
|
|
2019-12-11 12:03:20 +01:00
|
|
|
@set_type_structure('any(int, str)')
|
2020-06-21 02:36:20 +02:00
|
|
|
def check_string_or_int(var_name: str, val: object) -> Union[str, int]:
|
2019-07-13 01:48:04 +02:00
|
|
|
if isinstance(val, str) or isinstance(val, int):
|
2020-06-21 02:36:20 +02:00
|
|
|
return val
|
2019-07-13 01:48:04 +02:00
|
|
|
|
2020-06-21 02:36:20 +02:00
|
|
|
raise ValidationError(_('{var_name} is not a string or integer').format(var_name=var_name))
|