mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	validators: Add check_int_in validator.
This is a useful helper for our enum format fields where we want to only allow a fixed list of integer values.
This commit is contained in:
		@@ -30,7 +30,7 @@ import ujson
 | 
			
		||||
from django.utils.translation import ugettext as _
 | 
			
		||||
from django.core.exceptions import ValidationError
 | 
			
		||||
from django.core.validators import validate_email, URLValidator
 | 
			
		||||
from typing import Iterable, Optional, Tuple, cast
 | 
			
		||||
from typing import Iterable, Optional, Tuple, cast, List
 | 
			
		||||
 | 
			
		||||
from datetime import datetime
 | 
			
		||||
from zerver.lib.request import JsonableError
 | 
			
		||||
@@ -92,6 +92,17 @@ def check_int(var_name: str, val: object) -> Optional[str]:
 | 
			
		||||
        return _('%s is not an integer') % (var_name,)
 | 
			
		||||
    return None
 | 
			
		||||
 | 
			
		||||
def check_int_in(possible_values: List[int]) -> Validator:
 | 
			
		||||
    def validator(var_name: str, val: object) -> Optional[str]:
 | 
			
		||||
        not_int = check_int(var_name, val)
 | 
			
		||||
        if not_int is not None:
 | 
			
		||||
            return not_int
 | 
			
		||||
        if val not in possible_values:
 | 
			
		||||
            return _("Invalid %s") % (var_name,)
 | 
			
		||||
        return None
 | 
			
		||||
 | 
			
		||||
    return validator
 | 
			
		||||
 | 
			
		||||
def check_float(var_name: str, val: object) -> Optional[str]:
 | 
			
		||||
    if not isinstance(val, float):
 | 
			
		||||
        return _('%s is not a float') % (var_name,)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user