nanayawkumi / nyk-input-validator
Laravel package to sanitize and validate usernames, emails, phones, names, Ghana IDs, and URLs beyond default validation rules
Package info
github.com/nanayawkumi/nyk-input-validator
pkg:composer/nanayawkumi/nyk-input-validator
Requires
- php: ^8.2
- illuminate/support: ^10.0|^11.0|^12.0|^13.0
- illuminate/validation: ^10.0|^11.0|^12.0|^13.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0|^10.0|^11.0
- pestphp/pest: ^2.0|^3.0|^4.0
README
Laravel package to sanitize and validate usernames, emails, Ghana phone numbers, names, Ghana identity documents, and URLs — beyond default Laravel / Livewire rules.
Requirements
- PHP ^8.2 (Laravel 13 requires PHP ^8.3)
- Laravel ^10.0|^11.0|^12.0|^13.0
Installation
composer require nanayawkumi/nyk-input-validator
Publish the config (optional):
php artisan vendor:publish --tag=nyk-input-validator-config
Combined API
Sanitize and validate in one call. Returns a value object, or throws InvalidInputException.
use Nanayawkumi\InputValidator\Facades\Input; $username = Input::username(' johnny_doe '); // "johnny_doe" $email = Input::email('John.Doe+news@Example.COM'); // "john.doe@example.com" $phone = Input::phone('+233 24 123 4567'); // "0241234567" $first = Input::firstName('Ama'); $last = Input::lastName("O'Brien"); $full = Input::fullName('Jean-Pierre Mensah'); $card = Input::ghanaCard('gha1234567890'); // "GHA-123456789-0" $voter = Input::voterId('9001330422'); $passport = Input::passport('g1234567'); // "G1234567" $url = Input::url('https://example.com');
Phone helpers
use Nanayawkumi\InputValidator\Phone\GhPhoneValidator; GhPhoneValidator::normalize('+233241234567'); // 0241234567 GhPhoneValidator::formatNational('0241234567'); // 024 123 4567 GhPhoneValidator::formatInternational(...); // +233 24 123 4567 GhPhoneValidator::formatE164(...); // +233241234567 GhPhoneValidator::network('0241234567'); // Network::MTN
$phone = Input::phone('0241234567'); $phone->national(); $phone->international(); $phone->e164(); $phone->network();
Validation rules
String rules:
| Rule | Purpose |
|---|---|
nyk_username |
Username |
nyk_email |
Email (normalize + disposable + MX) |
nyk_phone / gh_phone |
Ghana phone |
nyk_first_name |
First name |
nyk_last_name |
Last name |
nyk_full_name |
Full name |
nyk_ghana_card |
Ghana Card |
nyk_voter_id |
Voter ID |
nyk_passport |
Passport |
nyk_url |
HTTPS URL |
$request->validate([ 'username' => ['required', 'nyk_username'], 'email' => ['required', 'nyk_email'], 'phone' => ['required', 'gh_phone'], 'ghana_card' => ['required', 'nyk_ghana_card'], 'website' => ['nullable', 'nyk_url'], ]);
Rule objects:
use Nanayawkumi\InputValidator\Rules\Username; use Nanayawkumi\InputValidator\Rules\Email; use Nanayawkumi\InputValidator\Rules\GhPhone; use Nanayawkumi\InputValidator\Rules\SecureUrl; $request->validate([ 'username' => ['required', new Username], 'email' => ['required', new Email], 'phone' => ['required', new GhPhone], 'website' => ['nullable', new SecureUrl], ]);
Eloquent casts
use Nanayawkumi\InputValidator\Casts\GhPhoneCast; use Nanayawkumi\InputValidator\Casts\SanitizedCast; protected function casts(): array { return [ 'phone' => GhPhoneCast::class, // or GhPhoneCast::class.':e164' 'username' => SanitizedCast::class.':username', 'email' => SanitizedCast::class.':email', 'ghana_card' => SanitizedCast::class.':ghana_card', ]; }
Livewire
use Nanayawkumi\InputValidator\Livewire\SanitizesInputs; class RegisterForm extends Component { use SanitizesInputs; public string $username = ''; public string $email = ''; protected function inputSanitizers(): array { return [ 'username' => 'username', 'email' => 'email', ]; } protected function rules(): array { return [ 'username' => ['required', 'nyk_username'], 'email' => ['required', 'nyk_email'], ]; } }
The trait soft-sanitizes on update (does not throw). Validate with rules on submit.
If your component already defines updated(), call $this->sanitizeUpdatedInput($name, $value) from it.
Defaults (configurable)
| Input | Behavior |
|---|---|
| Username | a-zA-Z0-9._-, 3–30 chars, no leading ._-, ASCII only, reserved list |
Lowercase entire address, strip +tag, block disposable domains, DNS/MX check |
|
| Phone | Ghana numbers (multi-country later), normalize to 0XXXXXXXXX |
| Names | Letters + spaces + hyphen/apostrophe, no digits, 3–50 chars |
| Ghana Card | GHA-XXXXXXXXX-X (uppercase) |
| Voter ID | 10 digits (pattern configurable) |
| Passport | A/AB + 6–7 digits (pattern configurable) |
| URL | HTTPS only, block localhost/private IPs, resolve DNS |
Relationship to gh-phone-validator
Ghana phone validation/normalization from nanayawkumi/gh-phone-validator is incorporated here (gh_phone rule, casts, network enum, formatters). Prefer this package going forward; the standalone phone package can be deprecated later.
Testing
composer test
License
MIT © Samuel Kumi-Buabeng