校验单个文件 参数: validator: 校验器实例 file_path: 文件路径 strict: 是否严格模式 返回: 是否通过校验
(validator: NodeYamlValidator, file_path: Path, strict: bool = False)
| 27 | |
| 28 | |
| 29 | def validate_file(validator: NodeYamlValidator, file_path: Path, strict: bool = False) -> bool: |
| 30 | """校验单个文件 |
| 31 | |
| 32 | 参数: |
| 33 | validator: 校验器实例 |
| 34 | file_path: 文件路径 |
| 35 | strict: 是否严格模式 |
| 36 | |
| 37 | 返回: |
| 38 | 是否通过校验 |
| 39 | """ |
| 40 | result = validator.validate_yaml_file(file_path) |
| 41 | |
| 42 | if result.is_valid and (not strict or len(result.warnings) == 0): |
| 43 | print(f"[PASS] {file_path.name} - Valid") |
| 44 | if result.warnings: |
| 45 | for warning in result.warnings: |
| 46 | print(f" [WARN] {warning.field}: {warning.message}") |
| 47 | return True |
| 48 | else: |
| 49 | error_count = len(result.errors) |
| 50 | warning_count = len(result.warnings) |
| 51 | status = f"Invalid ({error_count} errors" |
| 52 | if warning_count > 0: |
| 53 | status += f", {warning_count} warnings" |
| 54 | status += ")" |
| 55 | print(f"[FAIL] {file_path.name} - {status}") |
| 56 | |
| 57 | for error in result.errors: |
| 58 | print(f" [ERROR] {error.field}: {error.message}") |
| 59 | if error.suggestion: |
| 60 | print(f" Hint: {error.suggestion}") |
| 61 | |
| 62 | for warning in result.warnings: |
| 63 | print(f" [WARN] {warning.field}: {warning.message}") |
| 64 | |
| 65 | return False |
| 66 | |
| 67 | |
| 68 | def validate_directory( |
no test coverage detected