Laravel Case Format Validation Rules: A Complete Guide to CamelCase, PascalCase, SnakeCase, and KebabCase Validation
The milwad-dev/laravel-validate package provides four dedicated case format validation rules—ValidCamelCase, ValidPascalCase, ValidSnakeCase, and ValidKebabCase—that enforce string naming conventions through Unicode-aware regex patterns.
All case format validators reside in the Milwad\LaravelValidate\Rules namespace and implement Laravel's Illuminate\Contracts\Validation\Rule contract. These rules allow you to enforce consistent naming conventions for API keys, database columns, URL slugs, and class names directly within your form requests and validation logic.
Available Case Format Validation Rules
The package ships with four distinct validators located in src/Rules/. Each rule uses a specific regular expression to validate input strings against standard programming naming conventions.
ValidCamelCase
The ValidCamelCase rule validates strings following the camelCase convention, allowing an optional leading capital followed by lowercase groups separated by uppercase characters. According to the source code in src/Rules/ValidCamelCase.php, the implementation uses a Unicode-aware regex pattern:
preg_match('/^(?:\p{Lu}?\p{Ll}+)(?:\p{Lu}\p{Ll}+)*$/u', $value)
This pattern supports international characters while ensuring each word boundary is marked by an uppercase letter. The rule returns the translation key validate.camel-case for localized error messages.
ValidPascalCase
The ValidPascalCase rule enforces PascalCase formatting where each word starts with an uppercase letter followed by lowercase characters. As implemented in src/Rules/ValidPascalCase.php, the validator uses an ASCII-specific regex:
preg_match('/^[A-Z][a-z]+(?:[A-Z][a-z]+)*$/', $value)
This stricter pattern ensures class names and type identifiers follow standard PHP naming conventions. Failed validations trigger the validate.pascal-case translation key.
ValidSnakeCase
The ValidSnakeCase rule validates snake_case strings where lowercase words connect via underscores. The implementation in src/Rules/ValidSnakeCase.php utilizes Unicode character properties:
preg_match('/^(?:\p{Ll}+_)*\p{Ll}+$/u', $value)
This pattern accepts lowercase letters from any Unicode script while requiring underscore separators between words. The rule references the validate.snake-case translation key for error messaging.
ValidKebabCase
The ValidKebabCase rule ensures strings follow the kebab-case convention with hyphen-separated lowercase words. Found in src/Rules/ValidKebabCase.php, the validation logic employs:
preg_match('/^(?:\p{Ll}+\-)*\p{Ll}+$/u', $value)
This regex validates URL slugs and CSS class names by requiring hyphens between lowercase Unicode word groups. Errors are localized via the validate.kebab-case key.
How to Use Case Format Validators in Laravel
The package offers two integration methods: direct rule instantiation for one-off validation or container-based registration for string-based validation rules.
Method 1: Direct Rule Instantiation
Instantiate the rule classes directly in your validation arrays. This approach provides IDE autocompletion and explicit dependency injection:
use Milwad\LaravelValidate\Rules\ValidCamelCase;
use Milwad\LaravelValidate\Rules\ValidSnakeCase;
$request->validate([
'apiKey' => ['required', new ValidCamelCase()],
'table_name' => ['required', new ValidSnakeCase()],
]);
Method 2: Container-Based Registration
Enable automatic registration via the service provider by setting 'using_container' => true in config/laravel-validate.php. The LaravelValidateServiceProvider then iterates over the src/Rules directory during the boot() method, registering each rule using its snake-case filename:
// config/laravel-validate.php
return [
'using_container' => true,
];
// Available as string rules
$request->validate([
'className' => 'required|validpascalcase',
'url-slug' => 'required|validkebabcase',
]);
When using container registration, the package automatically loads translation files via loadTranslationsFrom(), ensuring error messages resolve through Laravel's localization system using the validate.* keys defined in each rule class.
Complete Validation Examples
Validating API Keys with CamelCase
use Milwad\LaravelValidate\Rules\ValidCamelCase;
$validated = $request->validate([
'stripeApiKey' => ['required', 'string', new ValidCamelCase()],
]);
// Passes: "myStripeKey", "testApiKey"
// Fails: "MyApiKey", "my_api_key"
Enforcing Database Naming Conventions with SnakeCase
use Milwad\LaravelValidate\Rules\ValidSnakeCase;
$validated = $request->validate([
'migration_name' => ['required', new ValidSnakeCase()],
]);
// Passes: "create_users_table", "add_email_index"
// Fails: "createUsersTable", "create-users-table"
Validating URL Slugs with KebabCase
use Milwad\LaravelValidate\Rules\ValidKebabCase;
$validated = $request->validate([
'post_slug' => ['required', new ValidKebabCase()],
]);
// Passes: "my-awesome-post", "laravel-validation-guide"
// Fails: "my_awesome_post", "MyAwesomePost"
Validating Class Names with PascalCase
use Milwad\LaravelValidate\Rules\ValidPascalCase;
$validated = $request->validate([
'controller_name' => ['required', new ValidPascalCase()],
]);
// Passes: "UserController", "AdminDashboard"
// Fails: "userController", "user_controller"
Summary
- Four case formats supported: The package provides
ValidCamelCase,ValidPascalCase,ValidSnakeCase, andValidKebabCaserules insrc/Rules/. - Unicode-aware validation: CamelCase, SnakeCase, and KebabCase rules support international character sets through
\p{Ll}and\p{Lu}regex patterns, while PascalCase uses strict ASCII matching. - Flexible integration: Use direct instantiation for type safety or enable
'using_container' => trueto reference rules as strings likevalidcamelcase. - Localized error messages: All rules return translation keys (
validate.camel-case,validate.pascal-case, etc.) managed by theLaravelValidateServiceProvider. - Source locations: Implementation details reside in
src/Rules/ValidCamelCase.php,src/Rules/ValidPascalCase.php,src/Rules/ValidSnakeCase.php, andsrc/Rules/ValidKebabCase.php.
Frequently Asked Questions
Does laravel-validate support Unicode characters in case validation?
Yes, three of the four rules support Unicode. According to the source code in milwad-dev/laravel-validate, the ValidCamelCase, ValidSnakeCase, and ValidKebabCase rules use Unicode property escapes (\p{Ll} for lowercase and \p{Lu} for uppercase) with the /u modifier. This allows validation of strings containing accented characters or non-Latin scripts. However, ValidPascalCase uses strict ASCII ranges [A-Z] and [a-z] as defined in src/Rules/ValidPascalCase.php.
How do I customize the error messages for case validation failures?
Override the translation keys. Each rule returns a specific translation key: validate.camel-case, validate.pascal-case, validate.snake-case, or validate.kebab-case. The LaravelValidateServiceProvider loads these from the package's translation files. Publish the language files using php artisan vendor:publish and modify the corresponding keys in your resources/lang directory to customize the validation messages.
Can I use these rules with Laravel's FormRequest classes?
Absolutely. Since all case format rules implement Illuminate\Contracts\Validation\Rule, they work seamlessly in FormRequest classes. Inject the rule classes via the rules() method or use string-based rules if you have enabled container registration in config/laravel-validate.php. Both approaches support Laravel's automatic validation error handling and redirection.
What is the performance impact of using these regex-based validators?
Negligible for typical HTTP requests. The regular expressions used in src/Rules/ValidCamelCase.php and related files are anchored (^ and $) and use possessive quantifiers where possible, ensuring linear time complexity relative to string length. The Unicode property checks in \p{Ll} and \p{Lu} are optimized in PHP's PCRE engine, making these validators suitable for high-throughput applications validating API keys or slugs.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →