countriesdb/validator
Composer 安装命令:
composer require countriesdb/validator
包简介
PHP validation client for CountriesDB with Laravel validation rules and a standalone Http-based validator for country and subdivision code checks against the CountriesDB backend API.
关键字:
README 文档
README
Backend validation package for CountriesDB. Provides server-side validation for country and subdivision codes using ISO 3166-1 and ISO 3166-2 standards.
📖 Full Documentation | 🌐 Website
Important: This package only provides validation methods. Data fetching for frontend widgets must be done through frontend packages (@countriesdb/widget-core, @countriesdb/widget).
Getting Started
⚠️ API Key Required: This package requires a CountriesDB private API key to function. You must create an account at countriesdb.com to obtain your private API key. Test accounts are available with limited functionality.
- 🔑 Get your API key - Create an account and get your private key
- 📚 View documentation - Complete API reference and examples
- 💬 Support - Get help and support
Features
- ✅ Laravel Validation Rules - Drop-in validation rules (
ValidCountry,ValidSubdivision) for Laravel forms - ✅ Standalone Validator - Use the
Validatorclass directly in any PHP application - ✅ ISO 3166 Compliant - Validates ISO 3166-1 (countries) and ISO 3166-2 (subdivisions) codes
- ✅ Multiple Validation Options - Support for
follow_upward,follow_related, andallow_parent_selection - ✅ Batch Validation - Validate multiple countries or subdivisions in a single request
- ✅ Laravel 9-12 Support - Compatible with Laravel 9.0, 10.0, 11.0, and 12.0
- ✅ Detailed Error Messages - Returns specific error messages from the CountriesDB API
Installation
composer require countriesdb/validator
Usage
Standalone Validator
use CountriesDB\Validator\Validator; $validator = new Validator('YOUR_API_KEY'); // Validate a single country $result = $validator->validateCountry('US'); if ($result['valid']) { echo "Valid country"; } else { echo "Invalid: " . $result['message']; } // Validate a single subdivision $result = $validator->validateSubdivision('US-CA', 'US'); if ($result['valid']) { echo "Valid subdivision"; } // Validate multiple countries $results = $validator->validateCountries(['US', 'CA', 'MX']); foreach ($results as $result) { echo $result['code'] . ': ' . ($result['valid'] ? 'Valid' : 'Invalid'); } // Validate multiple subdivisions $results = $validator->validateSubdivisions( ['US-CA', 'US-NY', 'US-TX'], 'US' );
Laravel Validation Rules
Configuration
Add to your config/services.php:
'countriesdb' => [ 'private_key' => env('COUNTRIESDB_PRIVATE_KEY'), 'api_url' => 'https://api.countriesdb.com', ],
Using Validation Rules
use CountriesDB\Validator\Rules\ValidCountry; use CountriesDB\Validator\Rules\ValidSubdivision; // In a FormRequest public function rules() { return [ 'country' => ['required', new ValidCountry()], 'subdivision' => ['required', new ValidSubdivision('country')], ]; } // With options public function rules() { return [ 'country' => ['required', new ValidCountry(followUpward: true)], 'subdivision' => [ 'required', new ValidSubdivision( countryAttribute: 'country', followRelated: true, allowParentSelection: true ), ], ]; }
API Reference
Validator
Main validator class.
Constructor
new Validator(string $apiKey, ?string $backendUrl = null)
Parameters:
apiKey(required): Your CountriesDB API keybackendUrl(optional): Backend API URL (defaults tohttps://api.countriesdb.com)
Methods
validateCountry(code, followUpward?)
Validate a single country code.
Parameters:
code(string): ISO 3166-1 alpha-2 country codefollowUpward(bool): Check if country is referenced in a subdivision (default:false)
Returns: array with valid (bool) and optional message (string|null)
validateCountries(codes)
Validate multiple country codes.
Parameters:
codes(array): Array of ISO 3166-1 alpha-2 country codes
Returns: array of results with code, valid, and optional message
validateSubdivision(code, country, followRelated?, allowParentSelection?)
Validate a single subdivision code.
Parameters:
code(string|null): Subdivision code (e.g., 'US-CA') or null/empty stringcountry(string): ISO 3166-1 alpha-2 country codefollowRelated(bool): Check if subdivision redirects to another country (default:false)allowParentSelection(bool): Allow selecting parent subdivisions (default:false)
Returns: array with valid (bool) and optional message (string|null)
validateSubdivisions(codes, country, allowParentSelection?)
Validate multiple subdivision codes.
Parameters:
codes(array): Array of subdivision codes or null/empty stringscountry(string): ISO 3166-1 alpha-2 country codeallowParentSelection(bool): Allow selecting parent subdivisions (default:false)
Returns: array of results with code, valid, and optional message
Laravel Rules
ValidCountry
Laravel validation rule for country codes.
new ValidCountry(bool $followUpward = false)
ValidSubdivision
Laravel validation rule for subdivision codes.
new ValidSubdivision( string $countryAttribute = 'country', bool $followRelated = false, bool $allowParentSelection = false )
Examples
End-to-end samples
A full Laravel application example using this package is available in the countriesdb/examples repository:
php/backend-laravel– Full Laravel application using this package's validation rules (ValidCountryandValidSubdivision)
The example includes setup instructions and demonstrates all validation scenarios with working API routes.
Manual Validation in Controllers
If you prefer to validate manually in your controllers instead of using Laravel validation rules:
use CountriesDB\Validator\Validator; $validator = new Validator(config('services.countriesdb.private_key')); // In your controller $country = $request->input('country'); $subdivision = $request->input('subdivision'); $countryResult = $validator->validateCountry($country); if (!$countryResult['valid']) { return response()->json(['error' => $countryResult['message']], 422); } $subdivisionResult = $validator->validateSubdivision($subdivision, $country); if (!$subdivisionResult['valid']) { return response()->json(['error' => $subdivisionResult['message']], 422); } // Validation passed
Requirements
- PHP 8.0+
- Laravel 9.0+ (or illuminate/http ^9.0|^10.0|^11.0|^12.0)
- Valid CountriesDB API key
License
Proprietary (NAYEE LLC)
Copyright (c) NAYEE LLC. All rights reserved.
This software is the proprietary property of NAYEE LLC. For licensing inquiries, please contact NAYEE LLC.
countriesdb/validator 适用场景与选型建议
countriesdb/validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「composer」 「validation」 「backend」 「geography」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 countriesdb/validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 countriesdb/validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 countriesdb/validator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This composer plugin enables installation of GravityForms WordPress plugin and its addons.
Adds request-parameter validation to the SLIM 3.x PHP framework
A jQuery augmented PHP library for creating and validating HTML forms
Simple ASCII output of array data
A Laravel validator for delimiter-separated list of emails.
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 41
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: proprietary
- 更新时间: 2025-12-16