承接 countriesdb/validator 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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.

Latest Version Total Downloads License

📖 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.

Features

  • Laravel Validation Rules - Drop-in validation rules (ValidCountry, ValidSubdivision) for Laravel forms
  • Standalone Validator - Use the Validator class 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, and allow_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 key
  • backendUrl (optional): Backend API URL (defaults to https://api.countriesdb.com)

Methods

validateCountry(code, followUpward?)

Validate a single country code.

Parameters:

  • code (string): ISO 3166-1 alpha-2 country code
  • followUpward (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 string
  • country (string): ISO 3166-1 alpha-2 country code
  • followRelated (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 strings
  • country (string): ISO 3166-1 alpha-2 country code
  • allowParentSelection (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 (ValidCountry and ValidSubdivision)

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 countriesdb/validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 41
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: proprietary
  • 更新时间: 2025-12-16