onecondition.validate

Contains methods to validate various conditions about 1 or more values.

Module Contents

Functions

true(→ None)

Validate that a value is pythonically True, and if it isn't, raise an exception.

false(→ None)

Validate that a value is pythonically False, and if it isn't, raise an exception.

none(→ None)

Validate that a value is None, and if it isn't, raise an exception.

not_none(→ None)

Validate that a value is not None, and if it is, raise an exception.

specific_type(→ None)

Validate that a value is a specific type (do not consider inheritance), and if it isn't, raise an exception.

not_specific_type(→ None)

Validate that a value is a not specific type (do not consider inheritance), and if it is, raise an exception.

instance(→ None)

Validate that a value is an instance (the same as or a subclass) of a specific type, and if it isn't, raise an exception.

not_instance(→ None)

Validate that a value is not an instance (the same as or a subclass) of a specific type, and if it is, raise an exception.

zero(→ None)

Validate that a value is exactly equal to 0, and if it isn't, raise an exception.

not_zero(→ None)

Validate that a value is not exactly equal to 0, and if it is, raise an exception.

positive(→ None)

Validate that a value is positive (non-zero), and if it isn't, raise an exception.

not_positive(→ None)

Validate that a value is not positive (non-zero), and if it is, raise an exception.

negative(→ None)

Validate that a value is negative (non-zero), and if it isn't, raise an exception.

not_negative(→ None)

Validate that a value is not negative (non-zero), and if it is, raise an exception.

range_inclusive(→ None)

Validate that a value is within a specified range (inclusive), and if it isn't, raise an exception.

not_range_inclusive(→ None)

Validate that a value is not within a specified range (inclusive), and if it is, raise an exception.

range_non_inclusive(→ None)

Validate that a value is within a specified range (non-inclusive), and if it isn't, raise an exception.

not_range_non_inclusive(→ None)

Validate that a value is not within a specified range (non-inclusive), and if it is, raise an exception.

eq(→ None)

Validate that a value is exactly equal to a second value, and if it isn't, raise an exception.

neq(→ None)

Validate that a value is not exactly equal to a second value, and if it is, raise an exception.

gt(→ None)

Validate that a value is greater than a second value, and if it isn't, raise an exception.

lt(→ None)

Validate that a value is less than a second value, and if it isn't, raise an exception.

gte(→ None)

Validate that a value is greater than or equal to a second value, and if it isn't, raise an exception.

lte(→ None)

Validate that a value is less than or equal to a second value, and if it isn't, raise an exception.

onecondition.validate.true(value: Any) None

Validate that a value is pythonically True, and if it isn’t, raise an exception.

Parameters:

value (Any) – The value to test.

Raises:

ValidationError – Raised if the value is not pythonically True.

Return type:

None

Example:
>>> true(True)
>>> true(False)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `False` must be (pythonically) True
>>> true(None)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `None` must be (pythonically) True
>>> true(0)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `0` must be (pythonically) True
>>> true(1)
>>> true("")
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `''` must be (pythonically) True
>>> true("foobar")
onecondition.validate.false(value: Any) None

Validate that a value is pythonically False, and if it isn’t, raise an exception.

Parameters:

value (Any) – The value to test.

Raises:

ValidationError – Raised if the value is not pythonically False.

Return type:

None

Example:
>>> false(True)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `True` must be (pythonically) False
>>> false(False)
>>> false(None)
>>> false(0)
>>> false(1)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `1` must be (pythonically) False
>>> false("")
>>> false("foobar")
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `'foobar'` must be (pythonically) False
onecondition.validate.none(value: Any) None

Validate that a value is None, and if it isn’t, raise an exception.

Parameters:

value (Any) – The value to test.

Raises:

ValidationError – Raised if the value is not None.

Return type:

None

Example:
>>> none(None)
>>> none("")
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `''` must be None
onecondition.validate.not_none(value: Any) None

Validate that a value is not None, and if it is, raise an exception.

