Documentation
¶
Overview ¶
Package validationhelper provides validation helper functions for govalid.
Package validationhelper provides validation helper functions for govalid.
Package validationhelper provides validation helper functions for govalid.
Package validationhelper provides validation helper functions for govalid.
Index ¶
- func IsAlphanum(s string) bool
- func IsLowercase(s string) bool
- func IsNumber(s string) bool
- func IsNumeric(s string) bool
- func IsValidAlpha(s string) bool
- func IsValidBoolean(s string) bool
- func IsValidCEL(expression string, value, structInstance any) bool
- func IsValidColour(s string) bool
- func IsValidDateDDMMYY(s string) bool
- func IsValidEmail(email string) bool
- func IsValidFQDN(s string) bool
- func IsValidLatitude(s string) bool
- func IsValidLongitude(s string) bool
- func IsValidURI(s string) bool
- func IsValidURL(input string) bool
- func IsValidUUID(s string) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsAlphanum ¶
IsAlphanum validates if a string contains only alphanumeric characters. Returns false for empty strings.
func IsLowercase ¶
IsLowercase validates if all characters in the string are lowercase. Empty strings return true. Numbers and symbols are ignored.
func IsNumber ¶
IsNumber validates if a string contains only numeric characters. Accepts optional leading sign (+/-) and decimal point.
func IsValidAlpha ¶
IsValidAlpha checks if the string contains only alphabetic characters using regex. An empty string is considered invalid.
func IsValidBoolean ¶
IsValidBoolean validates if a string represents a valid boolean value. Accepts: "true", "false", "1", "0", "yes", "no", "on", "off" (case-insensitive).
func IsValidCEL ¶
IsValidCEL evaluates a CEL expression for validation. This function uses a simple approach without reflection, following govalid's design principles. Compiled CEL programs are cached for performance.
Parameters:
- expression: The CEL expression string to evaluate
- value: The current field value being validated
- structInstance: The entire struct instance (reserved for future use)
Returns:
- bool: true if validation passes, false if validation fails
Example usage:
if !IsValidCEL("value > 0.0", score, instance) {
return errors.New("score must be positive")
}
Note: This implementation prioritizes simplicity and performance, following govalid's zero-reflection philosophy. Cross-field validation is not supported without reflection.
func IsValidColour ¶
IsValidColour validates if a string represents a valid color. Supports: hex (#RGB, #RRGGBB, #RRGGBBAA), rgb/rgba, hsl/hsla, and named colors.
func IsValidDateDDMMYY ¶
IsValidDateDDMMYY reports whether s matches dd/mm/yy.
func IsValidEmail ¶
IsValidEmail validates email format manually for maximum performance. This function implements a simplified but practical email validation algorithm based on RFC 5321 (SMTP) and RFC 5322 (Internet Message Format) specifications.
Email format: local-part@domain
Validation steps:
- Overall length check (5-254 characters per RFC 5321)
- Find and validate exactly one @ symbol
- Validate local part (before @): - Length: 1-64 characters (RFC 5321) - No leading/trailing dots - No consecutive dots - Allowed characters: a-z, A-Z, 0-9, and special chars: .!#$%&'*+-/=?^_`{|}~
- Validate domain part (after @): - Length: 1-253 characters (RFC 1035) - Must contain at least one dot - Must have at least 2 labels (e.g., "example.com") - Each label: 1-63 characters, alphanumeric + hyphen, no leading/trailing hyphen
Example breakdown:
user.name+tag@sub.example.com
^^^^^^^^^|^^^|^^^^^^^^^^^^^^^^
local |tag|domain
+ @ (separator)
Note: This implementation does not support:
- Quoted strings in local part (e.g., "john doe"@example.com)
- IP addresses as domains (e.g., user@[192.168.1.1])
- Internationalized domain names (IDN)
- Comments in email addresses
func IsValidFQDN ¶
IsValidFQDN validates if a string is a Fully Qualified Domain Name. FQDN must have at least one dot, valid characters, and proper structure.
func IsValidLatitude ¶
IsValidLatitude validates if a string represents a valid latitude (-90 to 90).
func IsValidLongitude ¶
IsValidLongitude validates if a string represents a valid longitude (-180 to 180).
func IsValidURI ¶
IsValidURI validates if a string is a valid URI (more permissive than URL). Accepts any valid URI scheme, not just HTTP/HTTPS.
func IsValidURL ¶
IsValidURL validates if a string is a valid URL format. This implementation prioritizes correctness while maintaining high performance.
func IsValidUUID ¶
IsValidUUID validates UUID format manually for maximum performance. This function implements UUID validation according to RFC 4122 specification.
UUID format: XXXXXXXX-XXXX-MXXX-NXXX-XXXXXXXXXXXX Where:
X = any hexadecimal digit (0-9, a-f, A-F) M = version field (1-5) N = variant field (8, 9, A, B)
Structure (36 characters total):
8 hex digits - time_low 4 hex digits - time_mid 4 hex digits - time_hi_and_version (version in 3rd position) 4 hex digits - clock_seq_hi_and_reserved (variant in 1st position) 12 hex digits - node
Example breakdown:
550e8400-e29b-41d4-a716-446655440000
^^^^^^^^|^^^^|^^^^|^^^^|^^^^^^^^^^^^^
8 |4 |4 |4 |12 hex digits
time_low| |ver |var |node
|time| |clk |
|_mid| |_seq|
Position indices:
0-7: time_low 8: hyphen 9-12: time_mid 13: hyphen 14-17: time_hi_and_version (version at position 14) 18: hyphen 19-22: clock_seq (variant at position 19) 23: hyphen 24-35: node
Valid versions (RFC 4122):
1 = Time-based 2 = DCE Security 3 = Name-based (MD5) 4 = Random 5 = Name-based (SHA-1)
Valid variants (high nibble of clock_seq_hi):
8, 9, A, B = RFC 4122 variant (10xx in binary)
Types ¶
This section is empty.