Coverage for src / ezcompiler / utils / validators / format_validators.py: 39.13%

17 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-27 06:49 +0000

1# /////////////////////////////////////////////////////////////// 

2# FORMAT_VALIDATORS - Format validation utilities 

3# Project: ezcompiler 

4# /////////////////////////////////////////////////////////////// 

5 

6""" 

7Format validators - Validation utilities for common data formats. 

8 

9This module provides validation functions for common data formats like 

10version strings, email addresses, and URLs. 

11""" 

12 

13from __future__ import annotations 

14 

15# /////////////////////////////////////////////////////////////// 

16# IMPORTS 

17# /////////////////////////////////////////////////////////////// 

18# Standard library imports 

19import re 

20 

21# /////////////////////////////////////////////////////////////// 

22# FUNCTIONS 

23# /////////////////////////////////////////////////////////////// 

24 

25 

26def validate_version_string(version: str) -> bool: 

27 """ 

28 Validate a version string format. 

29 

30 Args: 

31 version: Version string to validate 

32 

33 Returns: 

34 bool: True if version format is valid (e.g., 1.0.0), False otherwise 

35 

36 Note: 

37 Accepts common version formats: x.y.z, x.y.z.w, x.y, etc. 

38 

39 Example: 

40 >>> validate_version_string("1.0.0") 

41 True 

42 >>> validate_version_string("1.0") 

43 True 

44 >>> validate_version_string("invalid") 

45 False 

46 """ 

47 if not isinstance(version, str): 47 ↛ 48line 47 didn't jump to line 48 because the condition on line 47 was never true

48 return False 

49 

50 # Check for common version formats: x.y.z, x.y.z.w, x.y, etc. 

51 version_pattern = r"^\d+(\.\d+)*$" 

52 return bool(re.match(version_pattern, version)) 

53 

54 

55def validate_email(email: str) -> bool: 

56 """ 

57 Validate an email address format. 

58 

59 Args: 

60 email: Email address to validate 

61 

62 Returns: 

63 bool: True if email format is valid, False otherwise 

64 

65 Note: 

66 Uses basic regex pattern validation. Not a strict RFC 5322 validator. 

67 

68 Example: 

69 >>> validate_email("user@example.com") 

70 True 

71 >>> validate_email("invalid-email") 

72 False 

73 """ 

74 if not isinstance(email, str): 

75 return False 

76 

77 # Basic email validation pattern 

78 email_pattern = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$" 

79 return bool(re.match(email_pattern, email)) 

80 

81 

82def validate_url(url: str) -> bool: 

83 """ 

84 Validate a URL format. 

85 

86 Args: 

87 url: URL to validate 

88 

89 Returns: 

90 bool: True if URL format is valid, False otherwise 

91 

92 Note: 

93 Validates basic HTTP/HTTPS URL structure. 

94 

95 Example: 

96 >>> validate_url("https://example.com") 

97 True 

98 >>> validate_url("http://example.com/path") 

99 True 

100 >>> validate_url("invalid-url") 

101 False 

102 """ 

103 if not isinstance(url, str): 

104 return False 

105 

106 # Basic URL validation pattern 

107 url_pattern = r"^https?://[^\s/$.?#].[^\s]*$" 

108 return bool(re.match(url_pattern, url))