Kotchasan Framework Documentation
Validator Class - Data Validation & Security
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\ValidatorFunctionality 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): boolurl()
Validate URL format.
public static function url(string $url): boolinteger() / float()
Check if value is an integer or float.
public static function integer(mixed $value): bool
public static function float(mixed $value): booldate()
Validate date string (Format: YYYY-MM-DD).
public static function date(string $date): booltime()
Validate time string (Format: HH:MM or HH:MM:SS).
public static function time(string $time): boolphone()
Validate phone number (Allows digits, +, -, and spaces).
public static function phone(string $phone): boolusername()
Validate username (Allows alphanumeric, @, ., -, _).
public static function username(string $username): boolpassword()
Validate password length.
public static function password(string $password, int $minLength = 8): bool2. 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): boolvalidateFile()
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): boolvalidateImage()
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|boolReturns: 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): bool3. Advanced Validation
pattern()
Validate against a Regular Expression.
public static function pattern(mixed $value, string $pattern): boolisArrayValid()
Validate an array structure against a defined schema (supports nesting).
public static function isArrayValid(array $array, array $schema): boolSchema 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): boolFile Size: 858 lines