custom-validator skill
Symfonyの制約またはLaravelのバリデーションルールを使用して、API Platformリソースのカスタムバリデーション制約を作成します。
- GitHubスター
- 29
- ファイル
- 1
- 最終コミット
- 2026年10月22日
- ライセンス
- —
- 許可されたツール
- —
custom-validator skillは何をしますか
custom-validatorはAgent Skillsです。 Symfonyの制約またはLaravelのバリデーションルールを使用して、API Platformリソースのカスタムバリデーション制約を作成します。.
skillのSKILL.mdから生成された要約。
custom-validator skill について
このSKILLは、バックエンド開発者がAPI Platformリソースに対して複雑なビジネスロジックを実装することを可能にします。標準的なSymfonyやLaravelのルールでは特定の要件を満たせない場合に、カスタムバリデーション制約を実装するために必要なパターンを提供します。ドメイン固有の形式の強制、一意性のチェック、または書き込み操作中のレート制限やプラン制限の実装が必要な場合に、エージェントはこのSKILLをロードすべきです。
Symfonyベースのプロジェクトでは、アカウント制限のチェックなどのロジックを処理するために、Constraintクラスと対応するConstraintValidatorクラスを作成する方法を示します。また、複数フィールドのバリデーションのためのクラスレベル制約や、DTOのためのコールバックバリデーションについてもカバーしています。Laravel環境では、ApiResource属性内のrulesオプションの使用に焦点を当てており、配列、クロージャ、またはFormRequestクラスの文字列をサポートします。これにより、カスタムビジネスルールが422 ConstraintViolationListレスポンスを正しく返せるようになります。
エージェントができること
- 1プランに基づいたアカウント制限の強制現在のサブスクリプションプランに基づいて、ユーザーが特定のアカウント数を超えないようにするバリデーターを作成します。
skill の SKILL.md から作成されています。すべてのユースケースは、その引用と照合されています。
custom-validator skillのインストール方法
各コマンドはskillフォルダをクライアントのskillsディレクトリにコピーします。読み込むには、クライアントを再起動するか新しいセッションを開始してください。プラグインにcustom-validator skillをインストールする
/plugin marketplace add api-platform/skillset /plugin install api-platform@api-platform-skillset
Claude Codeにcustom-validator skillをインストールする
~/.claude/skills/custom-validator/SKILL.md
$ npx degit api-platform/skillset/skills/custom-validator ~/.claude/skills/custom-validatorClaude Code (project)にcustom-validator skillをインストールする
.claude/skills/custom-validator/SKILL.md
$ npx degit api-platform/skillset/skills/custom-validator .claude/skills/custom-validatorCodexにcustom-validator skillをインストールする
~/.agents/skills/custom-validator/SKILL.md
$ npx degit api-platform/skillset/skills/custom-validator ~/.agents/skills/custom-validatorGemini CLIにcustom-validator skillをインストールする
~/.gemini/skills/custom-validator/SKILL.md
$ npx degit api-platform/skillset/skills/custom-validator ~/.gemini/skills/custom-validatorClaude.aiにcustom-validator skillをインストールする
npx degit api-platform/skillset/skills/custom-validator custom-validator && zip -r custom-validator.zip custom-validator # claude.ai → Customize → Skills → + → Create skill → Upload a skill → custom-validator.zip
SKILL.md
GitHubで表示Custom Validation Constraints
Create custom validators when built-in Symfony constraints are insufficient.
Constraint Class
<?php
namespace App\Validator;
use Symfony\Component\Validator\Constraint;
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class IsValidAccountLimit extends Constraint
{
public string $message = 'Account limit reached for your current plan.';
public int $limit = 50;
}
Validator Class
Convention: {ConstraintName}Validator in the same namespace.
<?php
namespace App\Validator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class IsValidAccountLimitValidator extends ConstraintValidator
{
public function __construct(
private readonly UserHelper $userHelper,
private readonly AccountRepository $accountRepository,
) {}
public function validate(mixed $value, Constraint $constraint): void
{
if (!$constraint instanceof IsValidAccountLimit) {
throw new \InvalidArgumentException('Unexpected constraint type');
}
if (null === $value || '' === $value) {
return;
}
$user = $this->userHelper->getUser();
$count = $this->accountRepository->countByUser($user->getId());
if ($count >= $constraint->limit) {
$this->context->buildViolation($constraint->message)
->addViolation();
}
}
}
Usage on Properties
use App\Validator\IsValidAccountLimit;
use Symfony\Component\Validator\Constraints as Assert;
class Account
{
#[Assert\NotBlank(groups: ['account:write'])]
#[Assert\Email(groups: ['account:write'])]
#[IsValidAccountLimit(groups: ['account:write'])]
public ?string $address;
}
Class-Level Constraint (UniqueEntity)
For constraints that validate across multiple fields:
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class MongoDBUnique extends Constraint
{
public array $fields = [];
public string $message = 'This value is already used.';
public function getTargets(): string
{
return self::CLASS_CONSTRAINT;
}
}
Usage:
#[MongoDBUnique(fields: ['address'], groups: ['account:write', 'account:patch'])]
class Account {}
Validation Groups with Operations
Target a constraint to specific operations by passing groups: (as shown above)
and wiring those groups into each operation's validationContext — see
operations for the full per-operation setup.
Callback Validation
For quick one-off validation on DTOs:
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
class SendMessage
{
public ?string $text = null;
public ?string $html = null;
#[Assert\Callback]
public function validateContent(ExecutionContextInterface $context): void
{
if (null === $this->text && null === $this->html) {
$context->buildViolation('Either text or html content is required.')
->atPath('text')
->addViolation();
}
}
}
Laravel
Laravel does not use Symfony Constraints/ConstraintValidator. Validation is
declared with Laravel validation rules via the
rules option on #[ApiResource] or per-operation (rules can be an array, a closure,
or a FormRequest class-string). The ValidateProvider runs them before the
processor and emits the same 422 ConstraintViolationList shape.
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Post;
#[ApiResource(rules: ['title' => 'required|min:2'])]
#[Post(rules: ['isbn' => ['required', 'string']])] // per-operation override
class Book extends Model {}
For business rules beyond built-in rules, write a Laravel custom rule (a class
implementing Illuminate\Contracts\Validation\ValidationRule, scaffold with
php artisan make:rule) or a closure rule, and reference it in rules. A
FormRequest class-string is also accepted — its authorize()/rules() run, and an
AuthorizationException maps to 403, ValidationException to 422. Validation groups
and Symfony validationContext do not apply on Laravel; scope rules per operation
instead. Partial PATCH can relax required to sometimes via
partial_patch_validation in config/api-platform.php.
このスキルのファイル
1custom-validator skillに関する質問
Claude Codeにcustom-validator skillをインストールするには
npx degit api-platform/skillset/skills/custom-validator ~/.claude/skills/custom-validatorを実行してください。Claude Codeは~/.claude/skills(すべてのプロジェクト)または.claude/skills(単一プロジェクト)からskillsを読み込み、説明がタスクに一致する場合にskillを使用します。
custom-validator skillはどのクライアントで使用できますか
Agent Skills形式を読み込むすべてのクライアントで使用できます: Claude Code、Codex、Gemini CLI、claude.ai(ZIPとしてアップロード)。skill内のスクリプトは、クライアントがコード実行を許可した場合にのみ実行されます。
API PlatformでSymfonyのカスタムバリデーターを作成するにはどうすればよいですか?
同じ名前空間内で、Constraintクラスと、{ConstraintName}Validatorという命名規則に従った対応するValidatorクラスを作成する必要があります。