from typing import Union
from pathlib import Path


def validate_directory(
        directory: Union[str, Path], pth_obj: bool = False
) -> Union[str, Path]:
    """checks if a directory exists and creates if not

    Args:
        directory (str): path to a directory
        pth_obj (bool): if true return a Path object
    Returns:
        directory (str/Path): validated path to provided directory
    """
    dir = Path(directory)
    if dir.is_dir():
        if pth_obj:
            return dir
        else:
            return directory
    else:
        try:
            dir.mkdir()
            if pth_obj:
                return dir
            else:
                return directory
        except:
            raise ValueError("could not create directory")