forked from HAL9000/cleveragents-core
2f69358dcf
All 3 simulations tested with the fixed generate prompt that passes the original DoD to the LLM. Results: - SIM12 (Rate Limiter): 7 files — algorithms.py, store.py, middleware.py, config.py, example_app.py, requirements.txt, __init__.py - SIM14 (JSON Validator): 6 files — validator.py, types.py, errors.py, cli.py, requirements.txt, __init__.py - SIM17 (API Tester): 7 files — runner.py, assertions.py, variables.py, reporter.py, cli.py, requirements.txt, test_suite_example.yaml All Python files pass compile() syntax check. All DoD files present. Files written directly via --output-dir, no manual cleanup needed.
208 lines
6.0 KiB
Python
208 lines
6.0 KiB
Python
import re
|
|
from typing import Any, Dict
|
|
from .errors import ValidationError
|
|
|
|
|
|
def validate_string(value: Any, schema: Dict[str, Any], path: str) -> None:
|
|
"""Validate string type and constraints.
|
|
|
|
Args:
|
|
value: Value to validate
|
|
schema: Schema definition
|
|
path: JSON path to the value
|
|
|
|
Raises:
|
|
ValidationError: If validation fails
|
|
"""
|
|
if not isinstance(value, str):
|
|
raise ValidationError(
|
|
path=path,
|
|
message=f"Expected string, got {type(value).__name__}"
|
|
)
|
|
|
|
# Check minLength constraint
|
|
min_length = schema.get("minLength")
|
|
if min_length is not None and len(value) < min_length:
|
|
raise ValidationError(
|
|
path=path,
|
|
message=f"String length {len(value)} is less than minimum {min_length}"
|
|
)
|
|
|
|
# Check maxLength constraint
|
|
max_length = schema.get("maxLength")
|
|
if max_length is not None and len(value) > max_length:
|
|
raise ValidationError(
|
|
path=path,
|
|
message=f"String length {len(value)} exceeds maximum {max_length}"
|
|
)
|
|
|
|
# Check pattern constraint
|
|
pattern = schema.get("pattern")
|
|
if pattern is not None:
|
|
try:
|
|
if not re.match(pattern, value):
|
|
raise ValidationError(
|
|
path=path,
|
|
message=f"String '{value}' does not match pattern '{pattern}'"
|
|
)
|
|
except re.error as e:
|
|
raise ValidationError(
|
|
path=path,
|
|
message=f"Invalid regex pattern '{pattern}': {str(e)}"
|
|
)
|
|
|
|
|
|
def validate_number(value: Any, schema: Dict[str, Any], path: str) -> None:
|
|
"""Validate number type and constraints.
|
|
|
|
Args:
|
|
value: Value to validate
|
|
schema: Schema definition
|
|
path: JSON path to the value
|
|
|
|
Raises:
|
|
ValidationError: If validation fails
|
|
"""
|
|
if not isinstance(value, (int, float)) or isinstance(value, bool):
|
|
raise ValidationError(
|
|
path=path,
|
|
message=f"Expected number, got {type(value).__name__}"
|
|
)
|
|
|
|
# Check minimum constraint
|
|
minimum = schema.get("minimum")
|
|
if minimum is not None and value < minimum:
|
|
raise ValidationError(
|
|
path=path,
|
|
message=f"Value {value} is less than minimum {minimum}"
|
|
)
|
|
|
|
# Check maximum constraint
|
|
maximum = schema.get("maximum")
|
|
if maximum is not None and value > maximum:
|
|
raise ValidationError(
|
|
path=path,
|
|
message=f"Value {value} exceeds maximum {maximum}"
|
|
)
|
|
|
|
|
|
def validate_integer(value: Any, schema: Dict[str, Any], path: str) -> None:
|
|
"""Validate integer type and constraints.
|
|
|
|
Args:
|
|
value: Value to validate
|
|
schema: Schema definition
|
|
path: JSON path to the value
|
|
|
|
Raises:
|
|
ValidationError: If validation fails
|
|
"""
|
|
if not isinstance(value, int) or isinstance(value, bool):
|
|
raise ValidationError(
|
|
path=path,
|
|
message=f"Expected integer, got {type(value).__name__}"
|
|
)
|
|
|
|
# Check minimum constraint
|
|
minimum = schema.get("minimum")
|
|
if minimum is not None and value < minimum:
|
|
raise ValidationError(
|
|
path=path,
|
|
message=f"Value {value} is less than minimum {minimum}"
|
|
)
|
|
|
|
# Check maximum constraint
|
|
maximum = schema.get("maximum")
|
|
if maximum is not None and value > maximum:
|
|
raise ValidationError(
|
|
path=path,
|
|
message=f"Value {value} exceeds maximum {maximum}"
|
|
)
|
|
|
|
|
|
def validate_boolean(value: Any, schema: Dict[str, Any], path: str) -> None:
|
|
"""Validate boolean type.
|
|
|
|
Args:
|
|
value: Value to validate
|
|
schema: Schema definition
|
|
path: JSON path to the value
|
|
|
|
Raises:
|
|
ValidationError: If validation fails
|
|
"""
|
|
if not isinstance(value, bool):
|
|
raise ValidationError(
|
|
path=path,
|
|
message=f"Expected boolean, got {type(value).__name__}"
|
|
)
|
|
|
|
|
|
def validate_array(value: Any, schema: Dict[str, Any], path: str) -> None:
|
|
"""Validate array type and constraints.
|
|
|
|
Args:
|
|
value: Value to validate
|
|
schema: Schema definition
|
|
path: JSON path to the value
|
|
|
|
Raises:
|
|
ValidationError: If validation fails
|
|
"""
|
|
if not isinstance(value, list):
|
|
raise ValidationError(
|
|
path=path,
|
|
message=f"Expected array, got {type(value).__name__}"
|
|
)
|
|
|
|
# Check minLength constraint for arrays
|
|
min_length = schema.get("minLength")
|
|
if min_length is not None and len(value) < min_length:
|
|
raise ValidationError(
|
|
path=path,
|
|
message=f"Array length {len(value)} is less than minimum {min_length}"
|
|
)
|
|
|
|
# Check maxLength constraint for arrays
|
|
max_length = schema.get("maxLength")
|
|
if max_length is not None and len(value) > max_length:
|
|
raise ValidationError(
|
|
path=path,
|
|
message=f"Array length {len(value)} exceeds maximum {max_length}"
|
|
)
|
|
|
|
|
|
def validate_object(value: Any, schema: Dict[str, Any], path: str) -> None:
|
|
"""Validate object type.
|
|
|
|
Args:
|
|
value: Value to validate
|
|
schema: Schema definition
|
|
path: JSON path to the value
|
|
|
|
Raises:
|
|
ValidationError: If validation fails
|
|
"""
|
|
if not isinstance(value, dict):
|
|
raise ValidationError(
|
|
path=path,
|
|
message=f"Expected object, got {type(value).__name__}"
|
|
)
|
|
|
|
|
|
def validate_null(value: Any, schema: Dict[str, Any], path: str) -> None:
|
|
"""Validate null type.
|
|
|
|
Args:
|
|
value: Value to validate
|
|
schema: Schema definition
|
|
path: JSON path to the value
|
|
|
|
Raises:
|
|
ValidationError: If validation fails
|
|
"""
|
|
if value is not None:
|
|
raise ValidationError(
|
|
path=path,
|
|
message=f"Expected null, got {type(value).__name__}"
|
|
) |