VOOZH about

URL: https://deepwiki.com/hypervel/validation/3-core-validation-engine

⇱ Core Validation Engine | hypervel/validation | DeepWiki


Loading...
Menu

Core Validation Engine

Purpose and Scope

This document provides a deep dive into the Validator class, the central orchestrator of the validation system. The Validator coordinates validation execution, manages validation state, collects errors, and integrates with external dependencies to provide a complete validation solution.

The Validator uses three primary traits to organize its responsibilities:

  • ValidatesAttributes: Contains 50+ built-in validation rule implementations
  • FormatsMessages: Handles error message retrieval, formatting, and placeholder replacement
  • ReplacesAttributes: Manages attribute name transformations and display formatting

For detailed information on built-in validation rules, see Built-in Validation Rules. For the complete validation lifecycle, see Validation Lifecycle. For custom rule creation, see Custom Validation Rules.

Validator Class Architecture

The Validator class serves as the central orchestrator for all validation operations. It coordinates the execution of validation rules, manages state, collects errors, and provides the public API for validation operations.

Class Structure Overview


Sources: src/Validator.php30-306

Trait Responsibility Distribution

The Validator delegates specialized functionality to three core traits:

TraitResponsibilityKey MethodsLine Reference
ValidatesAttributesImplements 50+ validation rulesvalidateEmail(), validateRequired(), validateUnique(), etc.src/Concerns/ValidatesAttributes.php37-4200
FormatsMessagesError message generation and formattinggetMessage(), makeReplacements(), getDisplayableAttribute()src/Concerns/FormatsMessages.php1-500
ReplacesAttributesAttribute name transformationsreplaceAttributePlaceholder(), getAttributeFromTranslations()src/Concerns/ReplacesAttributes.php1-200

Sources: src/Validator.php32-33 src/Concerns/ValidatesAttributes.php37

Validation Orchestration

The Validator orchestrates the complete validation process through a series of coordinated steps, from rule parsing through error collection to final result reporting.

High-Level Orchestration Flow


Sources: src/Validator.php391-441 src/Validator.php554-606 src/Validator.php747-796

Rule Validation Execution


Sources: src/Validator.php554-606 src/Validator.php747-796 src/Validator.php827-854

State Management

The Validator maintains several categories of state throughout the validation process to track data, rules, errors, and configuration.

Core State Properties

PropertyTypePurposeInitialization
$dataarrayData under validation, with placeholders for dots/asterisksConstructor via parseData()
$rulesarrayExpanded validation rules (wildcards resolved)setRules() via ValidationRuleParser
$initialRulesarrayOriginal rules before wildcard expansionConstructor
$messagesMessageBagCollection of validation error messagespasses() method
$failedRulesarrayMap of attribute => rule => parameters for failurespasses() method
$excludeAttributesarrayAttributes marked for exclusion from validated dataDuring validation

Sources: src/Validator.php58-84

Data Placeholder System

The Validator uses a placeholder system to handle dots and asterisks in attribute names, preventing conflicts with array notation:


Sources: src/Validator.php310-368

Rule Expansion and Implicit Attributes

The Validator expands wildcard rules to explicit rules for each matching attribute:


Sources: src/Validator.php1069-1085 src/Validator.php640-648

Error Collection and Failed Rules Tracking


Sources: src/Validator.php827-854 src/Validator.php801-822

Dependency Integration

The Validator integrates with several external dependencies to provide comprehensive validation capabilities.

Required Dependencies

Translator Integration

The Translator is a required dependency for retrieving localized error messages:


Sources: src/Validator.php288-305 src/Concerns/FormatsMessages.php1-500

Optional Dependencies

PresenceVerifier for Database Validation

The PresenceVerifier enables database validation rules (unique, exists):


Sources: src/Validator.php1297-1316 src/Concerns/ValidatesAttributes.php788-883

Container for Custom Extensions

The PSR-11 Container enables dependency injection for custom extensions:


Sources: src/Validator.php1377-1409

Extension System

The Validator provides a flexible extension system for registering custom validation rules, implicit rules, dependent rules, and message replacers.

Extension Registration Methods

MethodPurposeBehaviorLine Reference
addExtension()Register custom ruleAdds to $extensions arraysrc/Validator.php1177-1180
addImplicitExtension()Register implicit custom ruleAdds to $extensions and $implicitRulessrc/Validator.php1185-1190
addDependentExtension()Register dependent custom ruleAdds to $extensions and $dependentRulessrc/Validator.php1195-1200
addReplacer()Register message replacerAdds to $replacers arraysrc/Validator.php1219-1222

Extension Invocation Flow


Sources: src/Validator.php1139-1222 src/Validator.php1385-1429

Implicit Rules and Dependent Rules

The Validator maintains lists of special rule types that affect validation behavior:


Implicit Rules (e.g., Required, Present): Execute even when attribute is missing from data.

Dependent Rules (e.g., Same, RequiredIf): Reference other attributes; parameters are expanded with wildcard keys.

Sources: src/Validator.php163-236 src/Validator.php688-705 src/Validator.php611-614

Helper Method Categories

The trait includes several categories of helper methods that support the core validation logic.

Size and Comparison Helpers

Helper MethodPurposeUsage Context
getSize()Calculate size of valuesMin/max validation
getDateTimestamp()Convert to timestampDate comparisons
compare()Generic comparisonNumeric/date validation
isSameType()Type compatibility checkComparison validation

Parameter Processing Helpers

Helper MethodPurposeLine Reference
requireParameterCount()Validate parameter count4098-4104
parseNamedParameters()Parse key=value parameters4116-4124
parseDependentRuleParameters()Parse conditional parameters1921-1927
getExtraConditions()Extract query conditions981-992

File Validation Helpers

Helper MethodPurposeLine Reference
isValidFileInstance()Check file validity4050-4053
shouldBlockPhpUpload()PHP file security check1386-1404
failsBasicDimensionChecks()Image dimension validation639-647

Sources: src/Concerns/ValidatesAttributes.php4098-4104 src/Concerns/ValidatesAttributes.php4116-4124 src/Concerns/ValidatesAttributes.php1921-1927