def __init__(self, validator_a, validator_b, doc=None): super().__init__(doc=doc) assert isinstance(validator_a, Validator), "validator_a must be a Validator" assert isinstance(validator_b, Validator), "validator_b must be a Validator" self._validator_a = validator_a self._validator_b = validator_b def unpacked(validator): if isinstance(validator, OrValidator): return [ unpacked(validator._validator_a), unpacked(validator._validator_b), ] else: return [validator] map_validator_count = len([ validator for validator in list(utils.flatten(unpacked(self))) if isinstance(validator, MapValidator) ]) if map_validator_count > 1: raise InvalidValidatorError(( "You tried to Or ('|') together {} Map validators. " "Try using revalidation instead.").format(map_validator_count)) seq_validator_count = len([ validator for validator in list(utils.flatten(unpacked(self))) if isinstance(validator, SeqValidator) ]) if seq_validator_count > 1: raise InvalidValidatorError(( "You tried to Or ('|') together {} Seq validators. " "Try using revalidation instead.").format(seq_validator_count)) self.doc["_type"] = "Or" self.doc["a"] = validator_a.doc self.doc["b"] = validator_b.doc
def _get_classes(class_: type) -> Set[type]: """ Helper function to return a set of attr classes that needs schema. """ def needs_schema(type_): """Helper function to ensure that the given attribute (type_) needs a strict yaml schema""" return is_attrs(type_) or (_is_from_typing_module(type_) and is_attrs(_obtain_referred_type(type_))) def get_attr_class_type(type_): """Helper function to retrieve the attr type being referred by the type hint""" return type_ if is_attrs(type_) else _obtain_referred_type(type_) classes = [] for key, value in attr.fields_dict(class_).items(): if needs_schema(value.type) and not key in IGNORED_PROPERTIES: classes.append(get_attr_class_type(value.type)) return set(flatten(classes))
def is_attrs(type_: type) -> bool: return any((hasattr(i, "__attrs_attrs__") for i in flatten([type_])))