def _validate_integer(_value: Any, **kwargs: Any) -> int: """Validate integer value. Parameters ---------- _value Some value kwargs Other metadata """ if isinstance(_value, str): # try converting h = HEX_REGEX.match(_value) b = BIN_REGEX.match(_value) if h is not None: if h.group(1) is None and b is not None: # is actually binary return int(h.group(2), 2) return int(h.group(2), 16) raise ValidationError("cannot validate as integer") elif isinstance(_value, bool): raise ValidationError("cannot validate as integer, got boolean") elif isinstance(_value, int): return _value raise ValidationError("cannot validate as integer")
def _wrapper(_value, **kwargs): try: fn(_value, **kwargs) except ValidationError: return _value raise ValidationError("validation passed where it shouldn't have")
def validate(self, _value, **kwargs): """Perform validation.""" if _value not in self._choices: choices = ", ".join(self._choices) raise ValidationError( f"value '{_value}' is not a valid choice, choose from [{choices}]" ) return _value
def validate(self, _value, **kwargs): """Perform validation.""" if (self._start is not None and _value < self._start) or (self._end is not None and _value > self._end): raise ValidationError("value out of [{}, {}] range".format( self._start, self._end)) return _value
def validate_null(_value: Any, **kwargs: Any) -> None: """Validate null value. Parameters --------- _value Some value kwargs Other metadata """ if _value is not None: raise ValidationError("value is not null") return _value
def validate(self, _value, **kwargs): """Perform validation.""" if self._condition is not None: try: _ = ( DEFAULT_VALIDATOR_BY_TYPE[self._condition].validate( _value, **kwargs ) if isinstance(self._condition, type) else self._condition(_value, **kwargs) ) except ValidationError: return _value raise ValidationError("validation passed where it shouldn't have")
def validate(self, _value, **kwargs): """Perform validation.""" for condition in self._conditions: try: if isinstance(condition, type): ret = DEFAULT_VALIDATOR_BY_TYPE[condition].validate( _value, **kwargs ) elif isinstance(condition, Validator): ret = condition.validate(_value, **kwargs) elif condition is None: if _value is not None: continue ret = _value else: ret = condition(_value, **kwargs) return ret except ValidationError: continue raise ValidationError("validate union failed for all conditions")
def validate(self, _value, **kwargs): """Perform validation.""" if not isinstance(_value, str): # ignore if not string return _value req_keys = re.findall(self.REPLACE_PATTERN, _value) req_keys = [(key, self.get_key_type(rel)) for rel, key in req_keys] true_depends = [] soft_depends = {} for req_key, ktype in req_keys: key_accessor = req_key.split("::") if ktype == "parent": if "_parent" not in kwargs or kwargs["_parent"] is None: raise ValidationError( "key requires a parent configuration which is not available" ) key_src = kwargs["_parent"] elif ktype == "normal": true_depends.append(key_accessor[0]) continue else: # top-level parent = kwargs["_parent"] if parent is None: # already at top-level key_src = kwargs else: key_src = None while parent is not None: key_src = parent parent = parent["_parent"] if key_accessor[0] not in key_src: raise ValidationError( f"couldn't find key {req_key} in referred configuration level" ) if len(key_accessor) > 1: for accessor in key_accessor: if not isinstance(key_src, dict): raise ValidationError( "key is not dictionary, cannot access member") if accessor not in key_src: raise FragmentError(f"member {accessor} not found") key_src = key_src[accessor] soft_depends[req_key] = key_src else: soft_depends[req_key] = key_src[req_key] @KeyDependency(*true_depends, validate_after=True) def _validate(_value, **kwargs): for req_key, ktype in req_keys: value_src = soft_depends if ktype != "normal" else kwargs key_accessor = req_key.split("::") if len(key_accessor) > 1 and ktype == "normal": if key_accessor[0] not in value_src: raise FragmentError( f"key {key_accessor[0]} is not available") if not isinstance(value_src[key_accessor[0]], dict): raise FragmentError( "sub-key requested from key which is not dictionary" ) for accessor in key_accessor: value_src = value_src[accessor] elif req_key not in value_src: raise FragmentError(f"key {req_key} is not available") else: value_src = value_src[req_key] _value = re.sub( r"\$\{((?:\.\.)|:)?" + req_key + r"\}", str(value_src), _value, ) # convert value back if numeric (and if possible) if isinstance(value_src, int): try: return int(_value) except ValueError: pass elif isinstance(value_src, float): try: return float(_value) except ValueError: pass return _value return _validate(_value, **kwargs)
def validate(self, _value, **kwargs): """Perform validation.""" if not isinstance(_value, self.target_types): raise ValidationError(f"value has unexpected type") return _value