def validate(document, schema, log_qualifier = True): """ If the document is valid according to the schema, this function returns True. If the document is not valid according to the schema, errors are logged then False is returned. """ if isinstance(schema, ValidatorNode): return _validate_node(document, schema, log_qualifier) if isinstance(schema, dict): return _validate_dict(document, schema) if isinstance(schema, list): return _validate_list(document, schema) if isinstance(schema, Any): return True if isinstance(schema, Scalar): return helpers.is_scalar(document) # scalar if isinstance(schema, text_type): schema = ensure_text(schema) if isinstance(document, text_type): document = ensure_text(document, 'utf8') if schema.__class__ != document.__class__: LOG.error("wanted a %s, got a %s", schema.__class__.__name__, document.__class__.__name__) return False return True
def task_value(self, data, value_tasks=None, collect_value=None, collect_default=None): tasks = value_tasks or self.value_tasks (failed, noresult) = (False, False) if isinstance(data, CovenantTargetFailed): if self.on_fail['remove']: return self.remove(True) failed = True data = copy.copy(self.on_fail['value']) elif isinstance(data, CovenantNoResult): if self.on_noresult['remove']: return self.remove(True) noresult = True data = copy.copy(self.on_noresult['value']) value = None for labelvalue in self.labelvalues: value = copy.copy(data) if not failed and not noresult and tasks: for task in tasks: if isinstance(value, CovenantNoResult): if self.on_noresult['remove']: return self.remove(True) value = self.on_noresult['value'] break value = task(value=value, labelvalue=labelvalue) if isinstance(value, CovenantNoResult): if self.on_noresult['remove']: return self.remove(True) value = self.on_noresult['value'] if helpers.is_scalar(value): labelvalue.set(value) elif collect_value is not None: labelvalue.set(collect_value) elif collect_default is not None: labelvalue.set(collect_default) elif tasks: raise CovenantTaskError( "unable to fetch metricvalue for labelvalue: %r" % labelvalue.labelvalue) del value return self
def response_dumps(data, charset): # pylint: disable=unused-argument if isinstance(data, bool): data = int(data) if data is None: return "" if helpers.is_scalar(data): return "%s" % data if hasattr(data, '__str__') \ and type(data).__str__ is not object.__str__: return "%s" % data return repr(data)
def _validate_dict(document, schema): if not isinstance(document, dict): LOG.error("wanted a dictionary, got a %s", document.__class__.__name__) return False generic = [] optional = {} optionalnull = {} mandatory = [] for key, schema_val in iteritems(schema): if isinstance(key, ValidatorNode): if key.mode == 'mandatory': mandatory.append((key, schema_val)) else: generic.append((key, schema_val)) elif isinstance(schema_val, Optional): optional[key] = schema_val elif isinstance(schema_val, OptionalNull): optional[key] = schema_val optionalnull[key] = True else: mandatory.append((key, schema_val)) doc_copy = document.copy() for key, schema_val in mandatory: if isinstance(key, ValidatorNode): nb = 0 rm = [] for doc_key, doc_val in iteritems(doc_copy): if not validate(doc_key, key, False): continue nb += 1 if validate(doc_val, schema_val): rm.append(doc_key) else: return False if nb == 0: LOG.error("missing document %r for qualifier: %r", key.content, key.validator.__name__) return False if key.min is not None and nb < key.min: LOG.error("no enough document %r for qualifier: %r (min: %r, found: %r)", key.content, key.validator.__name__, key.min, nb) return False if key.max is not None and nb > key.max: LOG.error("too many document %r for qualifier: %r (max: %r, found: %r)", key.content, key.validator.__name__, key.max, nb) return False for x in rm: del doc_copy[x] continue doc_val = doc_copy.get(key, Nothing) if doc_val is Nothing: LOG.error("missing key %r in document", key) return False if helpers.is_scalar(schema_val): if not validate(doc_val, schema_val): return False del doc_copy[key] continue if schema_val.modifier: for modname in schema_val.modifier: if modname in _modifiers: document[key] = _modifiers[modname](document[key]) doc_val = _modifiers[modname](doc_val) elif hasattr(doc_val, modname): document[key] = getattr(document[key], modname)() doc_val = getattr(doc_val, modname)() if _valid_len(key, doc_val, schema_val.min_len, schema_val.max_len) is False: return False if not validate(doc_val, schema_val.content): return False del doc_copy[key] for key, schema_val in generic: nb = 0 rm = [] for doc_key, doc_val in iteritems(doc_copy): if not validate(doc_key, key, False): continue nb += 1 if validate(doc_val, schema_val): rm.append(doc_key) else: return False if key.min is not None and nb < key.min: LOG.error("no enough document %r for qualifier: %r (min: %r, found: %r)", key.content, key.validator.__name__, key.min, nb) return False if key.max is not None and nb > key.max: LOG.error("too many document %r for qualifier: %r (max: %r, found: %r)", key.content, key.validator.__name__, key.max, nb) return False for x in rm: del doc_copy[x] continue for key, doc_val in iteritems(doc_copy): schema_val = optional.get(key, Nothing) if schema_val is Nothing: LOG.error("forbidden key %s in document", key) return False if key in optionalnull and doc_val is None: continue if schema_val.min_len == 0 and doc_val is "": continue if schema_val.modifier: for modname in schema_val.modifier: if modname in _modifiers: document[key] = _modifiers[modname](document[key]) doc_val = _modifiers[modname](doc_val) elif hasattr(doc_val, modname): document[key] = getattr(document[key], modname)() doc_val = getattr(doc_val, modname)() if key in optionalnull and doc_val is None: continue if schema_val.min_len == 0 and doc_val is "": continue if _valid_len(key, doc_val, schema_val.min_len, schema_val.max_len) is False: return False if not validate(doc_val, schema_val.content): return False return True