Validation Utilities¶
Use validation to stop bad input early and return consistent, actionable errors to clients.
Prerequisites¶
- API Class is scaffolded and routed.
config/responses.csvand response codes for expected validation failures exists in your project.
Implementation¶
Validation at request boundaries prevents invalid states from reaching business logic.
django-mindoff provides mo_validation_kit for checks and MindoffValidationError for structured failure payloads.
Recommended flow:
- Validate request data with
mo_validation_kit.ensure_*checks. - Use
is_exception=Truewhen you want immediate failure as exception. - Use
is_aggregate=Truewithfinalize(...)when you need to collect multiple field errors first. - Return envelope responses through Response Kit.
Error Handling Modes
mo_validation_kit supports three practical modes:
- Immediate exception: raise on first failure with
is_exception=True. - Structured validation error: default mode raises
MindoffValidationError. - Aggregated errors: accumulate with
is_aggregate=True, then callfinalize(...).
Choose one strategy per endpoint branch
Mixing immediate and aggregate validation in the same branch can make error contracts hard to reason about. Keep one clear strategy for each API path.
1. Ensure Equal¶
Validate that two values are equal.
Usage:
mo_validation_kit.ensure_equal(
left: Any,
right: Any,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
left(Any): First value to compare.right(Any): Second value to compare.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
2. Ensure Not Equal¶
Validate that two values are not equal.
Usage:
mo_validation_kit.ensure_not_equal(
left: Any,
right: Any,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
left(Any): First value to compare.right(Any): Second value to compare.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
3. Ensure Same¶
Validate that two references point to the same object.
Usage:
mo_validation_kit.ensure_same(
left: Any,
right: Any,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
left(Any): First value to compare.right(Any): Second value to compare.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
4. Ensure Not Same¶
Validate that two references do not point to the same object.
Usage:
mo_validation_kit.ensure_not_same(
left: Any,
right: Any,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
left(Any): First value to compare.right(Any): Second value to compare.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
5. Ensure Greater¶
Validate that left is greater than right.
Usage:
mo_validation_kit.ensure_greater(
left: Any,
right: Any,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
left(Any): First value to compare.right(Any): Second value to compare.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
6. Ensure Greater Equal¶
Validate that left is greater than or equal to right.
Usage:
mo_validation_kit.ensure_greater_equal(
left: Any,
right: Any,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
left(Any): First value to compare.right(Any): Second value to compare.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
7. Ensure Lesser¶
Validate that left is less than right.
Usage:
mo_validation_kit.ensure_lesser(
left: Any,
right: Any,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
left(Any): First value to compare.right(Any): Second value to compare.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
8. Ensure Lesser Equal¶
Validate that left is less than or equal to right.
Usage:
mo_validation_kit.ensure_lesser_equal(
left: Any,
right: Any,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
left(Any): First value to compare.right(Any): Second value to compare.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
9. Ensure In Range¶
Validate that a value is inside an inclusive numeric range.
Usage:
mo_validation_kit.ensure_in_range(
value: float,
min_value: float,
max_value: float,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
value(float): Value being validated.min_value(float): Minimum allowed value.max_value(float): Maximum allowed value.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
10. Ensure Not In Range¶
Validate that a value is outside an inclusive numeric range.
Usage:
mo_validation_kit.ensure_not_in_range(
value: float,
min_value: float,
max_value: float,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
value(float): Value being validated.min_value(float): Minimum allowed value.max_value(float): Maximum allowed value.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
11. Ensure Almost Equal¶
Validate that two floating-point values are approximately equal within tolerance.
Usage:
mo_validation_kit.ensure_almost_equal(
left: float,
right: float,
tol: float = 1e-6,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
left(float): First value to compare.right(float): Second value to compare.tol(float, default=1e-6): Allowed tolerance for floating-point comparisons.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
12. Ensure Not Almost Equal¶
Validate that two floating-point values are not approximately equal within tolerance.
Usage:
mo_validation_kit.ensure_not_almost_equal(
left: float,
right: float,
tol: float = 1e-6,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
left(float): First value to compare.right(float): Second value to compare.tol(float, default=1e-6): Allowed tolerance for floating-point comparisons.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
13. Ensure Falsey¶
Validate that a value evaluates to False.
Usage:
mo_validation_kit.ensure_falsey(
value: Any,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
value(Any): Value being validated.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
14. Ensure Truthy¶
Validate that a value evaluates to True.
Usage:
mo_validation_kit.ensure_truthy(
value: Any,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
value(Any): Value being validated.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
15. Ensure Type¶
Validate that a value is an instance of the expected type.
Usage:
mo_validation_kit.ensure_type(
value: Any,
typ: type,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
value(Any): Value being validated.typ(type): Expected Python type.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
16. Ensure Not Type¶
Validate that a value is not an instance of the provided type.
Usage:
mo_validation_kit.ensure_not_type(
value: Any,
typ: type,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
value(Any): Value being validated.typ(type): Expected Python type.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
17. Ensure Subclass¶
Validate that cls is a subclass of parent.
Usage:
mo_validation_kit.ensure_subclass(
cls: type,
parent: type,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
cls(type): Class to validate.parent(type): Expected parent class.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
18. Ensure Not Subclass¶
Validate that cls is not a subclass of parent.
Usage:
mo_validation_kit.ensure_not_subclass(
cls: type,
parent: type,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
cls(type): Class to validate.parent(type): Expected parent class.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
19. Ensure In¶
Validate that value exists in container.
Usage:
mo_validation_kit.ensure_in(
value: Any,
container: Any,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
value(Any): Value being validated.container(Any): Container used for membership checks.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
20. Ensure Not In¶
Validate that value does not exist in container.
Usage:
mo_validation_kit.ensure_not_in(
value: Any,
container: Any,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
value(Any): Value being validated.container(Any): Container used for membership checks.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
21. Ensure Count Equal¶
Validate that two iterables have equal element counts.
Usage:
mo_validation_kit.ensure_count_equal(
left: Any,
right: Any,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
left(Any): First value to compare.right(Any): Second value to compare.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
22. Ensure Count Not Equal¶
Validate that two iterables do not have equal element counts.
Usage:
mo_validation_kit.ensure_count_not_equal(
left: Any,
right: Any,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
left(Any): First value to compare.right(Any): Second value to compare.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
23. Ensure Finite¶
Validate that a numeric value is finite.
Usage:
mo_validation_kit.ensure_finite(
value: Any,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
value(Any): Value being validated.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
24. Ensure Regex¶
Validate that a string fully matches a regex pattern.
Usage:
mo_validation_kit.ensure_regex(
value: Any,
pattern: str,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
value(Any): Value being validated.pattern(str): Regex pattern for full-string matching.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
25. Ensure Not Regex¶
Validate that a string does not fully match a regex pattern.
Usage:
mo_validation_kit.ensure_not_regex(
value: Any,
pattern: str,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
value(Any): Value being validated.pattern(str): Regex pattern for full-string matching.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
26. Ensure Path¶
Validate that a filesystem path exists.
Usage:
mo_validation_kit.ensure_path(
path: Any,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
path(Any): Filesystem path to validate.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
27. Ensure Not Path¶
Validate that a filesystem path does not exist.
Usage:
mo_validation_kit.ensure_not_path(
path: Any,
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
)
Parameters:
path(Any): Filesystem path to validate.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
28. Ensure¶
Validate a custom boolean or callable check.
Usage:
mo_validation_kit.ensure(
check: Union[bool, Callable[[], bool]],
msg: Optional[str] = None,
code: str = "VALIDATION_ERR",
is_exception: bool = False,
is_aggregate: bool = False,
exc_type: type[Exception] = ValidationError,
)
Parameters:
check(Union[bool, Callable[[], bool]]): Boolean or callable returning a boolean.msg(Optional[str], default=None): Custom validation error message.code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.is_exception(bool, default=False): IfTrue, raises the method-specific exception immediately.is_aggregate(bool, default=False): IfTrue, stores validation failure and continues execution.exc_type(type[Exception], default=ValidationError): Exception class used byensurewhenis_exception=True.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
29. Finalize¶
Finalize aggregated validation failures.
Usage:
mo_validation_kit.finalize(
code: str = "VALIDATION_ERR",
message: str = 'Aggregated Validation Failed',
return_mode: Literal['list', 'error', 'exception'] = 'error',
)
Parameters:
code(str, default="VALIDATION_ERR"): Error code to attach on validation failure.message(str, default='Aggregated Validation Failed'): Top-level message used byfinalizeinerrormode.return_mode(Literal['list', 'error', 'exception'], default='error'): Finalize output mode:list,error, orexception.
Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
30. Reset¶
Reset validator aggregate state.
Usage:
Parameters: Possible responses:
- Returns
Truewhen validation passes. - Returns
Nonewhenis_aggregate=Trueand validation fails (error is buffered). - Raises method-specific exception when
is_exception=Trueand validation fails. - Raises
MindoffValidationErrorwhen validation fails in default mode.
Example:
Troubleshooting¶
- Validation errors look inconsistent across APIs
Ensure all APIs use the same response code strategy inconfig/responses.csvand return throughmo_response_kit. - Some errors are missing in aggregate mode
Confirmfinalize(...)is called after allis_aggregate=Truechecks.