VOOZH about

URL: https://deepwiki.com/hypervel/devtool/11.2-validation-rule-generation

⇱ Validation Rule Generation | hypervel/devtool | DeepWiki


Loading...
Menu

Validation Rule Generation

Purpose and Scope

This document describes the validation rule generation system provided by the make:rule command. This generator creates custom validation rule classes that implement Hypervel's validation interface, allowing developers to define reusable validation logic for form requests and validation operations.

For generating form request validation classes that use these rules, see Request and Resource Generation. For other code generators, see Additional Generators.


Command Overview

The validation rule generator is implemented by the RuleCommand class, which extends the base GeneratorCommand infrastructure to create validation rule classes.

RuleCommand Class Structure

The RuleCommand class src/Generator/RuleCommand.php9-32 provides a minimal implementation focused on rule generation:

MethodPurposeReturn Value
__construct()Registers command name make:rulevoid
configure()Sets description "Create a new validation rule"void
getStub()Returns path to rule.stub templatestring
getDefaultNamespace()Returns default namespace App\Rulesstring

Sources: src/Generator/RuleCommand.php1-32


Class Hierarchy and Dependencies


Sources: src/Generator/RuleCommand.php9-32 src/Generator/stubs/rule.stub1-22


Stub Template Structure

The validation rule stub src/Generator/stubs/rule.stub1-22 defines the generated class structure:

Template Components


Key Template Features

FeatureImplementationLine Reference
Strict typingdeclare(strict_types=1)src/Generator/stubs/rule.stub3
Namespace placeholder%NAMESPACE%src/Generator/stubs/rule.stub5
Class placeholder%CLASS%src/Generator/stubs/rule.stub10
Interface implementationimplements ValidationRulesrc/Generator/stubs/rule.stub10
Validation methodvalidate(string $attribute, mixed $value, Closure $fail): voidsrc/Generator/stubs/rule.stub17
Fail closure typeClosure(string, ?string=): \Hypervel\Translation\PotentiallyTranslatedStringsrc/Generator/stubs/rule.stub15

Sources: src/Generator/stubs/rule.stub1-22


Generation Process Flow


Sources: src/Generator/RuleCommand.php9-32 src/Generator/stubs/rule.stub1-22


Generated Validation Rule Specification

Output File Structure

When executing make:rule UppercaseRule, the generator produces:

File Location: app/Rules/UppercaseRule.php

Class Structure:


Validation Method Contract

The validate() method src/Generator/stubs/rule.stub17-20 receives three parameters:

ParameterTypePurpose
$attributestringThe name of the field being validated
$valuemixedThe value to validate
$failClosureCallback to invoke when validation fails

The $fail closure accepts:

  • First parameter: Error message (string)
  • Second parameter: Translation parameters (optional string)
  • Returns: PotentiallyTranslatedString instance

Sources: src/Generator/stubs/rule.stub15-20


Command Usage Patterns


Basic Usage


Common Validation Rule Patterns

Use CaseExample Class NameImplementation Purpose
Custom format validationPhoneNumberRuleValidate phone number format
Business logic validationUniqueEmailRuleCheck email uniqueness with custom logic
Complex conditional validationConditionalRequiredRuleRequire field based on other fields
External API validationRecaptchaRuleValidate reCAPTCHA token
Database constraint validationExistsInActiveRecordsRuleCheck existence with additional constraints

Sources: src/Generator/RuleCommand.php9-32


Configuration Options

Namespace Configuration

The RuleCommand supports namespace customization through configuration src/Generator/RuleCommand.php28-31:


Stub Template Configuration

The stub template location can be customized src/Generator/RuleCommand.php23-26:


Configuration Resolution Flow


Sources: src/Generator/RuleCommand.php23-31


Integration with Validation System

Usage in Form Requests

Generated validation rules integrate with Hypervel's form request validation:


Manual Validator Usage

Rules can also be used with manual validator instances:


Validation Flow


Sources: src/Generator/stubs/rule.stub8-21


Command Registration

The RuleCommand is registered through the ConfigProvider src/ConfigProvider.php as part of the 28 generator commands:


This registration occurs only when Hyperf\Devtool\Generator\GeneratorCommand exists, ensuring graceful degradation if the base generator infrastructure is unavailable.

Sources: src/Generator/RuleCommand.php9-14


Comparison with Other Generators

Similarities to ConsoleCommand

Both RuleCommand and ConsoleCommand src/Generator/ConsoleCommand.php11-52 follow the same generator pattern:

AspectRuleCommandConsoleCommand
Base classGeneratorCommandGeneratorCommand
Stub locationstubs/rule.stubstubs/console.stub
Custom optionsNone--command for signature
Placeholder replacementStandardAdds %COMMAND%
Default namespaceApp\RulesApp\Console\Commands

Key Differences


Sources: src/Generator/RuleCommand.php9-32 src/Generator/ConsoleCommand.php11-52