validation-pattern

Implements declarative input validation in Spring Boot using Jakarta ConstraintValidator and Spring Validator.

Updated Jun 25, 2026
One-click install
npx skills add https://github.com/oriddd/ai-toolkit --skill validation-pattern-oriddd
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: validation-pattern
Source: https://github.com/oriddd/ai-toolkit/tree/main/copilot/public/skills/validation-pattern
Command: npx skills add https://github.com/oriddd/ai-toolkit --skill validation-pattern-oriddd

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Scattered ad-hoc null checks and if/throw blocks in controllers and services make validation logic hard to test, reuse, and maintain. This Skill externalizes input validation into dedicated, layered components so business code can assume inputs are already valid. ## Core Features & Use Cases - Two validation styles: Jakarta Bean Validation ConstraintValidator for field/parameter-level rules and Spring Validator for object/cross-field rules, both wired into Spring MVC to return HTTP 400. - Layered architecture: Separates annotations (contracts), validators (orchestration), and domain rule beans (pure testable logic with no framework imports). - Testing strategies: Unit tests for rule beans and validators, plus @WebMvcTest component tests verifying the HTTP 400 path. - Use Case: When building a REST endpoint that must reject an invalid entity ID or enforce that a recurring schedule includes an interval, apply this pattern to create a custom constraint annotation, a validator, and a domain rule bean instead of inline checks. ## Quick Start Apply the validation-pattern skill to add a custom Jakarta constraint annotation and validator for the id path variable in my Spring Boot controller.

Frequently Asked Questions about validation-pattern

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I create a custom validation annotation in Spring Boot?

Create an annotation interface with @Constraint(validatedBy = {...}) and the three mandatory elements message(), groups(), and payload(). Then implement a ConstraintValidator as a Spring @Component that captures annotation attributes in initialize() and returns true or false in isValid().

Jakarta ConstraintValidator vs Spring Validator: which should I use?

Use Jakarta ConstraintValidator for single field or parameter rules that are reusable across endpoints and may need injected Spring beans. Use Spring Validator for cross-field or object-level rules on a specific DTO, registered via @InitBinder in a @ControllerAdvice.

Why is my @PathVariable validation not working in Spring Boot?

Parameter-level validation requires the @Validated annotation on the controller class in addition to the constraint annotation on the parameter. Without @Validated, Spring MVC does not trigger method-level constraint checks.

How do I test a custom ConstraintValidator in unit tests?

Instantiate the validator directly, call isValid() with a mocked ConstraintValidatorContext, and assert the boolean result. Verify that disableDefaultConstraintViolation() is called when a custom violation message is built.

When should validation logic not live inside a validator class?

Business logic should live in dedicated domain rule beans, not validators. Validators only orchestrate: they delegate to rule beans that contain pure logic with no Jakarta or Spring MVC imports, making the rules unit-testable as plain POJOs.