Skip to content

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.csv and 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:

  1. Validate request data with mo_validation_kit.ensure_* checks.
  2. Use is_exception=True when you want immediate failure as exception.
  3. Use is_aggregate=True with finalize(...) when you need to collect multiple field errors first.
  4. 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 call finalize(...).

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_equal(2 + 2, 4, is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_not_equal("draft", "published", is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_same(request.user, owner, is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_not_same(current_user, banned_user, is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_greater(order_total, 0, is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_greater_equal(stock_qty, 0, is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_lesser(discount, subtotal, is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_lesser_equal(page_size, 100, is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_in_range(rating, 1, 5, is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_not_in_range(age, 0, 12, is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_almost_equal(total, expected, tol=1e-6, is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_not_almost_equal(score, 0.0, tol=1e-9, is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_falsey(payload.get("debug"), is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_truthy(payload.get("customer_id"), is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_type(payload, dict, is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_not_type(payload, list, is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_subclass(CustomError, Exception, is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_not_subclass(int, Exception, is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_in(status, {"queued", "running", "done"}, is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_not_in(role, {"banned", "blocked"}, is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_count_equal(["a", "b"], ["b", "a"], is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_count_not_equal(["a", "a"], ["a", "b"], is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_finite(latency_ms, is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_regex(email, r"^[^@]+@[^@]+\.[^@]+$", is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_not_regex(username, r"^admin$", is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_path("/tmp/report.csv", is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure_not_path("/tmp/lockfile", is_exception=True)

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): If True, raises the method-specific exception immediately.
  • is_aggregate (bool, default=False): If True, stores validation failure and continues execution.
  • exc_type (type[Exception], default=ValidationError): Exception class used by ensure when is_exception=True.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.ensure(lambda: total_qty > 0, msg="total_qty must be > 0", is_exception=True)

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 by finalize in error mode.
  • return_mode (Literal['list', 'error', 'exception'], default='error'): Finalize output mode: list, error, or exception.

Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

errors = mo_validation_kit.finalize(return_mode="list")

30. Reset

Reset validator aggregate state.

Usage:

mo_validation_kit.reset(
)

Parameters: Possible responses:

  • Returns True when validation passes.
  • Returns None when is_aggregate=True and validation fails (error is buffered).
  • Raises method-specific exception when is_exception=True and validation fails.
  • Raises MindoffValidationError when validation fails in default mode.

Example:

mo_validation_kit.reset()

Troubleshooting

  • Validation errors look inconsistent across APIs
    Ensure all APIs use the same response code strategy in config/responses.csv and return through mo_response_kit.
  • Some errors are missing in aggregate mode
    Confirm finalize(...) is called after all is_aggregate=True checks.