class Context:
- resolve_directory: Path
+ resolve_root: Optional[Path]
+ strict_validation: bool
- def __init__(self, resolve_directory: Path) -> None:
- self.resolve_directory = resolve_directory
+ def __init__(self, resolve_root: Optional[Path], strict_validation: bool = True) -> None:
+ self.resolve_root = resolve_root
+ self.strict_validation = strict_validation
-_global_context: Optional[Context] = None
+_global_context: Context = Context(None)
def set_global_validation_context(context: Context) -> None:
def reset_global_validation_context() -> None:
global _global_context
- _global_context = None
+ _global_context = Context(None)
-def get_global_validation_context() -> Context:
- if _global_context is None:
+def get_resolve_root() -> Path:
+ if _global_context.resolve_root is None:
raise RuntimeError(
- "Global validation context is not set! Before validation, you have to call `set_global_validation_context()` function!"
+ "Global validation context 'resolve_root' is not set!"
+ " Before validation, you have to set it using `set_global_validation_context()` function!"
)
- return _global_context
+ return _global_context.resolve_root
+
+
+def get_strict_validation() -> bool:
+ return _global_context.strict_validation
from pathlib import Path
from typing import Any, Dict, Tuple, Type, TypeVar
-from knot_resolver_manager.datamodel.globals import get_global_validation_context
+from knot_resolver_manager.datamodel.globals import get_resolve_root, get_strict_validation
from knot_resolver_manager.utils.modeling.base_value_type import BaseValueType
super().__init__(source_value, object_path=object_path)
self._object_path: str = object_path
self._parents: Tuple[UncheckedPath, ...] = parents
+ self.strict_validation: bool = get_strict_validation()
if isinstance(source_value, str):
# we do not load global validation context if the path is absolute
if source_value.startswith("/"):
resolve_root = Path("/")
else:
- resolve_root = get_global_validation_context().resolve_directory
+ resolve_root = get_resolve_root()
self._raw_value: str = source_value
if self._parents:
self, source_value: Any, parents: Tuple["UncheckedPath", ...] = tuple(), object_path: str = "/"
) -> None:
super().__init__(source_value, parents=parents, object_path=object_path)
- if not self._value.is_dir():
+ if self.strict_validation and not self._value.is_dir():
raise ValueError(f"path '{self._value}' does not point to an existing directory")
self, source_value: Any, parents: Tuple["UncheckedPath", ...] = tuple(), object_path: str = "/"
) -> None:
super().__init__(source_value, parents=parents, object_path=object_path)
- if not self._value.is_absolute():
+ if self.strict_validation and not self._value.is_absolute():
raise ValueError("path not absolute")
self, source_value: Any, parents: Tuple["UncheckedPath", ...] = tuple(), object_path: str = "/"
) -> None:
super().__init__(source_value, parents=parents, object_path=object_path)
- if not self._value.exists():
+ if self.strict_validation and not self._value.exists():
raise ValueError("file does not exist")
- if not self._value.is_file():
+ if self.strict_validation and not self._value.is_file():
raise ValueError("path is not a file")
) -> None:
super().__init__(source_value, parents=parents, object_path=object_path)
p = self._value.parent
- if not p.exists() or not p.is_dir():
+ if self.strict_validation and not p.exists() or not p.is_dir():
raise ValueError(f"path '{self._value}' does not point inside an existing directory")
- if self._value.is_dir():
+ if self.strict_validation and self._value.is_dir():
raise ValueError("path points to a directory when we expected a file")