Parameters:

value (Any) – The value to test.

Raises:

ValidationError – Raised if the value is None.

Return type:

None

Example:
>>> not_none("")
>>> not_none(None)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `None` must not be None
onecondition.validate.specific_type(value: Any, value_type: type | types.UnionType | tuple[type | types.UnionType | tuple[Any, Ellipsis], Ellipsis]) None

Validate that a value is a specific type (do not consider inheritance), and if it isn’t, raise an exception.

Parameters:
  • value (Any) – The value to test.

  • value_type (type) – The type to test the value against.

Raises:

ValidationError – Raised if the type of the value isn’t an exact match.

Return type:

None

Example:
>>> class TestError(ValueError):
...     def __init__(self, message):
...         super().__init__(message)
>>> test_error = TestError("Test")
>>> specific_type(test_error, TestError)
>>> specific_type(test_error, ValueError)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `TestError('Test')` must be of type <class 'ValueError'>, not <class 'onecondition.validate.TestError'>
>>> specific_type(test_error, (int, float))
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `TestError('Test')` must be of type (<class 'int'>, <class 'float'>), not <class 'onecondition.validate.TestError'>
onecondition.validate.not_specific_type(value: Any, value_type: type | types.UnionType | tuple[type | types.UnionType | tuple[Any, Ellipsis], Ellipsis]) None

Validate that a value is a not specific type (do not consider inheritance), and if it is, raise an exception.

Parameters:
  • value (Any) – The value to test.

  • value_type (type) – The type to test the value against.

Raises:

ValidationError – Raised if the type of the value is an exact match.

Return type:

None

Example:
>>> class TestError(ValueError):
...     def __init__(self, message):
...         super().__init__(message)
>>> test_error = TestError("Test")
>>> not_specific_type(test_error, ValueError)
>>> not_specific_type(test_error, TestError)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `TestError('Test')` must be not of type <class 'onecondition.validate.TestError'>
>>> not_specific_type(test_error, (int, float))
onecondition.validate.instance(value: Any, value_type: type | types.UnionType | tuple[type | types.UnionType | tuple[Any, Ellipsis], Ellipsis]) None

Validate that a value is an instance (the same as or a subclass) of a specific type, and if it isn’t, raise an exception.

Parameters:
  • value (Any) – The value to test.

  • value_type (type) – The type to test the value against.

Raises:

ValidationError – Raised if the value isn’t an instance of the type.

Return type:

None

Example:
>>> class TestError(ValueError):
...     def __init__(self, message):
...         super().__init__(message)
>>> test_error = TestError("Test")
>>> instance(test_error, ValueError)
>>> instance(test_error, TypeError)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `TestError('Test')` must be an instance of <class 'TypeError'>, not a <class 'onecondition.validate.TestError'>
>>> instance(test_error, (int, float))
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `TestError('Test')` must be an instance of (<class 'int'>, <class 'float'>), not a <class 'onecondition.validate.TestError'>
onecondition.validate.not_instance(value: Any, value_type: type | types.UnionType | tuple[type | types.UnionType | tuple[Any, Ellipsis], Ellipsis]) None

Validate that a value is not an instance (the same as or a subclass) of a specific type, and if it is, raise an exception.

Parameters:
  • value (Any) – The value to test.

  • value_type (type) – The type to test the value against.

Raises:

ValidationError – Raised if the value is an instance of the type.

Return type:

None

Example:
>>> class TestError(ValueError):
...     def __init__(self, message):
...         super().__init__(message)
>>> test_error = TestError("Test")
>>> not_instance(test_error, TypeError)
>>> not_instance(test_error, ValueError)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `TestError('Test')` must not be an instance of <class 'ValueError'>
>>> not_instance(test_error, (int, float))
onecondition.validate.zero(value: int | float) None

Validate that a value is exactly equal to 0, and if it isn’t, raise an exception.

Parameters:

value (Any) – The value to test.

Raises:

