CTY Validation¶
The pyvider.cty.validation module provides utilities for preventing infinite recursion during validation of deeply nested or circular data structures.
๐ค AI-Generated Content
This documentation was generated with AI assistance and is still being audited. Some, or potentially a lot, of this information may be inaccurate. Learn more.
Key components:
- RecursionContext - Tracks visited objects during validation to detect cycles
- RecursionDetector - Manages recursion detection state
- with_recursion_detection - Decorator that adds recursion detection to validation methods
- get_recursion_context() - Returns the current recursion context
- clear_recursion_context() - Clears the recursion detection state
- validate_config(schema, config) - Convenience function for validating configurations against schemas (raises CtyValidationError on failure)
The recursion detection system is used internally by all types during the validation process. You typically won't need to interact with it directly unless you're implementing custom types that need to participate in cycle detection.
Usage Example:
from pyvider.cty import CtyObject, CtyString
from pyvider.cty.validation import validate_config
schema = CtyObject(attribute_types={"name": CtyString()})
config = {"name": "Alice"}
# This validates and raises on error, but doesn't return the CtyValue
validate_config(schema, config)
# For most use cases, prefer calling validate() directly on the type:
validated_value = schema.validate(config) # Returns CtyValue
For comprehensive validation documentation, see: User Guide: Validation
pyvider.cty.validation
¶
Classes¶
RecursionContext
dataclass
¶
RecursionContext(
validation_graph: dict[int, ValidationNode] = dict(),
validation_path: list[str] = list(),
max_depth_reached: int = 0,
total_validations: int = 0,
validation_start_time: float = time.time(),
max_depth_allowed: int = MAX_VALIDATION_DEPTH,
max_object_revisits: int = MAX_OBJECT_REVISITS,
max_validation_time_ms: int = MAX_VALIDATION_TIME_MS,
validation_stopped: bool = False,
)
RecursionDetector
¶
Advanced recursion detector for CTY validation.
This detector uses sophisticated algorithms to distinguish between: - Circular references (object A -> object B -> object A) - Deep but finite nesting (legitimate complex configurations) - Performance pathological cases (excessive validation time)
Source code in pyvider/cty/validation/recursion.py
Functions¶
should_continue_validation
¶
Determine if validation should continue for the given value.
Returns:
| Type | Description |
|---|---|
tuple[bool, str | None]
|
(should_continue, reason_if_stopped) |
Production requirements: - Must handle legitimate deep nesting (1000+ levels) - Must detect genuine circular references quickly - Must provide detailed diagnostics for debugging - Must have predictable performance characteristics
Source code in pyvider/cty/validation/recursion.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
enter_validation_scope
¶
exit_validation_scope
¶
get_current_path
¶
get_performance_metrics
¶
Get performance metrics for monitoring and debugging.
Source code in pyvider/cty/validation/recursion.py
Functions¶
clear_recursion_context
¶
get_recursion_context
¶
Get or create thread-local recursion context.
Source code in pyvider/cty/validation/recursion.py
with_recursion_detection
¶
Decorator for advanced recursion detection in validation functions.
Source code in pyvider/cty/validation/recursion.py
validate_config
¶
Validates a configuration against a CtyType schema.
This function serves as the primary entry point for validation,
delegating to the validate method of the provided schema. It allows
the CtyValidationError to propagate, which is the expected contract
for testing and low-level framework integration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Any
|
The CtyType object to validate against. |
required |
config
|
Any
|
The raw Python data to validate. |
required |
Raises:
| Type | Description |
|---|---|
CtyValidationError
|
If the configuration does not conform to the schema. |