def __init__(self, context): super(Regex, self).__init__(context) if not self.context: raise exc.ValidationError( 'Context is necessary for the validator "{}"'.format( self.name)) if not isinstance(self.context, basestring): raise exc.ValidationError( 'Context must be of instance basestring(). Got {}'.format( type(self.context))) else: try: re.compile(self.context) except re.error: raise exc.ValidationError( 'Context must be a valid regex'.format())
def validate_value(properties, prop, value): validator = properties[prop] if isinstance(validator.get('values'), list): valid = value in properties.get(prop, {}).get('values', {}) if not valid: raise exc.ValidationError( 'Value "{}" not allowed for {}. Must be any of {}'.format( value, prop, properties[prop])) elif isinstance(validator.get('values')(), Validator): v = validator.get('values')() if not v.validate(value): raise exc.ValidationError( 'Value "{}" not allowed for {}. Must be any of {}'.format( value, prop, v.name)) return value
def validate_property(properties, raw_prop): # Check that the given property is valid if not properties.get(raw_prop, None): raise exc.ValidationError( 'Property "{}" not allowed. Must be any of {}'.format( raw_prop, properties.keys())) return raw_prop
def get_truth(inp, relate, val=None): """ Perform a comparison based on `relate` Args: inp: str - input value relate: val: Returns: """ if inp is None: raise exc.ValidationError('get_truth: actual value not set.') if not relate: raise exc.ValidationError('get_truth: matcher not set.') inp = inp.strip('\n\t ') if isinstance(inp, basestring) else inp # empty/not empty is not part of the operators. suite if isinstance(relate, basestring) and 'is empty' in relate: return len(inp) == 0 if isinstance(relate, basestring) and 'is not empty' in relate: return len(inp) # from now on, we expect val to be not None if val is None: raise exc.ValidationError('get_truth: expected value not set.') # special case: 'contains' not is not covered by the operator module if relate == 'contains_not': return val not in inp # for operator.contains, the order of arguments is reversed if relate.__name__ == 'contains': try: return relate(inp, val) except TypeError as e: raise exc.ValidationError(e) try: inp_integer = int(inp) val_integer = int(val) return relate(val_integer, inp_integer) except ValueError: pass return relate(val, inp)
def validate_port(raw_entity): # Check that raw_entity is valid # 0 < port <= 65535 # \d+ entity = re.search('(\d+)[/]?(\w{,3})?', raw_entity, re.IGNORECASE) if not entity: raise exc.ValidationError('Port "{}" not valid.'.format(raw_entity)) port = int(entity.group(1)) if entity else None protocol = entity.group(2) if entity.groups is not None else None if not (0 < port <= 65535): raise exc.ValidationError( 'Port "{}" not valid. Must be between 1 and 65535'.format(port)) if len(protocol) > 0 and lower(protocol) not in ['tcp', 'udp']: raise exc.ValidationError( 'Protocol "{}" not valid. Only udp and tcp are supported'.format( protocol)) return port, protocol
def call_validator(entity, validator, context=None, override=None): if context is not None: v = validator(context=context) elif override is not None: v = validator(override=override) elif override is not None and context is not None: v = validator(context=context, override=override) else: v = validator() if isinstance(v, Validator): res = v.validate(entity) if res: return True else: raise exc.ValidationError('validators.{}: "{}" not valid'.format( v.name, entity)) else: raise TypeError( 'call_validator must be called with validator callable. Got: {}'. format(entity))
def validate_against_regex(context, raw_value, regex): found = re.search(regex, raw_value) if not found: raise exc.ValidationError('Value "{}" not valid for {}'.format( raw_value, context))
def __init__(self, context): super(Service, self).__init__(context) if not self.context: raise exc.ValidationError( 'Context is necessary for the validator "{}"'.format( self.name))