ValidationError – Raised if the value isn’t exactly equal to zero.

Return type:

None

Example:
>>> zero(0)
>>> zero(42)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `42` must be zero
onecondition.validate.not_zero(value: int | float) None

Validate that a value is not exactly equal to 0, and if it is, raise an exception.

Parameters:

value (Any) – The value to test.

Raises:

ValidationError – Raised if the value is exactly equal to zero.

Return type:

None

Example:
>>> not_zero(42)
>>> not_zero(0)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `0` must not be zero
onecondition.validate.positive(value: int | float) None

Validate that a value is positive (non-zero), and if it isn’t, raise an exception.

Parameters:

value (Any) – The value to test.

Raises:

ValidationError – Raised if the value isn’t positive (non-zero).

Return type:

None

Example:
>>> positive(42)
>>> positive(0)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `0` must be positive (non-zero)
>>> positive(-123.45)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `-123.45` must be positive (non-zero)
onecondition.validate.not_positive(value: int | float) None

Validate that a value is not positive (non-zero), and if it is, raise an exception.

Parameters:

value (Any) – The value to test.

Raises:

ValidationError – Raised if the value is positive (non-zero).

Return type:

None

Example:
>>> not_positive(42)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `42` must not be positive (non-zero)
>>> not_positive(0)
>>> not_positive(-123.45)
onecondition.validate.negative(value: int | float) None

Validate that a value is negative (non-zero), and if it isn’t, raise an exception.

Parameters:

value (Any) – The value to test.

Raises:

ValidationError – Raised if the value isn’t negative (non-zero).

Return type:

None

Example:
>>> negative(-123.45)
>>> negative(0)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `0` must be negative (non-zero)
>>> negative(42)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `42` must be negative (non-zero)
onecondition.validate.not_negative(value: int | float) None

Validate that a value is not negative (non-zero), and if it is, raise an exception.

Parameters:

value (Any) – The value to test.

Raises:

ValidationError – Raised if the value is negative (non-zero).

Return type:

None

Example:
>>> not_negative(-123.45)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `-123.45` must not be negative (non-zero)
>>> not_negative(0)
>>> not_negative(42)
onecondition.validate.range_inclusive(value: int | float, minimum: int | float, maximum: int | float) None

Validate that a value is within a specified range (inclusive), and if it isn’t, raise an exception.

Parameters:
  • value (Any) – The value to test.

  • minimum (int | float) – The minimum value to test against.

  • maximum (int | float) – The maximum value to test against.

Raises:

ValidationError – Raised if the value isn’t within the specified range (inclusive).

Return type:

None

Example:
>>> range_inclusive(0, 0, 1)
>>> range_inclusive(1, 0, 1)
>>> range_inclusive(42, 0, 1)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `42` must be between 0 and 1 (inclusive)
onecondition.validate.not_range_inclusive(value: int | float, minimum: int | float, maximum: int | float) None

Validate that a value is not within a specified range (inclusive), and if it is, raise an exception.

Parameters:
  • value (Any) – The value to test.

  • minimum (int | float) – The minimum value to test against.

  • maximum (int | float) – The maximum value to test against.

Raises:

ValidationError – Raised if the value is within the specified range (inclusive).

Return type:

None

Example:
>>> not_range_inclusive(0, 0, 1)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `0` must not be between 0 and 1 (inclusive)
>>> not_range_inclusive(1, 0, 1)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `1` must not be between 0 and 1 (inclusive)
>>> not_range_inclusive(42, 0, 1)
onecondition.validate.range_non_inclusive(value: int | float, minimum: int | float, maximum: int | float) None

Validate that a value is within a specified range (non-inclusive), and if it isn’t, raise an exception.

Parameters:
  • value (Any) – The value to test.

  • minimum (int | float) – The minimum value to test against.

  • maximum (int | float) – The maximum value to test against.

Raises:

ValidationError – Raised if the value isn’t within the specified range (non-inclusive).

Return type:

None

Example:
>>> range_non_inclusive(0.5, 0, 1)
>>> range_non_inclusive(0, 0, 1)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `0` must be between 0 and 1 (non-inclusive)
>>> range_non_inclusive(42, 0, 1)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `42` must be between 0 and 1 (non-inclusive)
onecondition.validate.not_range_non_inclusive(value: int | float, minimum: int | float, maximum: int | float) None

Validate that a value is not within a specified range (non-inclusive), and if it is, raise an exception.

Parameters:
  • value (Any) – The value to test.

  • minimum (int | float) – The minimum value to test against.

  • maximum (int | float) – The maximum value to test against.

Raises:

ValidationError – Raised if the value is within the specified range (non-inclusive).

Return type:

None

Example:
>>> not_range_non_inclusive(0.5, 0, 1)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `0.5` must not be between 0 and 1 (non-inclusive)
>>> not_range_non_inclusive(0, 0, 1)
>>> not_range_non_inclusive(42, 0, 1)
onecondition.validate.eq(first: Any, second: Any) None

Validate that a value is exactly equal to a second value, and if it isn’t, raise an exception.

Parameters:
  • first (Any) – The value to test.

  • second (Any) – The value to test against.

Raises:

ValidationError – Raised if the value isn’t exactly equal to a second value.

Return type:

None

Example:
>>> eq("foo", "foo")
>>> eq(42, 42)
>>> eq(-123.45, 0)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `-123.45` must be equal to `0`
onecondition.validate.neq(first: Any, second: Any) None

Validate that a value is not exactly equal to a second value, and if it is, raise an exception.

Parameters:
  • first (Any) – The value to test.

  • second (Any) – The value to test against.

Raises:

ValidationError – Raised if the value is exactly equal to a second value.

Return type:

None

Example:
>>> neq("foo", "foo")
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `'foo'` must not be equal to `'foo'`
>>> neq(42, 42)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `42` must not be equal to `42`
>>> neq(-123.45, 0)
onecondition.validate.gt(first: int | float, second: int | float) None

Validate that a value is greater than a second value, and if it isn’t, raise an exception.

Parameters:
  • first (int | float) – The value to test.

  • second (int | float) – The value to test against.

Raises:

ValidationError – Raised if the value isn’t greater than a second value.

Return type:

None

Example:
>>> gt(42, 0)
>>> gt(0, 0)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `0` must be greater than `0`
>>> gt(-123.45, 0)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `-123.45` must be greater than `0`
onecondition.validate.lt(first: int | float, second: int | float) None

Validate that a value is less than a second value, and if it isn’t, raise an exception.

Parameters:
  • first (int | float) – The value to test.

  • second (int | float) – The value to test against.

Raises:

ValidationError – Raised if the value isn’t less than a second value.

Return type:

None

Example:
>>> lt(42, 0)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `42` must be less than `0`
>>> lt(0, 0)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `0` must be less than `0`
>>> lt(-123.45, 0)
onecondition.validate.gte(first: int | float, second: int | float) None

Validate that a value is greater than or equal to a second value, and if it isn’t, raise an exception.

Parameters:
  • first (int | float) – The value to test.

  • second (int | float) – The value to test against.

Raises:

ValidationError – Raised if the value isn’t greater than or equal to a second value.

Return type:

None

Example:
>>> gte(42, 0)
>>> gte(0, 0)
>>> gte(-123.45, 0)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `-123.45` must be greater than or equal to `0`
onecondition.validate.lte(first: int | float, second: int | float) None

Validate that a value is less than or equal to a second value, and if it isn’t, raise an exception.

Parameters:
  • first (int | float) – The value to test.

  • second (int | float) – The value to test against.

Raises:

ValidationError – Raised if the value isn’t less than or equal to a second value.

Return type:

None

Example:
>>> lte(42, 0)
Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `42` must be less than or equal to `0`
>>> lte(0, 0)
>>> lte(-123.45, 0)