[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M79N2A14SPGW88RPV2CT6PRP":3,"skill-same:01M79N2A14SPGW88RPV2CT6PRP":43},{"skillNo":4,"name":5,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":15,"categories":16,"keywords":17,"publishedAt":18,"linkOnly":19,"markdownContent":20,"markdownBody":20,"frontMatter":21,"refName":22,"commitSha":23,"skillDirectory":24,"forksCount":13,"repositoryHtmlUrl":25,"repositoryHomepageUrl":6,"ownerHtmlUrl":26,"ownerBlogUrl":6,"ownerDescription":27,"updatedAt":28,"install":29,"resources":42},"01M79N2A14SPGW88RPV2CT6PRP","bean-validation-cheat-sheet",null,"JunMystery\u002FAgent-Guidance","JunMystery","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F161468934?v=4","01M79MTMV8F0BY8NK1A92RA8A0","Jun","01M79MTMWCFBKPHN5B25E6V20B",0,"MIT","https:\u002F\u002Fgithub.com\u002FJunMystery\u002FAgent-Guidance\u002Fblob\u002Fmain\u002Fskills\u002Fbean-validation-cheat-sheet\u002FSKILL.md",[],[],"2026-11-15 14:19:17",false,"# Bean Validation Cheat Sheet\n\n## Introduction\n\nThis article is focused on providing clear, simple, actionable guidance for providing Java Bean Validation security functionality in your applications.\n\nBean validation (aka [Jakarta Validation](https:\u002F\u002Fbeanvalidation.org\u002F)) is one of the most common ways to perform [input validation](https:\u002F\u002Fcheatsheetseries.owasp.org\u002Fcheatsheets\u002FInput_Validation_Cheat_Sheet.html) in Java. It is an application layer agnostic validation spec which provides the developer with the means to define a set of validation constraints on a domain model and then perform validation of those constraints through out the various application tiers.\n\nOne advantage of this approach is that the validation constraints and the corresponding validators are only written once, thus reducing duplication of effort and ensuring uniformity:\n\n### Typical Validation\n\n![Typical](..\u002Fassets\u002FBean_Validation_Cheat_Sheet_Typical.png)\n\n### Bean Validation\n\n![JSR](..\u002Fassets\u002FBean_Validation_Cheat_Sheet_JSR.png)\n\n## Setup\n\nThe illustrative examples below use the legacy `javax.validation` API, including its [built-in constraint package](https:\u002F\u002Fdocs.hibernate.org\u002Fbeanvalidation\u002Fspec\u002F1.1\u002Fapi\u002Fjavax\u002Fvalidation\u002Fconstraints\u002Fpackage-summary.html), with Hibernate Validator. [Jakarta Validation 3.0](https:\u002F\u002Fjakarta.ee\u002Fspecifications\u002Fbean-validation\u002F3.0\u002Fjakarta-bean-validation-spec-3.0.html) uses `jakarta.validation`; use API and provider versions compatible with your application.\n\nAdd Hibernate Validator to your **pom.xml**:\n\n```xml\n\u003Cdependency>\n   \u003CgroupId>org.hibernate\u003C\u002FgroupId>\n   \u003CartifactId>hibernate-validator\u003C\u002FartifactId>\n   \u003Cversion>USE_LATEST_VERSION\u003C\u002Fversion>\n\u003C\u002Fdependency>\n```\n\nEnable bean validation support in Spring's **context.xml**:\n\n```xml\n\u003Cbeans:beans ...\n   ...\n   \u003Cmvc:annotation-driven \u002F>\n   ...\n\u003C\u002Fbeans:beans>\n```\n\nFor more info, please see the [setup guide](https:\u002F\u002Fhibernate.org\u002Fvalidator\u002Fdocumentation\u002Fgetting-started\u002F)\n\n## Basics\n\nIn order to get started using Bean Validation, you must add validation constraints (`@Pattern`, `@Digits`, `@Min`, `@Max`, `@Size`, `@Past`, `@Future`, `@CreditCardNumber`, `@Email`, `@URL`, etc.) to your model and then utilize the `@Valid` annotation when passing your model around in various application layers.\n\nConstraints can be applied in several places:\n\n- Fields\n- Properties\n- Classes\n\nFor Bean Validation 1.1 also on:\n\n- Parameters\n- Return values\n- Constructors\n\nFor the sake of simplicity all the examples below feature field constraints and all validation is triggered by the controller. Refer to the Bean Validation documentation for a full list of examples.\n\nWhen it comes to error handling, the Hibernate Validator returns a `BindingResult` object which contains a `List\u003CObjectError>`. The examples below feature simplistic error handling, while a production ready application would have a more elaborate design that takes care of logging and error page redirection.\n\n## Predefined Constraints\n\n### @Pattern\n\n**Annotation**:\n\n`@Pattern(regexp=,flags=)`\n\n**Data Type**:\n\n`CharSequence`\n\n**Use**:\n\nChecks if the annotated string matches the regular expression regex considering the given flag match. Please visit [OWASP Validation Regex Repository](https:\u002F\u002Fowasp.org\u002Fwww-community\u002FOWASP_Validation_Regex_Repository) for other useful regex's.\n\n**Reference**:\n\n[Documentation](https:\u002F\u002Fdocs.jboss.org\u002Fhibernate\u002Fvalidator\u002F5.2\u002Freference\u002Fen-US\u002Fhtml\u002Fch02.html#section-builtin-constraints)\n\n**Model**:\n\n```java\nimport javax.validation.constraints.Pattern;\n\npublic class Article  {\n \u002F\u002FConstraint: One or more ASCII letters, digits, or spaces\n @Pattern(regexp = \"[a-zA-Z0-9 ]+\")\n private String articleTitle;\n public String getArticleTitle()  {\n  return  articleTitle;\n }\n public void setArticleTitle(String  articleTitle)  {\n   this.articleTitle  =  articleTitle;\n  }\n\n  ...\n\n}\n```\n\n**Controller**:\n\n```java\nimport javax.validation.Valid;\nimport com.company.app.model.Article;\n\n@Controller\npublic class ArticleController  {\n\n ...\n\n @RequestMapping(value = \"\u002FpostArticle\",  method = RequestMethod.POST)\n public @ResponseBody String postArticle(@Valid  Article  article,  BindingResult  result,\n HttpServletResponse  response) {\n  if (result.hasErrors()) {\n   String errorMessage  =  \"\";\n   response.setStatus(HttpServletResponse.SC_BAD_REQUEST);\n   List\u003CObjectError> errors = result.getAllErrors();\n   for(ObjectError  e :  errors) {\n    errorMessage += \"ERROR: \" +  e.getDefaultMessage();\n   }\n   return  errorMessage;\n  } else {\n   return  \"Validation Successful\";\n  }\n }\n}\n```\n\n### @Digits\n\n**Annotation**:\n\n`@Digits(integer=,fraction=)`\n\n**Data Type**:\n\n`BigDecimal`, `BigInteger`, `CharSequence`, `byte`, `short`, `int`, `long` and the respective wrappers of the primitive types; Additionally supported by HV: any sub-type of Number\n\n**Use**:\n\nChecks whether the annotated value is a number having up to integer digits and fraction fractional digits\n\n**Reference**:\n\n[Documentation](https:\u002F\u002Fdocs.jboss.org\u002Fhibernate\u002Fvalidator\u002F5.2\u002Freference\u002Fen-US\u002Fhtml\u002Fch02.html#section-builtin-constraints)\n\n**Model**:\n\n```java\nimport javax.validation.constraints.Digits;\n\npublic class Customer {\n  \u002F\u002FConstraint: Age can only be 3 digits long or less\n  @Digits(integer = 3, fraction = 0)\n  private int age;\n\n  public int getAge()  {\n    return age;\n  }\n\n  public void setAge(int age)  {\n      this.age = age;\n    }\n\n    ...\n}\n```\n\n**Controller**:\n\n```java\nimport javax.validation.Valid;\nimport com.company.app.model.Customer;\n\n@Controller\npublic class CustomerController  {\n\n ...\n\n @RequestMapping(value = \"\u002FregisterCustomer\",  method = RequestMethod.POST)\n public @ResponseBody String registerCustomer(@Valid Customer customer, BindingResult result,\n HttpServletResponse  response) {\n\n  if (result.hasErrors()) {\n   String errorMessage = \"\";\n   response.setStatus(HttpServletResponse.SC_BAD_REQUEST);\n   List\u003CObjectError> errors = result.getAllErrors();\n\n   for( ObjectError  e :  errors) {\n    errorMessage += \"ERROR: \"  +  e.getDefaultMessage();\n   }\n   return  errorMessage;\n  } else {\n   return  \"Validation Successful\";\n  }\n }\n}\n```\n\n### @Size\n\n**Annotation**:\n\n`@Size(min=,` `max=)`\n\n**Data Type**:\n\n`CharSequence`, `Collection`, `Map` and `Arrays`\n\n**Use**:\n\nChecks if the annotated element's size is between min and max (inclusive)\n\n**Reference**:\n\n[Documentation](https:\u002F\u002Fdocs.jboss.org\u002Fhibernate\u002Fvalidator\u002F5.2\u002Freference\u002Fen-US\u002Fhtml\u002Fch02.html#section-builtin-constraints)\n\n**Model**:\n\n```java\nimport javax.validation.constraints.Size;\n\npublic class Message {\n\n   \u002F\u002FConstraint: Message must contain 10 to 500 characters, inclusive\n   @Size(min = 10, max = 500)\n   private String message;\n\n   public String getMessage() {\n      return message;\n   }\n\n   public void setMessage(String message) {\n      this.message = message;\n   }\n\n...\n}\n```\n\n**Controller**:\n\n```java\nimport javax.validation.Valid;\nimport com.company.app.model.Message;\n\n@Controller\npublic class MessageController {\n\n...\n\n@RequestMapping(value=\"\u002FsendMessage\", method=RequestMethod.POST)\npublic @ResponseBody String sendMessage(@Valid Message message, BindingResult result,\nHttpServletResponse response){\n\n   if(result.hasErrors()){\n      String errorMessage = \"\";\n      response.setStatus(HttpServletResponse.SC_BAD_REQUEST);\n      List\u003CObjectError> errors = result.getAllErrors();\n      for( ObjectError e : errors){\n         errorMessage+= \"ERROR: \" + e.getDefaultMessage();\n      }\n      return errorMessage;\n   }\n   else{\n      return \"Validation Successful\";\n   }\n}\n}\n```\n\n### @Past \u002F @Future\n\n**Annotation**:\n\n`@Past,` `@Future`\n\n**Data Type**:\n\n`java.util.Date`, `java.util.Calendar`, `java.time.chrono.ChronoZonedDateTime`, `java.time.Instant`, `java.time.OffsetDateTime`\n\n**Use**:\n\nChecks whether the annotated date is in the past \u002F future. Use a supported date type, such as `java.util.Date`; [`@Future` does not validate date strings](https:\u002F\u002Fdocs.hibernate.org\u002Fbeanvalidation\u002Fspec\u002F1.1\u002Fapi\u002Fjavax\u002Fvalidation\u002Fconstraints\u002FFuture.html).\n\n**Reference**:\n\n[Documentation](https:\u002F\u002Fdocs.jboss.org\u002Fhibernate\u002Fvalidator\u002F5.2\u002Freference\u002Fen-US\u002Fhtml\u002Fch02.html#section-builtin-constraints)\n\n**Model**:\n\n```java\nimport java.util.Date;\nimport javax.validation.constraints.Past;\nimport javax.validation.constraints.Future;\n\npublic class DoctorVisit {\n\n   \u002F\u002FConstraint: Birthdate must be in the past\n   @Past\n   private Date birthDate;\n\n   public Date getBirthDate() {\n      return birthDate;\n   }\n\n   public void setBirthDate(Date birthDate) {\n      this.birthDate = birthDate;\n   }\n\n   \u002F\u002FConstraint: Schedule visit date must be in the future\n   @Future\n   private Date scheduledVisitDate;\n\n   public Date getScheduledVisitDate() {\n      return scheduledVisitDate;\n   }\n\n   public void setScheduledVisitDate(Date scheduledVisitDate) {\n      this.scheduledVisitDate = scheduledVisitDate;\n   }\n\n...\n}\n```\n\n**Controller**:\n\n```java\nimport javax.validation.Valid;\nimport com.company.app.model.DoctorVisit;\n\n@Controller\npublic class DoctorVisitController {\n\n   ...\n\n   @RequestMapping(value=\"\u002FscheduleVisit\", method=RequestMethod.POST)\n   public @ResponseBody String scheduleVisit(@Valid DoctorVisit doctorvisit, BindingResult result,\n   HttpServletResponse response){\n\n      if(result.hasErrors()){\n         String errorMessage = \"\";\n         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);\n         List\u003CObjectError> errors = result.getAllErrors();\n         for( ObjectError e : errors){\n            errorMessage+= \"ERROR: \" + e.getDefaultMessage();\n         }\n         return errorMessage;\n      }\n      else{\n         return \"Validation Successful\";\n      }\n   }\n}\n```\n\n### Combining Constraints\n\nValidation annotations can be combined in any suitable way. For instance, to specify a valid reviewRating value between 1 and 5, specify the validation like this :\n\n**Annotation**:\n\n`@Min(value=),` `@Max(value=)`\n\n**Data Type**:\n\n`BigDecimal`, `BigInteger`, `byte`, `short`, `int`, `long` and the respective wrappers of the primitive types; Additionally supported by HV: any sub-type of `CharSequence` (the numeric value represented by the character sequence is evaluated), any sub-type of Number\n\n**Use**:\n\nChecks whether the annotated value is higher\u002Flower than or equal to the specified minimum\n\n**Reference:**\n\n[Documentation](https:\u002F\u002Fdocs.jboss.org\u002Fhibernate\u002Fvalidator\u002F9.0\u002Freference\u002Fen-US\u002Fhtml_single\u002F#section-builtin-constraints)\n\n**Model**:\n\n```java\nimport javax.validation.constraints.Min;\nimport javax.validation.constraints.Max;\n\npublic class Review {\n\n \u002F\u002FConstraint: Review rating must be between 1 and 5\n @Min(1)\n @Max(5)\n private int reviewRating;\n\n public int getReviewRating() {\n   return reviewRating;\n }\n\n public void setReviewRating(int reviewRating) {\n   this.reviewRating = reviewRating;\n}\n ...\n}\n```\n\n**Controller**:\n\n```java\nimport javax.validation.Valid;\nimport com.company.app.model.ReviewRating;\n\n@Controller\npublic class ReviewController {\n\n   ...\n\n   @RequestMapping(value=\"\u002FpostReview\", method=RequestMethod.POST)\n   public @ResponseBody String postReview(@Valid Review review, BindingResult result,\n   HttpServletResponse response){\n\n      if(result.hasErrors()){\n         String errorMessage = \"\";\n         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);\n         List\u003CObjectError> errors = result.getAllErrors();\n         for( ObjectError e : errors){\n            errorMessage+= \"ERROR: \" + e.getDefaultMessage();\n         }\n         return errorMessage;\n      }\n       else{\n         return \"Validation Successful\";\n      }\n   }\n}\n```\n\n### Cascading Constraints\n\nValidating one bean is a good start, but often, beans are nested or in a complete graph of beans. To validate that graph in one go, apply cascading validation with [@Valid](https:\u002F\u002Fdocs.jboss.org\u002Fhibernate\u002Fvalidator\u002F9.0\u002Freference\u002Fen-US\u002Fhtml_single\u002F#_cascaded_validation)\n\n### Additional Constraints\n\nIn addition to providing the complete set of JSR303 constraints, Hibernate Validator also defines some additional constraints for convenience:\n\n- `@CreditCardNumber`\n- `@EAN`\n- `@Email`\n- `@Length`\n- `@Range`\n- `@ScriptAssert`\n- `@URL`\n\nTake a look at this [list](https:\u002F\u002Fdocs.jboss.org\u002Fhibernate\u002Fvalidator\u002F9.0\u002Freference\u002Fen-US\u002Fhtml_single\u002F#validator-defineconstraints-hv-constraints) for the complete list.\n\nNote that `@SafeHtml`, a previously valid constraint, has been deprecated according to the [Hibernate Validator 6.1.0.Final and 6.0.18.Final release blogpost](https:\u002F\u002Fin.relation.to\u002F2019\u002F11\u002F20\u002Fhibernate-validator-610-6018-released\u002F). Please refrain from using the `@SafeHtml` constraint.\n\n## Custom Constraints\n\nOne of the most powerful features of bean validation is the ability to define your own constraints that go beyond the simple validation offered by built-in constraints.\n\nCreating custom constraints is beyond the scope of this guide. Please see this [documentation](https:\u002F\u002Fdocs.jboss.org\u002Fhibernate\u002Fvalidator\u002F).\n\n## Error Messages\n\nIt is possible to specify a message ID with the validation annotation, so that error messages are customized :\n\n```java\n@Pattern(regexp = \"[a-zA-Z0-9 ]+\", message=\"article.title.error\")\nprivate String articleTitle;\n```\n\nSpring MVC will then look up a message with ID *article.title.error* in a defined MessageSource. More on this [documentation](https:\u002F\u002Fwww.silverbaytech.com\u002F2013\u002F04\u002F16\u002Fcustom-messages-in-spring-validation\u002F).\n\n## References\n\n- [Jakarta Bean Validation 3.0 Specification](https:\u002F\u002Fjakarta.ee\u002Fspecifications\u002Fbean-validation\u002F3.0\u002Fjakarta-bean-validation-spec-3.0.html)\n- [Hibernate Validator 9.0: Interpolating Constraint Error Messages](https:\u002F\u002Fdocs.hibernate.org\u002Fvalidator\u002F9.0\u002Freference\u002Fen-US\u002Fhtml_single\u002F#chapter-message-interpolation)\n",{},"main","5d0a87185c0e172c3901e2a6e5916b6d544b8b94","skills\u002Fbean-validation-cheat-sheet","https:\u002F\u002Fgithub.com\u002FJunMystery\u002FAgent-Guidance","https:\u002F\u002Fgithub.com\u002FJunMystery","AI is the typist, I am the architect. Building efficient, secure, and user-centric products through disciplined AI prompt engineering.","2026-11-13 17:41:25",{"source":7,"skillName":5,"commands":30,"prompt":40,"zipAvailable":41},[31,34,37],{"tool":32,"command":33},"npx","npx skills add JunMystery\u002FAgent-Guidance --skill bean-validation-cheat-sheet",{"tool":35,"command":36},"bunx","bunx skills add JunMystery\u002FAgent-Guidance --skill bean-validation-cheat-sheet",{"tool":38,"command":39},"pnpm","pnpm dlx skills add JunMystery\u002FAgent-Guidance --skill bean-validation-cheat-sheet","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002FJunMystery\u002FAgent-Guidance\u002Fblob\u002Fmain\u002Fskills\u002Fbean-validation-cheat-sheet\u002FSKILL.md",true,[],[44,50,56,62,68,74],{"skillNo":45,"name":46,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":47,"categories":48,"keywords":49,"publishedAt":18},"01M79QR1E309VJ6CTQKB32AWWA","zero-trust-architecture-cheat-sheet","https:\u002F\u002Fgithub.com\u002FJunMystery\u002FAgent-Guidance\u002Fblob\u002Fmain\u002Fskills\u002Fzero-trust-architecture-cheat-sheet\u002FSKILL.md",[],[],{"skillNo":51,"name":52,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":53,"categories":54,"keywords":55,"publishedAt":18},"01M79QQV7GHYR02DYGN41S23WN","xss-filter-evasion-cheat-sheet","https:\u002F\u002Fgithub.com\u002FJunMystery\u002FAgent-Guidance\u002Fblob\u002Fmain\u002Fskills\u002Fxss-filter-evasion-cheat-sheet\u002FSKILL.md",[],[],{"skillNo":57,"name":58,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":59,"categories":60,"keywords":61,"publishedAt":18},"01M79QQNSN7YB8XB61FFSCS3WH","xs-leaks-cheat-sheet","https:\u002F\u002Fgithub.com\u002FJunMystery\u002FAgent-Guidance\u002Fblob\u002Fmain\u002Fskills\u002Fxs-leaks-cheat-sheet\u002FSKILL.md",[],[],{"skillNo":63,"name":64,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":65,"categories":66,"keywords":67,"publishedAt":18},"01M79QQHZZJKFDY9132G5A65C2","xpath-injection-prevention-cheat-sheet","https:\u002F\u002Fgithub.com\u002FJunMystery\u002FAgent-Guidance\u002Fblob\u002Fmain\u002Fskills\u002Fxpath-injection-prevention-cheat-sheet\u002FSKILL.md",[],[],{"skillNo":69,"name":70,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":71,"categories":72,"keywords":73,"publishedAt":18},"01M79QQB9ZSD3DJG8658EHQXR1","xml-security-cheat-sheet","https:\u002F\u002Fgithub.com\u002FJunMystery\u002FAgent-Guidance\u002Fblob\u002Fmain\u002Fskills\u002Fxml-security-cheat-sheet\u002FSKILL.md",[],[],{"skillNo":75,"name":76,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":77,"categories":78,"keywords":79,"publishedAt":18},"01M79QQ60WKV7PFMXWT9CW8PM6","xml-external-entity-prevention-cheat-sheet","https:\u002F\u002Fgithub.com\u002FJunMystery\u002FAgent-Guidance\u002Fblob\u002Fmain\u002Fskills\u002Fxml-external-entity-prevention-cheat-sheet\u002FSKILL.md",[],[]]