def validate(self, chunk): return_snippet = chunk.contents if type(chunk.contents) != CommentedMap: raise_exception( "when expecting a mapping", "found non-mapping", chunk, ) else: for key, value in chunk.contents.items(): if key not in self._validator_dict.keys(): raise_exception( u"while parsing a mapping", u"unexpected key not in schema '{0}'".format( unicode(key)), chunk.key(key)) validator = self._validator_dict[key] del return_snippet[key] return_snippet[YAML(key, chunk=chunk.key(key), validator=validator)] = validator( chunk.val(key)) return YAML(return_snippet, chunk=chunk, validator=self)
def validate(self, chunk): return_snippet = chunk.contents if not isinstance(return_snippet, CommentedSeq): raise_exception( "when expecting a sequence of {0} elements".format( len(self._validators)), "found non-sequence", chunk, ) else: if len(self._validators) != len(chunk.contents): raise_exception( "when expecting a sequence of {0} elements".format( len(self._validators)), "found a sequence of {0} elements".format( len(chunk.contents)), chunk, ) for i, item_and_val in enumerate( zip(chunk.contents, self._validators)): item, validator = item_and_val return_snippet[i] = validator(chunk.index(i)) return YAML(return_snippet, chunk=chunk)
def validate(self, document, location=None): if location is None: location = YAMLLocation() document = copy.deepcopy(document) return_snippet = {} if type(location.get(document)) != CommentedMap: raise_exception( "when expecting a mapping", "found non-mapping", document, location=location, ) else: for key, value in location.get(document).items(): if key not in self._validator_dict.keys(): raise_exception( "while parsing a mapping", "unexpected key not in schema '{0}'".format(key), document, location=location.key(key) ) return_snippet[key] = self._validator_dict[key]( document, location.val(key) ) return return_snippet
def validate(self, document, location=None): if location is None: location = YAMLLocation() document = copy.deepcopy(document) return_snippet = {} if type(location.get(document)) != CommentedMap: raise_exception( "when expecting a mapping", "found non-mapping", document, location=location, ) else: for key, value in location.get(document).items(): if key not in self._validator_dict.keys(): raise_exception( "while parsing a mapping", "unexpected key not in schema '{0}'".format(key), document, location=location.key(key) ) return_snippet[key] = self._validator_dict[key]( document, location=location.val(key) ) return return_snippet
def validate(self, document, location=None): if location is None: location = YAMLLocation() document = copy.deepcopy(document) return_snippet = [] if type(location.get(document)) != CommentedSeq: raise_exception( "when expecting a unique sequence", "found non-sequence", document, location=location, ) else: existing_items = set() for i, item in enumerate(location.get(document)): if item in existing_items: raise_exception( "while parsing a sequence", "duplicate found", document, location=location ) else: existing_items.add(item) return_snippet.append(self._validator(document, location=location.index(i))) return return_snippet
def validate_scalar(self, document, location, value=None): val = str(location.get(document)) if value is None else value if re.compile(r"^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$").match(str(val)) is None: raise_exception( "when expecting a decimal", "found non-decimal", document, location=location, ) else: return decimal.Decimal(val)
def validate_scalar(self, chunk, value=None): val = unicode(chunk.contents) if value is None else value if not utils.is_decimal(val): raise_exception( "when expecting a decimal", "found non-decimal", chunk, ) else: return YAML(decimal.Decimal(val), val, chunk=chunk, validator=self)
def validate(self, document, location=None): val = str(location.get(document)) if val not in self._restricted_to: raise_exception( "when expecting one of: {0}".format(", ".join(self._restricted_to)), "found '{0}'".format(val), document, location=location, ) else: return val
def validate_scalar(self, document, location): val = str(location.get(document)) if val != "": raise_exception( "when expecting an empty value", "found non-empty value", document, location=location, ) else: return self.empty()
def validate_scalar(self, chunk, value): val = unicode(chunk.contents) if value is None else value if val != "": raise_exception( "when expecting an empty value", "found non-empty value", chunk, ) else: return self.empty(chunk)
def validate_scalar(self, document, location, value=None): val = str(location.get(document)) if value is None else value if re.compile("^[-+]?\d+$").match(val) is None: raise_exception( "when expecting an integer", "found non-integer", document, location=location, ) else: return int(val)
def validate_scalar(self, document, location): val = str(location.get(document)) if re.compile(r"^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$").match(str(val)) is None: raise_exception( "when expecting a decimal", "found non-decimal", document, location=location, ) else: return decimal.Decimal(val)
def validate_scalar(self, document, location): val = str(location.get(document)) if re.compile("^[-+]?\d+$").match(val) is None: raise_exception( "when expecting an integer", "found non-integer", document, location=location, ) else: return int(val)
def validate_scalar(self, chunk, value): val = unicode(chunk.contents) if value is None else value if val not in self._restricted_to: raise_exception( "when expecting one of: {0}".format(", ".join(self._restricted_to)), "found '{0}'".format(val), chunk, ) else: return YAML(val, chunk=chunk)
def validate_scalar(self, chunk, value=None): val = unicode(chunk.contents) if value is None else value if not utils.is_integer(val): raise_exception( "when expecting an integer", "found non-integer", chunk, ) else: return YAML(int(val), val, chunk=chunk)
def validate_scalar(self, document, location, value): val = str(location.get(document)) if value is None else value if val not in self._restricted_to: raise_exception( "when expecting one of: {0}".format(", ".join(self._restricted_to)), "found '{0}'".format(val), document, location=location, ) else: return val
def validate_scalar(self, document, location, value): val = str(location.get(document)) if value is None else value if val != "": raise_exception( "when expecting an empty value", "found non-empty value", document, location=location, ) else: return self.empty()
def validate(self, chunk): val = chunk.contents if type(val) == CommentedSeq or type(val) == CommentedMap: raise_exception( "when expecting a {0}".format(self.__class__.__name__.lower()), "found mapping/sequence", chunk, ) else: return self.validate_scalar(chunk, value=None)
def validate_scalar(self, chunk, value=None): val = unicode(chunk.contents) if value is None else value try: return YAML(dateutil.parser.parse(val), val, chunk=chunk) except ValueError: raise_exception( "when expecting a datetime", "found non-datetime", chunk, )
def validate(self, document, location=None): val = location.get(document) if type(val) == CommentedSeq or type(val) == CommentedMap: raise_exception( "when expecting a {0}".format(self.__class__.__name__.lower()), "found mapping/sequence", document, location=location, ) else: return self.validate_scalar(document, location, value=None)
def validate_scalar(self, document, location, value=None): val = str(location.get(document)) if value is None else value try: return dateutil.parser.parse(val) except ValueError: raise_exception( "when expecting a datetime", "found non-datetime", document, location=location, )
def validate(self, document, location=None): val = location.get(document) if type(val) == CommentedSeq or type(val) == CommentedMap: raise_exception( "when expecting a {0}".format(self.__class__.__name__.lower()), "found mapping/sequence", document, location=location, ) else: return self.validate_scalar(document, location=location)
def validate_scalar(self, document, location): val = str(location.get(document)) try: return dateutil.parser.parse(val) except ValueError: raise_exception( "when expecting a datetime", "found non-datetime", document, location=location, )
def validate_scalar(self, chunk, value=None): if re.compile(self._regex).match(chunk.contents) is None: raise_exception( self._matching_message, "found non-matching string", chunk, ) return YAML( unicode(chunk.contents) if value is None else value, text=chunk.contents, chunk=chunk )
def validate_scalar(self, chunk, value=None): val = unicode(chunk.contents) if value is None else value if unicode(val).lower() not in constants.BOOL_VALUES: raise_exception( """when expecting a boolean value (one of "{0}")""".format( '", "'.join(constants.BOOL_VALUES)), "found non-boolean", chunk, ) else: if val.lower() in constants.TRUE_VALUES: return YAML(True, val, chunk=chunk, validator=self) else: return YAML(False, val, chunk=chunk, validator=self)
def validate(self, chunk): return_snippet = chunk.contents if not isinstance(return_snippet, CommentedSeq): raise_exception( "when expecting a sequence", "found non-sequence", chunk, ) else: for i, item in enumerate(chunk.contents): return_snippet[i] = self._validator(chunk.index(i)) return YAML(return_snippet, chunk=chunk)
def validate_scalar(self, document, location, value=None): val = str(location.get(document)) if value is None else value if str(val).lower() not in BOOL_VALUES: raise_exception( """when expecting a boolean value (one of "{0}")""".format( '", "'.join(BOOL_VALUES) ), "found non-boolean", document, location=location, ) else: if val in TRUE_VALUES: return True else: return False
def validate_scalar(self, document, location): val = str(location.get(document)) if str(val).lower() not in BOOL_VALUES: raise_exception( """when expecting a boolean value (one of "{0}")""".format( '", "'.join(BOOL_VALUES) ), "found non-boolean", document, location=location, ) else: if val in TRUE_VALUES: return True else: return False
def validate(self, document, location=None): if location is None: location = YAMLLocation() document = copy.deepcopy(document) return_snippet = [] if type(location.get(document)) != CommentedSeq: raise_exception( "when expecting a sequence", "found non-sequence", document, location=location, ) else: for i, item in enumerate(location.get(document)): return_snippet.append(self._validator(document, location=location.index(i))) return return_snippet
def validate(self, document, location=None): if location is None: location = YAMLLocation() document = copy.deepcopy(document) return_snippet = {} if type(location.get(document)) != CommentedMap: raise_exception( "when expecting a mapping", "found non-mapping", document, location=location, ) else: for key, value in location.get(document).items(): valid_key = self._key_validator(document, location.key(key)) valid_val = self._value_validator(document, location.val(key)) return_snippet[valid_key] = valid_val return return_snippet
def validate(self, chunk): return_snippet = chunk.contents if not isinstance(return_snippet, CommentedMap): raise_exception( "when expecting a mapping", "found non-mapping", chunk, ) else: for key, value in chunk.contents.items(): valid_key = self._key_validator(chunk.key(key)) valid_val = self._value_validator(chunk.val(key)) return_snippet[valid_key] = valid_val del return_snippet[valid_key] return_snippet[valid_key] = self._value_validator( chunk.val(key)) return YAML(return_snippet, chunk=chunk)
def validate(self, chunk): return_snippet = chunk.contents if type(chunk.contents) != CommentedSeq: raise_exception( "when expecting a unique sequence", "found non-sequence", chunk, ) else: existing_items = set() for i, item in enumerate(chunk.contents): if item in existing_items: raise_exception("while parsing a sequence", "duplicate found", chunk) else: existing_items.add(item) return_snippet[i] = self._validator(chunk.index(i)) return YAML(return_snippet, chunk=chunk)