Initial upload: ErrorFix CLI with rule engine and pattern matching
Some checks failed
CI / test (push) Has been cancelled
Some checks failed
CI / test (push) Has been cancelled
This commit is contained in:
43
errorfix/rules/validator.py
Normal file
43
errorfix/rules/validator.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from typing import Dict, Any, List
|
||||
import re
|
||||
|
||||
|
||||
class RuleValidator:
|
||||
REQUIRED_FIELDS = ['id', 'name', 'pattern', 'fix', 'description']
|
||||
OPTIONAL_FIELDS = ['severity', 'language', 'tool', 'tags', 'priority', 'metadata']
|
||||
|
||||
def validate(self, data: Dict[str, Any]) -> bool:
|
||||
missing = [field for field in self.REQUIRED_FIELDS if field not in data]
|
||||
if missing:
|
||||
raise ValueError(f"Missing required fields: {', '.join(missing)}")
|
||||
|
||||
for field in data:
|
||||
if field not in self.REQUIRED_FIELDS + self.OPTIONAL_FIELDS:
|
||||
raise ValueError(f"Unknown field: {field}")
|
||||
|
||||
self._validate_pattern(data['pattern'])
|
||||
self._validate_severity(data.get('severity', 'error'))
|
||||
self._validate_priority(data.get('priority', 0))
|
||||
|
||||
return True
|
||||
|
||||
def _validate_pattern(self, pattern: str) -> None:
|
||||
if not isinstance(pattern, str):
|
||||
raise ValueError("Pattern must be a string")
|
||||
if not pattern.strip():
|
||||
raise ValueError("Pattern cannot be empty")
|
||||
try:
|
||||
re.compile(pattern)
|
||||
except re.error as e:
|
||||
raise ValueError(f"Invalid regex pattern: {e}")
|
||||
|
||||
def _validate_severity(self, severity: str) -> None:
|
||||
valid_severities = ['error', 'warning', 'info', 'suggestion']
|
||||
if severity not in valid_severities:
|
||||
raise ValueError(f"Invalid severity: {severity}. Must be one of {valid_severities}")
|
||||
|
||||
def _validate_priority(self, priority: int) -> None:
|
||||
if not isinstance(priority, int):
|
||||
raise ValueError("Priority must be an integer")
|
||||
if priority < 0:
|
||||
raise ValueError("Priority must be non-negative")
|
||||
Reference in New Issue
Block a user