def match_ref(self, key_name, ref_key_value): if not key_name in self._key_index: raise ValidationException('No key for %s exists' % key_name, key_name) for key_path in self._key_index[key_name]: key = '%s:%s' % (key_name, key_path) for key_value, instance_path in self._keys[key].items(): if key_value == ref_key_value: return instance_path raise ValidationException( 'Could not match ref %s for %s' % (ref_key_value, key_name), ref_key_value)
def add_value(self, key_names, target_path, key_value, key_path): if isinstance(key_names, string_types): key_names_list = [key_names] else: key_names_list = key_names for key_name in key_names_list: key = '%s:%s' % (key_name, target_path) if self.in_keys(key_name, target_path): if key_value in self._keys[key]: msg = 'Duplicate key value %s for %s at %s' % ( key_value, key_name, key_path) raise ValidationException(msg, key_value) self._keys[key][key_value] = key_path return True msg = 'Could not find target path %s for key name(s) %s' % ( target_path, ', '.join(key_names_list)) raise ValidationException(msg, key_value)
def to_python(self, elements_list, **kwargs): if elements_list is None and not self.not_empty: pass else: if not isinstance(elements_list, list): raise ValidationException('Expected child elements.', elements_list) if self.initial: self.initial.to_python(None, **kwargs) for element in elements_list: if not isinstance(element, Element): raise ValidationException( 'All values must be of type Element.', elements_list) el_dict = OrderedDict() for element in elements_list: if element.tag in el_dict: if isinstance(el_dict[element.tag], list): el_dict[element.tag].append(element) else: el_dict[element.tag] = [el_dict[element.tag], element] else: el_dict[element.tag] = element tag = '(%s)' % el_dict['tag'].value if 'tag' in el_dict else '' parent_path = '/'.join(elements_list[0].path.split('/')[:-1]) msg = 'Validating: %s for element <%s%s> with keys: %s' % ( parent_path, self.tag, tag, ', '.join(el_dict.keys())) utils.debug(logger, msg) sequence = self.match_sequence(el_dict.keys(), parent_path) result = [] if sequence: for element_schema in sequence: field_element = el_dict[element_schema.tag] if isinstance(field_element, list): validated_elements = [ element_schema.to_python(item, **kwargs) for item in field_element ] result.extend(validated_elements) else: validated_element = element_schema.to_python( field_element, **kwargs) result.append(validated_element) return result
def add_key(self, key_names, target_path): if isinstance(key_names, list): key_names_list = key_names else: key_names_list = [key_names] for key_name in key_names_list: key = '%s:%s' % (key_name, target_path) if key in self._keys: raise ValidationException('Key %s does already exist.' % key, target_path) if not key_name in self._key_index: self._key_index[key_name] = [target_path] else: self._key_index[key_name].append(target_path) self._keys[key] = {}
def to_python(self, value, **kwargs): key_value, path, stores = get_value_path_stores(value, **kwargs) if not key_value: if not self.not_empty: return key_value # self.not_empty raise ValidationException(self.messages['emptyValue'], value) if self.string_validator_instance: string_value = self.string_validator_instance.to_python(key_value) else: string_value = key_value target_path = '/'.join(path.split('/')[:-self.level]) if self.refer_key_name: stores.refStore.add_key_ref(self.refer_key_name, key_value, path) self.add_value(stores, target_path, string_value, path) return string_value
def test_validation_exception__str__pass(): iv = ValidationException('message', 42) nose.tools.eq_(str(iv), 'message, value:42')
def set_target(self, ref_path, target_path): if ref_path in self._targets: raise ValidationException('Target for ref_path already exists.', ref_path) self._targets[ref_path] = target_path
def add_key_ref(self, key_name, key_value, ref_path): if not key_value: raise ValidationException('key value is required', key_value) self._refs.append(KeyRef(key_name, key_value, ref_path))