Coverage for src/csv_schema_validator/cli.py: 0%

44 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-20 12:32 +0200

1import sys 

2import os 

3import json 

4 

5from .validate_csv import validate_csv 

6 

7""" 

8CLI usages: 

9csv-schema-validator <csv_file> <schema_file> 

10csv-schema-validator -h 

11csv-schema-validator --help 

12csv-schema-validator -v 

13csv-schema-validator --version 

14""" 

15 

16 

17def cli(): 

18 """ 

19 Command-line interface. 

20 """ 

21 if len(sys.argv) != 3: 

22 print("Usage: csv-schema-validator <csv_file> <schema_file>", file=sys.stderr) 

23 sys.exit(1) 

24 

25 csv_file, schema_file = sys.argv[1:3] 

26 

27 if not csv_file or not schema_file: 

28 print("Usage: csv-schema-validator <csv_file> <schema_file>") 

29 sys.exit(1) 

30 

31 if not os.path.exists(csv_file): 

32 print(f"Error: CSV file {csv_file} does not exist") 

33 sys.exit(1) 

34 

35 if not os.path.exists(schema_file): 

36 print(f"Error: Schema file {schema_file} does not exist") 

37 sys.exit(1) 

38 

39 try: 

40 with open(schema_file, "r") as f: 

41 schema = json.load(f) 

42 except json.JSONDecodeError as e: 

43 print(f"Error: Invalid JSON in schema file {e}", file=sys.stderr) 

44 sys.exit(1) 

45 except IOError as e: 

46 print(f"Erro: Cannot read schema file {e}", file=sys.stderr) 

47 sys.exit(1) 

48 

49 try: 

50 result = validate_csv(csv_file, schema) 

51 # print(json.dumps(result, indent=2)) 

52 print( 

53 f"❌ Validation failed: {len(result['errors'])} errors found" 

54 if not result["is_valid"] 

55 else "✅ Validation passed" 

56 ) 

57 print() 

58 for error in result["errors"]: 

59 print( 

60 f"Row {error['row']}, Column {error['column']}: {error['error_type']}" 

61 ) 

62 print(f" Value: {error['value']}") 

63 print(f" Details: {error['details']}") 

64 print() 

65 sys.stdout.flush() 

66 except ValueError as e: 

67 print(f"Error: {e}") 

68 sys.exit() 

69 

70 

71""" 

72>>> csv-schema-validator file.csv schema.json 

73 

74❌ Validation failed: 8 errors found 

75 

76Errors: 

77Row 2, Column 'email': Invalid email format 

78 Value: "invalid-email" 

79 Expected: Valid email address matching pattern 

80 

81Row 3, Column 'department': Invalid department 

82 Value: "InvalidDepartment"  

83 Expected: One of: Engineering, Marketing, Sales, HR, Finance 

84 

85Row 3, Column 'email': Invalid email format 

86 Value: "jane.smith@company" 

87 Expected: Valid email address matching pattern 

88 

89Row 4, Column 'salary': Invalid number 

90 Value: "25o000" 

91 Expected: Valid number 

92 

93Row 5, Column 'hire_date': Invalid date format 

94 Value: "invalid-date" 

95 Expected: Date in YYYY-MM-DD format 

96 

97Row 6, Column 'is_active': Invalid boolean 

98 Value: "maybe" 

99 Expected: true or false 

100 

101Row 7, Column 'email': Invalid email format 

102 Value: "david.wilson@company" 

103 Expected: Valid email address matching pattern 

104 

105Missing required fields: 

106 - required_non_existing 

107 

108Summary: 8 errors across 7 rows 

109 

110 

111>>> csv-schema-validator file.csv schema.json --format json 

112 

113<JSON> 

114 

115 

116>>> csv-schema-validator file.csv schema.json --format compact 

117 

118R2:C4:invalid-email|R3:C5:InvalidDepartment|R3:C4:jane.smith@company|R4:C6:25o000|R5:C7:invalid-date|R6:C8:maybe|R7:C4:david.wilson@company|missing:required_non_existing 

119""" 

120 

121if __name__ == "__main__": 

122 cli()