Kotchasan Framework Documentation

Kotchasan Framework Documentation

Validator Class - Data Validation & Security

EN 03 Feb 2026 17:01

Validator Class - Data Validation & Security

The Validator class provides a comprehensive suite of methods for verifying data integrity, ensuring file upload security, and validating complex array structures against schemas.

Namespace

Kotchasan\Validator

Functionality Categories

1. Basic Data Validation

These methods return true if the input valid, false otherwise.

email()

Validate email address format.

public static function email(string $email): bool

url()

Validate URL format.

public static function url(string $url): bool

integer() / float()

Check if value is an integer or float.

public static function integer(mixed $value): bool
public static function float(mixed $value): bool

date()

Validate date string (Format: YYYY-MM-DD).

public static function date(string $date): bool

time()

Validate time string (Format: HH:MM or HH:MM:SS).

public static function time(string $time): bool

phone()

Validate phone number (Allows digits, +, -, and spaces).

public static function phone(string $phone): bool

username()

Validate username (Allows alphanumeric, @, ., -, _).

public static function username(string $username): bool

password()

Validate password length.

public static function password(string $password, int $minLength = 8): bool

2. File Security & Uploads

isFileSafe()

Check file safety via MIME type verification and content scanning.

public static function isFileSafe(array $file, array $allowedMimes = [], bool $checkUpload = true): bool

validateFile()

Comprehensive file validation (Size, Extension, MIME type, Content Safety).

public static function validateFile(array $file, array $allowedTypes = [], array $allowedMimes = [], int $maxSize = 0, bool $checkUpload = true): bool

validateImage()

Strict image validation (Dimensions, Extensions, Content Scanning).

public static function validateImage(array $file, array $allowedTypes = ['jpg', ...], int $maxWidth = 0, int $maxHeight = 0, bool $checkUpload = true): array|bool

Returns: Array with image metadata (width, height, mime) on success, or false on failure.

scanFileContent()

Scans file content for malicious code signatures (e.g., PHP shells, script tags).

private static function scanFileContent(string $filePath): bool

3. Advanced Validation

pattern()

Validate against a Regular Expression.

public static function pattern(mixed $value, string $pattern): bool

isArrayValid()

Validate an array structure against a defined schema (supports nesting).

public static function isArrayValid(array $array, array $schema): bool

Schema Example:

$schema = [
    'username' => [
        'required' => true,
        'type' => 'string',
        'validate' => ['min' => 5, 'username' => true]
    ],
    'age' => [
        'type' => 'int',
        'validate' => ['between' => [18, 99]]
    ],
    'contact' => [
        'type' => 'array',
        'schema' => [
            'email' => ['validate' => ['email' => true]]
        ]
    ]
];

$valid = Validator::isArrayValid($data, $schema);

csrf()

Securely compare CSRF tokens (using constant-time comparison).

public static function csrf(string $token, string $sessionToken): bool

File Size: 858 lines