Check
The Check
class provides a fluent interface for making assertions about policy data. The object keeps track of accessed data within the component JSON and records that for traceability purposes. The final result of a check will have not just the status (pass
, fail
, no-data
or error
), but complete information about which JSON paths were used to reach this conclusion. Designed to be used as a context manager with Python's with
statement, the Check
class automatically handles NoDataError
, tracks result statuses, and provides isolation between different policy checks on "no-data" outcomes.
Constructor
name (str): A unique identifier for this check
description (str, optional): A human-readable description of what this check validates
component_data (ComponentData, optional): A ComponentData instance to use for this check
Context Manager
The Check
class is designed to be used as a context manager with Python's with
statement. This is the recommended way to use the class as it ensures proper setup, teardown, and error handling.
When used as a context manager, the Check
class:
On enter: Sets up the check context and automatically loads component data if not provided
On exit: Records the check result with its status and all accessed data paths
Exception handling:
Catches
NoDataError
and sets the check status to "no-data" instead of "fail"Records other exceptions as "error" status with the exception message
Prevents exceptions from propagating, allowing subsequent checks to execute
This means you can have multiple checks in sequence, and if one fails with a NoDataError
, the others will still execute:
Note, however that other exceptions will still propagate, so you should still handle those appropriately.
Data Access Methods
get
Retrieves data from the component JSON using a JSONPath expression.
path (str): JSONPath expression to query the component data
Returns: The value at the specified path, or raises
NoDataError
if not found
Example:
get_all
Retrieves data from all metadata instances using a JSONPath expression. This method is useful if there are path collisions in the component JSON.
path (str): JSONPath expression to query the component data
Returns: A list of values matching the path across all metadata instances, or raises
NoDataError
if not found
Example:
Assertion Methods
All assertion methods have these common parameters:
value: Can be a direct value, or a
Path
objectfailure_message (str, optional): Custom message to display if the assertion fails
all_instances (bool, optional): If True, checks all metadata instances, not just the merged blob
Additionally, all assertion methods raise NoDataError
if the path doesn't exist in the component data.
assert_true
Asserts that a value is True
.
Example:
assert_false
Asserts that a value is False
.
Example:
assert_equals
Asserts that a value equals the expected value.
expected: The expected value to compare against
Example:
assert_contains
Asserts that a value contains the expected value (works for strings, lists, etc.).
expected: The value that should be contained
Example:
assert_greater
Asserts that a numeric value is greater than the expected value.
expected: The threshold value to compare against
Example:
assert_greater_or_equal
Asserts that a numeric value is greater than or equal to the expected value.
expected: The threshold value to compare against
Example:
assert_less
Asserts that a numeric value is less than the expected value.
expected: The threshold value to compare against
Example:
assert_less_or_equal
Asserts that a numeric value is less than or equal to the expected value.
expected: The threshold value to compare against
Example:
assert_match
Asserts that a string value matches a regular expression pattern.
pattern (str): A regular expression pattern to match against
Example:
fail
Unconditionally fails the check with a given message.
message (str): The message to display when the check fails
Example:
Result Methods
get_assertions
Retrieves the list of assertions made in this check.
Returns: A list of all assertions made in this check
Each assertion object contains information about the assertion:
passed
(bool): Whether the assertion passedmessage
(str): The assertion messageexpected
: The expected value (if applicable)actual
: The actual value (if applicable)
Last updated