validationhelper

package
v0.0.0-...-264534e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2026 License: MIT Imports: 4 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsNumeric

func IsNumeric(s string) bool

IsNumeric checks if a string contains only digit characters (0–9).

func IsValidAlpha

func IsValidAlpha(s string) bool

IsValidAlpha checks if the string contains only alphabetic characters using regex. An empty string is considered invalid.

func IsValidCEL deprecated

func IsValidCEL(expression string, value, structInstance any) bool

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.

Deprecated: IsValidCEL is deprecated and will be removed in a future version. govalid now converts CEL expressions to Go code at generation time for better performance. This function is kept for backward compatibility but is no longer used by govalid.

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 IsValidEmail

func IsValidEmail(email string) bool

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:

  1. Overall length check (5-254 characters per RFC 5321)
  2. Find and validate exactly one @ symbol
  3. 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: .!#$%&'*+-/=?^_`{|}~
  4. 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 IsValidURL

func IsValidURL(input string) bool

IsValidURL validates if a string is a valid URL format. This implementation prioritizes correctness while maintaining high performance.

func IsValidUUID

func IsValidUUID(s string) bool

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL