├── .gitignore ├── LICENSE ├── README.md ├── VERSION ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── main ├── java │ └── net │ │ └── andreinc │ │ └── jbvext │ │ ├── annotations │ │ ├── date │ │ │ ├── After.java │ │ │ ├── Before.java │ │ │ ├── IsDate.java │ │ │ └── validator │ │ │ │ ├── AfterValidator.java │ │ │ │ ├── BeforeValidator.java │ │ │ │ └── IsDateValidator.java │ │ ├── digits │ │ │ ├── MaxDigits.java │ │ │ ├── MinDigits.java │ │ │ └── validator │ │ │ │ ├── MaxDigitsDoubleValidator.java │ │ │ │ └── MinDigitsDoubleValidator.java │ │ ├── misc │ │ │ ├── InstanceOf.java │ │ │ ├── NotInstanceOf.java │ │ │ ├── OneOfChars.java │ │ │ ├── OneOfDoubles.java │ │ │ ├── OneOfIntegers.java │ │ │ ├── OneOfLongs.java │ │ │ ├── OneOfStrings.java │ │ │ └── validators │ │ │ │ ├── InstanceOfValidator.java │ │ │ │ ├── NotInstanceOfValidator.java │ │ │ │ ├── OneOfCharsValidator.java │ │ │ │ ├── OneOfDoublesValidator.java │ │ │ │ ├── OneOfIntegersValidator.java │ │ │ │ ├── OneOfLongsValidator.java │ │ │ │ ├── OneOfStringsValidator.java │ │ │ │ └── OneOfValidator.java │ │ └── str │ │ │ ├── Alpha.java │ │ │ ├── AlphaSpace.java │ │ │ ├── Alphanumeric.java │ │ │ ├── AlphanumericSpace.java │ │ │ ├── AsciiPrintable.java │ │ │ ├── Blank.java │ │ │ ├── CC.java │ │ │ ├── CreditCardType.java │ │ │ ├── EndsWith.java │ │ │ ├── IPv4.java │ │ │ ├── IPv6.java │ │ │ ├── LowerCase.java │ │ │ ├── Numeric.java │ │ │ ├── Parseable.java │ │ │ ├── ParseableType.java │ │ │ ├── Password.java │ │ │ ├── StartsWith.java │ │ │ ├── UpperCase.java │ │ │ └── validators │ │ │ ├── AlphaSpaceValidator.java │ │ │ ├── AlphaValidator.java │ │ │ ├── AlphanumericSpaceValidator.java │ │ │ ├── AlphanumericValidator.java │ │ │ ├── AsciiPrintableValidator.java │ │ │ ├── BlankValidator.java │ │ │ ├── CCValidator.java │ │ │ ├── EndsWithValidator.java │ │ │ ├── GenericStringValidator.java │ │ │ ├── IPv4Validator.java │ │ │ ├── IPv6Validator.java │ │ │ ├── LowerCaseValidator.java │ │ │ ├── NumericValidator.java │ │ │ ├── ParseableValidator.java │ │ │ ├── PasswordValidator.java │ │ │ ├── StartsWithValidator.java │ │ │ └── UpperCaseValidator.java │ │ └── utils │ │ ├── BeanValidationException.java │ │ ├── SimpleValidation.java │ │ ├── TimeAction.java │ │ └── TimeActionResponse.java └── resources │ └── ValidationMessages.properties └── test └── java └── net └── andreinc └── jbvext └── test ├── AfterBeforeTest.java ├── AfterTest.java ├── AlphaSpaceTest.java ├── AlphaTest.java ├── AlphanumericSpaceTest.java ├── AlphanumericTest.java ├── AsciiPrintableTest.java ├── BeforeTest.java ├── BlankTest.java ├── CCTest.java ├── EndsWithTest.java ├── IPv4Test.java ├── IPv6Test.java ├── InstanceOfTest.java ├── IsDateTest.java ├── LowerCaseTest.java ├── MaxDigitsDoubleValidatorTest.java ├── MinDigitsDoubleValidatorTest.java ├── MinDigitsMaxDigitsTest.java ├── NotInstanceOfTest.java ├── NumericTest.java ├── OneOfStringsTest.java ├── ParseableTest.java ├── PasswordTest.java ├── StartsWithTest.java └── UpperCaseTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.iml 3 | .gradle/* 4 | .idea/* 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # java-bean-validation-extension (JBVExt) 2 | 3 | *JBVE* (Java Bean Validation Extension) is a small utils library that extends the [Java Bean Validation Specification](http://beanvalidation.org) with additional @Annotations. 4 | 5 | If you are not familiar with JSR-380 (or the Java Bean Validation Specification) please follow [this nice tutorial](http://www.baeldung.com/javax-validation) first. 6 | 7 | If you want to see *JVBExt* at work please: 8 | - read this [blog article](http://andreinc.net/2017/10/02/writing-an-unified-validation-mechanism-for-rest-apis-using-spring-boot-and-jsr-380/); 9 | - check out this [repository](https://github.com/nomemory/spring-boot-jbvext-example). 10 | 11 | ## Installing the library 12 | 13 | For versions (`>=0.0.12`): 14 | 15 | ```xml 16 | 17 | net.andreinc 18 | jbve 19 | 0.0.12 20 | 21 | ``` 22 | *Important note(s):* 23 | 24 | In the runtime environment you will an existing JSR-380 implementation in the classpath. Spring Boot started web comes by default with [Hibernate Validator](http://hibernate.org/validator/). 25 | 26 | If you are using the library in another environment that doesn't provide a JSR-380 implementation you will need to add the following as dependencies: 27 | 28 | ```groovy 29 | compile group: 'org.hibernate', name: 'hibernate-validator', version: '6.0.2.Final' 30 | compile group: 'org.glassfish', name: 'javax.el', version: '3.0.1-b08' 31 | ``` 32 | 33 | ## Who is using jbvext ? 34 | 35 | **Java Bean Validation Extension** was downloaded +/-450 times since October, 2017. 36 | 37 | If you are using **jbvext** in your cool projects please send me a note so I can include you in this list. 38 | 39 | ## Additional supported annotations 40 | 41 | | @Annotation | Supported Types | Description | 42 | | --- | --- | --- | 43 | | [`@After`](#after) | `Date` | Check if the Date is after the given date value, with date format as parameter. | 44 | | [`@Alpha`](#alpha) | `String` | Checks if the String contains only unicode letters. | 45 | | [`@Alphanumeric`](#alphanumeric) | `String` | Checks if the String contains unly unicode letters or digits | 46 | | [`@AlphanumericSpace`](#alphanumericspace) | `String` | Checks if the String contains only unicode letters, digits, empty strings or spaces. | 47 | | [`@AlphaSpace`](#alphaspace) | `String` | Checks if the String contains only Unicode letters and space `" "`. | 48 | | [`@AsciiPrintable`](#asciiprintable) | `String` | Checks if the String contains only ASCII printable characters. | 49 | | [`@Blank`](#blank) | `String` | Checks if the String is empty `""`, null or whitespace(s) `" "` only. | 50 | | [`@Before`](#before) | `Date` | Check if the Date is before the given date value, with date format as parameter. | 51 | | [`@CC`](#cc) | `String` | Checks if the String is a valid credit card number. | 52 | | [`@EndsWith`](#endswith) | `String` | Checks if the Strings ends with a specified suffix(es). | 53 | | [`@InstanceOf`](#instanceof) | `Object` | Check if the Object is an `instanceof` of (at least one of) the supplied value(s). | 54 | | [`@IPv4`](#ipv4) | `String` | Checks if the String is a valid IPv4 address. | 55 | | [`@IPv6`](#ipv6) | `String` | Checks if the String is a valid IPv6 address. | 56 | | [`@IsDate`](#isdate) | `String` | Check if the String is in a date format. | 57 | | [`@LowerCase`](#lowercase) | `String` | Checks if the String contains only lowercase letters. | 58 | | [`@MinDigits`](#mindigits) | `Double` | Checks whether the annotated value is higher than or equal to the specified minimum. | 59 | | [`@MaxDigits`](#maxdigits) | `Double` | Checks whether the annotated value is less than or equal to the specified maximum. | 60 | | [`@NotInstanceOf`](#notinstanceof) | `Object` | Check if the is not an `instanceof` of (all the) the supplied value(s). | 61 | | [`@Numeric`](#numeric) | `String` | Checks if the String contains only unicode digits. *Note: A decimal point is not considered a digit and the validation fails. Use `@Parseable` instead for more advanced validations*. | 62 | | [`@OneOfChars`](#oneofchars) | `Character` | Checks if the Character is contained in a given array (`char[]`) of values. | 63 | | [`@OneOfDoubles`](#oneofdoubles) | `Double` | Check if the Double is contained in a given array (`double[]`) of values. | 64 | | [`@OneOfIntegers`](#oneofintegers) | `Integer` | Check if the Integer is contained in a given array (`int[]`) of values. | 65 | | [`@OneOfLongs`](#oneoflongs) | `Long` | Check if the Long is contained in a given array (`long[]`) of values. | 66 | | [`@OneOfStrings`](#oneofstrings) | `String` | Checks if the String is contained in a given array (`String[]`) of values. | 67 | | [`@Parseable`](#parseable) | `String` | Checks if the String can be parsed to a number (`Short`, `Integer`, `Long`, `Double`, etc.). | 68 | | [`@Password`](#password) | `String` | Checks if the String is a valid password. | 69 | | [`@StartsWith`](#startswith) | `String` | Checks if the String starts with the specified prefix(es). | 70 | | [`@UpperCase`](#uppercase) | `String` | Checks if the String contains only uppercase letters. | 71 | 72 | 73 | *Note:* 74 | 75 | *All the examples are using [project's lombok](https://projectlombok.org) annotations like `@Data`, `@NoArgsConstructor`, `@AllArgsConstructor`, etc.* 76 | *Those annotations are used make the examples more compact, but their use is optional.* 77 | 78 | ### `@After` 79 | 80 | Check if the Date is after the given date value. 81 | 82 | The annotation supports a second property format that by default is "yyyy-MM-dd'T'HH:mm:ss.SSSZ". 83 | 84 | #### Example 85 | 86 | ```java 87 | @Data 88 | class AfterBean { 89 | @After(value = "2018-01-01", format = "yyyy-MM-dd") 90 | private Date isAfter = new Date(1522399999911L); // Passes // Date = 2018-03-30 91 | } 92 | ``` 93 | 94 | ### `@Alpha` 95 | 96 | Check if the String contains only unicode letters. 97 | 98 | Behavior: 99 | 100 | | Value | Result | 101 | | --- | --- 102 | | `null` | :x: Fails | 103 | | `""` | :x: Fails | 104 | | `" "` | :x: Fails | 105 | | `"abc"` | :white_check_mark: Passes | 106 | | `"ab2c"` | :x: Fails | 107 | | `"ab-c"` | :x: Fails | 108 | 109 | 110 | ### Example 111 | ```java 112 | @Data 113 | class TestAlpha { 114 | @Alpha 115 | private String alpha = "abc"; 116 | 117 | @Alpha /** Will fail */ 118 | private String nonAlpha = "pr�s-*"; 119 | } 120 | ``` 121 | 122 | ### `@Alphanumeric` 123 | 124 | Checks if the String contains only unicode letters or digits. 125 | 126 | Behavior: 127 | 128 | | Value | Result | 129 | | --- | --- | 130 | | `null` | :x: Fails | 131 | | `""` | :x: Fails | 132 | | `" "` | :x: Fails | 133 | | `"abc"` | :white_check_mark: Passes | 134 | | `"ab c"` | :x: Fails | 135 | | `"ab2c"` | :white_check_mark: Passes | 136 | | `"ab-c"` | :x: Fails | 137 | 138 | ### `@AlphanumericSpace` 139 | 140 | Checks if the String contains only unicode letters, digits, empty strings or spaces. 141 | 142 | Behavior: 143 | 144 | | Value | Result | 145 | | --- | --- | 146 | | `null` | :x: Fails | 147 | | `""` | :white_check_mark: Passes | 148 | | `" "` | :white_check_mark: Passes | 149 | | `"abc"` | :white_check_mark: Passes | 150 | | `"ab c"` | :white_check_mark: Passes | 151 | | `"ab2c"` | :white_check_mark: Passes | 152 | | `"ab-c"` | :x: Fails | 153 | 154 | ### `@AlphaSpace` 155 | 156 | Checks if the String contains only Unicode letters and space (" "). 157 | 158 | Behavior: 159 | 160 | | Value | Result | 161 | | --- | --- | 162 | | `null` | :x: Fails | 163 | | `""` | :white_check_mark: Passes | 164 | | `" "` | :white_check_mark: Passes | 165 | | `"abc"` | :white_check_mark: Passes | 166 | | `"ab c"` | :white_check_mark: Passes | 167 | | `"ab1c"` | :x: Fails | 168 | | `"ab-c"` | :x: Fails | 169 | 170 | ### `@AsciiPrintable` 171 | 172 | Checks if the String is printable (ASCII printable characters). 173 | 174 | Behavior: 175 | 176 | | Value | Result | 177 | | --- | --- | 178 | | `null` | :x: Fails | 179 | | `""` | :white_check_mark: Passes | 180 | | `" "` | :white_check_mark: Passes | 181 | | `"\u0020"` | :white_check_mark: Passes | 182 | | `"\u007e"` | :x: Fails | 183 | | `"G\u00fclc\u00fc"` | :x: Fails | 184 | 185 | 186 | ### `@Before` 187 | 188 | Check if the Date is before the given date value. 189 | 190 | The annotation supports a second property format that by default is "yyyy-MM-dd'T'HH:mm:ss.SSSZ". 191 | 192 | #### Example 193 | 194 | ```java 195 | @Data 196 | class BeforeBean { 197 | @Before(value = "2018-12-01", format = "yyyy-MM-dd") 198 | private Date isBefore = new Date(1522399999911L); // Passes // Date = 2018-03-30 199 | } 200 | ``` 201 | 202 | ### `@Blank` 203 | 204 | Checks if the String is empty `""`, null or whitespace(s) `" "` only. 205 | 206 | Behavior: 207 | 208 | | Value | Result | 209 | | --- | --- | 210 | | `null` | :white_check_mark: Passes | 211 | | `""` | :white_check_mark: Passes | 212 | | `" "` | :white_check_mark: Passes | 213 | | `"abc"` | :x: Fails | 214 | | `" abc "` | :x: Fails | 215 | 216 | 217 | ### `@CC` 218 | 219 | Checks if the String is a valid credit card number. Supported types are defined in the `CreditCardType` enum: 220 | 221 | - AMEX: 222 | - DINERS; 223 | - DISCOVER; 224 | - MASTERCARD; 225 | - VISA; 226 | - VPAY; 227 | - ALL. 228 | 229 | Multiple credit card types can be supplied to the `@CC` annotation. 230 | 231 | #### Example 232 | 233 | ```java 234 | @Data 235 | class Account { 236 | @CC({ AMEX, VISA }) // AMEX or VISA 237 | private String ccNumber; 238 | } 239 | ``` 240 | 241 | Multiple credit card types can be used. 242 | 243 | ### `@EndsWith` 244 | 245 | Checks if the String `endsWith` a list of given suffix(es). If multiple suffixes are supplied, the relationship between them is `OR`(eg.: endsWith(prefix1) OR endsWith(prefix2) OR ...). 246 | 247 | The annotation supports a second property `ignoreCase` that by default is `false`. 248 | 249 | Behavior (`ignoreCase==false`): 250 | 251 | | Value | Suffix | Result | 252 | | --- | --- | --- | 253 | | `null` | "abc" | :x: Fails | 254 | | `"abcdef"` | `"def"` | :white_check_mark: Passes | 255 | | `"ABCDEF"` | `"def"` | :x: Fails | 256 | | `"ABCDEF"` | `""` | :white_check_mark: Passes | 257 | 258 | Behavior (`ignoreCase==true`) 259 | 260 | | Value | Suffix | Result | 261 | | --- | --- | --- | 262 | | `null` | "abc" | :x: Fails | 263 | | `"abcdef"` | `"def"` | :white_check_mark: Passes | 264 | | `"ABCDEF"` | `"def"` | :white_check_mark: Passes | 265 | | `"ABCDEF"` | `""` | :white_check_mark: Passes | 266 | 267 | #### Example 268 | 269 | ```java 270 | @Data 271 | @AllArgsConstructor 272 | @NoArgsConstructor 273 | class SomeStrings { 274 | private List<@EndsWith({"1", "2"}) String> someStrings; 275 | } 276 | ``` 277 | 278 | In the above example we validate all the contents of `someStrings` so that they end with either `1` or `2`. 279 | 280 | ### `@InstanceOf` 281 | 282 | Tests if an object is instance of the supplied classes. 283 | 284 | #### Examples 285 | 286 | ```java 287 | @Data class Animal {} 288 | @Data class Dog extends Animal {} 289 | @Data class Cat extends Animal {} 290 | @Data class Horse extends Animal {} 291 | 292 | @Data 293 | @AllArgsConstructor 294 | class Pets { 295 | /** This should contain only Cats and Dogs as pets, and doesn't contain null */ 296 | List<@NotNull @InstanceOf({Dog.class, Cat.class}) Animal> pets; 297 | } 298 | ``` 299 | 300 | In order to test the above code we need something like this: 301 | 302 | ```java 303 | Animal aDog = new Dog(); 304 | Animal aCat = new Cat(); 305 | Pets pets = new Pets(asList(aDog, aCat)); 306 | 307 | ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); 308 | Validator validator = factory.getValidator(); 309 | 310 | Set> validations = validator.validate(pets); 311 | 312 | // Should return 0 because the Pets class doesn't have any validation issues. 313 | System.out.println(validations.size()); 314 | ``` 315 | 316 | ### `@IPv4` 317 | 318 | Checks if the given string is a valid IPv4 address. 319 | 320 | This is implemented using `InetAddressValidator.class` from [Apache Common Validator](https://commons.apache.org/proper/commons-validator/). 321 | 322 | ### `@IPv6` 323 | 324 | Checks if the given string is a valid IPv6 address. 325 | 326 | This is implemented using `InetAddressValidator.class` from [Apache Common Validator](https://commons.apache.org/proper/commons-validator/). 327 | 328 | ### `@IsDate` 329 | 330 | Checks if the given string is formatted as a date. 331 | 332 | #### Example 333 | 334 | ```java 335 | @Data 336 | class IsDateBean { 337 | @IsDate("yyyy-MM-dd") 338 | private String isDate = "2018-12-01"; // Passes 339 | } 340 | ``` 341 | 342 | ### `@LowerCase` 343 | 344 | Checks if the String contains only lowercase letters. 345 | 346 | Behavior: 347 | 348 | | Value | Result | 349 | | --- | --- | 350 | | `null` | :x: Fails | 351 | | `""` | :x: Fails | 352 | | `" "` | :x: Fails | 353 | | `"abc"` | :white_check_mark: Passes | 354 | | `"abC"` | :x: Fails | 355 | | `"ab c"` | :x: Fails | 356 | | `"ab1c"` | :x: Fails | 357 | | `"ab-c"` | :x: Fails | 358 | 359 | ### `@MinDigits` 360 | 361 | Checks whether the annotated value is higher than or equal to the specified minimum. 362 | 363 | #### Example 364 | 365 | ```java 366 | @Data 367 | class MinDigitsDoubleBean { 368 | @MinDigits(value = "10.5") 369 | private Double isOk = new Double(11.0); // Passes 370 | 371 | @MinDigits(value = "10.5") 372 | private Double isKo = new Double(10.0); // Do not Pass 373 | } 374 | 375 | ``` 376 | 377 | #### Supported data types 378 | 379 | `Double` 380 | 381 | TODO Add support for more types 382 | 383 | 384 | ### `@MaxDigits` 385 | 386 | Checks whether the annotated value is less than or equal to the specified maximum. 387 | 388 | #### Example 389 | 390 | ```java 391 | @Data 392 | class MaxDigitsDoubleBean { 393 | @MaxDigits(value = "10.5") 394 | private Double isKo = new Double(11.0); // Do not Pass 395 | 396 | @MaxDigits(value = "10.5") 397 | private Double isOk = new Double(10.0); // Passes 398 | } 399 | 400 | ``` 401 | 402 | #### Supported data types 403 | 404 | `Double` 405 | 406 | TODO Add support for more types 407 | 408 | ### `@NotInstanceOf` 409 | 410 | Test if an object is not an instance of any of the supplied classes. 411 | 412 | #### Example 413 | 414 | ```java 415 | @Data class Animal {} 416 | @Data class Dog extends Animal {} 417 | @Data class Cat extends Animal {} 418 | @Data class Horse extends Animal {} 419 | 420 | @Data 421 | @AllArgsConstructor 422 | class Horses { 423 | /** This should contain only horses and doesn't contain NULL */ 424 | List<@NotNull @NotInstanceOf({Dog.class, Cat.class}) Animal> horses; 425 | } 426 | ``` 427 | 428 | ### `@Numeric` 429 | 430 | Checks if a String contains only Unicode digits. A decimal point is not an unicode digit and thus, the validation fails. 431 | 432 | Behavior: 433 | 434 | | Value | Result | 435 | | --- | --- | 436 | | `null` | :x: Fails | 437 | | `""` | :x: Fails | 438 | | `" "` | :x: Fails | 439 | | `"123"` | :white_check_mark: Passes | 440 | | `"\u0967\u0968\u0969"` | :white_check_mark: Passes | 441 | | `"12 3"` | :x: Fails | 442 | | `"12a3"` | :x: Fails | 443 | | `"12-3"` | :x: Fails | 444 | 445 | ### `@Parseable` 446 | 447 | Check if the String can be parsed to a number. The annotations accepts the type of parsing you want to perform as an input parameter. 448 | 449 | For example if you want to parse it `Integer`, @Parseable(TO_INT) should be used. 450 | 451 | All the possible parsing strategies accepted are described in the enum `ParseableType`. It currently supports: 452 | 453 | - `TO_SHORT` 454 | - `TO_INT` 455 | - `TO_LONG` 456 | - `TO_FLOAT` 457 | - `TO_DOUBLE` 458 | 459 | ### `@Password` 460 | 461 | Check if a `String` is a valid password - matching a set of constraints. 462 | 463 | 464 | - `containsUpperCase() default true` - Needs to contain at least an Uppercase letter; 465 | - `boolean containsLowerCase() default true` - Needs to contain at least a Lowercase letter; 466 | - `boolean containsSpecialChar() default true` - Needs to contain at least one special character; 467 | - `boolean containsDigits() default true` - Needs to contain at least one digit; 468 | - `boolean allowSpace() default false` - Password can contain spaces; 469 | - `int minSize() default 8` - The min size of the password; 470 | - `int maxSize() default 32` - The maximum size of the password; 471 | 472 | ### `@OneOfChars` 473 | 474 | Checks if the `Character` is contained in a given array (`char[]`) of values. 475 | 476 | #### Example 477 | 478 | In the following example we test if the field `aOrBOrC` is either `'a'`, `'b'` or `'c'`. 479 | 480 | ```java 481 | @Data 482 | class { 483 | @OneOfChars({'a', 'b', 'c'}) 484 | private Character aOrBOrC; 485 | } 486 | ``` 487 | 488 | ### `@OneOfDoubles` 489 | 490 | Check if the Double is contained in a given array (`double[]`) of values. 491 | 492 | #### Example 493 | 494 | In the following example we test if the field `value` is either `1.0` or `2.0`. 495 | 496 | ```java 497 | @Data 498 | class { 499 | @OneOfDoubles({1.0, 2.0}) 500 | private Double value; 501 | } 502 | ``` 503 | 504 | ### `@OneOfIntegers` 505 | 506 | Check if the Integer is contained in a given array (`int[]`) of values. 507 | 508 | #### Example 509 | 510 | In the following example we test if the field `value` is either `1` or `2`. 511 | 512 | ```java 513 | @Data 514 | class { 515 | @OneOfIntegers({1, 2}) 516 | private Integer value; 517 | } 518 | ``` 519 | 520 | ### `@OneOfLongs` 521 | 522 | Check if the Long is contained in a given array (`long[]`) of values. 523 | 524 | ### `@OneOfStrings` 525 | 526 | Checks if the String is contained in a given array (`String[]`) of values. 527 | 528 | #### Example 529 | 530 | In the following example we check if the value returned by the `getValue()` getter is either `"A"`, `"B"` or `"C"`. 531 | 532 | ```java 533 | class Test { 534 | @OneOfStrings({ "A" , "B", "C"}) 535 | private String getValue() { return /***/ } 536 | } 537 | ``` 538 | 539 | ### `@StartsWith` 540 | 541 | Checks if a String starts with the specified prefix(es). 542 | 543 | The annotation supports a second property `ignoreCase` that by default is `false`. 544 | 545 | Behavior (`ignoreCase==false`): 546 | 547 | | Value | Prefix | Result | 548 | | --- | --- | --- | 549 | | `null` | "abc" | :x: Fails | 550 | | `"abcdef"` | `"abc"` | :white_check_mark: Passes | 551 | | `"ABCDEF"` | `"abc"` | :x: Fails | 552 | | `"ABCDEF"` | `""` | :white_check_mark: Passes | 553 | 554 | Behavior (`ignoreCase==true`): 555 | 556 | | Value | Prefix | Result | 557 | | --- | --- | --- | 558 | | `null` | "abc" | :x: Fails | 559 | | `"abcdef"` | `"abc"` | :white_check_mark: Passes | 560 | | `"ABCDEF"` | `"abc"` | :white_check_mark: Passes | 561 | | `"ABCDEF"` | `""` | :white_check_mark: Passes | 562 | 563 | 564 | #### Example 565 | 566 | ```java 567 | @Data 568 | class Starters { 569 | private List< @StartsWith("1", "2") String> starts; 570 | } 571 | ``` 572 | 573 | ### `@UpperCase` 574 | 575 | Checks if the String contains only uppercase letters. 576 | 577 | Behavior: 578 | 579 | | Value | Result | 580 | | --- | --- | 581 | | `null` | :x: Fails | 582 | | `""` | :x: Fails | 583 | | `" "` | :x: Fails | 584 | | `"ABC"` | :white_check_mark: Passes | 585 | | `"aBC"` | :x: Fails | 586 | | `"A C"` | :x: Fails | 587 | | `"1AB"` | :x: Fails | 588 | | `"A-C"` | :x: Fails | 589 | 590 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | Version 0.0.10 2 | -------------- 3 | - Added a new annotation: @MinDigits : Checks whether the annotated value is higher than or equal to the specified minimum 4 | - Added a new annotation: @MaxDigits : Checks whether the annotated value is less than or equal to the specified maximum 5 | 6 | Version 0.0.10 7 | -------------- 8 | - Added a new annotation: @Password 9 | 10 | Version 0.0.8 11 | ------------- 12 | - Fixed bug that was making @CC not working; 13 | 14 | 15 | Version 0.0.7 16 | ------------- 17 | - Added @After annotation; 18 | - Added @Before annotation; -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id "com.github.johnrengelman.shadow" version "6.1.0" 3 | } 4 | 5 | apply plugin: 'java' 6 | apply plugin: 'maven' 7 | apply plugin: 'signing' 8 | 9 | group 'net.andreinc' 10 | version '0.0.12' 11 | archivesBaseName = "jbve" 12 | 13 | repositories { 14 | mavenCentral() 15 | } 16 | 17 | tasks.withType(JavaCompile) { 18 | sourceCompatibility = '1.8' 19 | targetCompatibility = '1.8' 20 | } 21 | 22 | dependencies { 23 | 24 | compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.4' 25 | annotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.4' 26 | compileOnly group: 'org.hibernate.validator', name: 'hibernate-validator', version: '6.0.18.Final' 27 | compileOnly group: 'org.glassfish', name: 'javax.el', version: '3.0.1-b10' 28 | 29 | compile group: 'org.graalvm.sdk', name: 'graal-sdk', version: '1.0.0-rc13' 30 | compile group: 'org.graalvm.js', name: 'js', version: '1.0.0-rc13' 31 | compile group: 'org.graalvm.js', name: 'js-scriptengine', version: '1.0.0-rc13' 32 | 33 | compile group: 'org.slf4j', name: 'slf4j-api', version: '1.8.0-beta2' 34 | compile group: 'commons-validator', name: 'commons-validator', version: '1.6' 35 | compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1' 36 | 37 | testCompile group: 'org.hibernate.validator', name: 'hibernate-validator', version: '6.0.18.Final' 38 | testCompile group: 'org.glassfish', name: 'javax.el', version: '3.0.1-b10' 39 | testCompile group: 'org.projectlombok', name: 'lombok', version: '1.18.4' 40 | testAnnotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.4' 41 | testCompile group: 'junit', name: 'junit', version: '4.13.1' 42 | testCompile group: 'org.slf4j', name: 'slf4j-simple', version: '1.8.0-alpha2' 43 | } 44 | 45 | task javadocJar(type: Jar) { 46 | classifier = 'javadoc' 47 | from javadoc 48 | } 49 | 50 | task sourcesJar(type: Jar) { 51 | classifier = 'sources' 52 | from sourceSets.main.allSource 53 | } 54 | 55 | artifacts { 56 | archives javadocJar, sourcesJar 57 | } 58 | 59 | signing { 60 | sign configurations.archives 61 | } 62 | 63 | uploadArchives { 64 | repositories { 65 | mavenDeployer { 66 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } 67 | 68 | repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") { 69 | authentication(userName: ossrhUsername, password: ossrhPassword) 70 | } 71 | 72 | snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") { 73 | authentication(userName: ossrhUsername, password: ossrhPassword) 74 | } 75 | 76 | pom.project { 77 | name 'Java Bean Validation Extension(s)' 78 | packaging 'jar' 79 | description "Java Bean Validation Extension(s)" 80 | url 'https://github.com/nomemory/java-bean-validation-extension' 81 | 82 | scm { 83 | connection 'https://github.com/nomemory/java-bean-validation-extension' 84 | developerConnection 'https://github.com/nomemory/java-bean-validation-extension' 85 | url 'https://github.com/nomemory/java-bean-validation-extension' 86 | } 87 | 88 | licenses { 89 | license { 90 | name 'The Apache License, Version 2.0' 91 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt' 92 | } 93 | } 94 | 95 | developers { 96 | developer { 97 | id 'nomemory' 98 | name 'nomemory' 99 | email 'gnomemory@yahoo.com' 100 | } 101 | } 102 | } 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nomemory/java-bean-validation-extension/2f0acd06ec5be258f24fac34a2013717dc74f911/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'jbvext' 2 | 3 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/date/After.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.date; 2 | 3 | import net.andreinc.jbvext.annotations.date.validator.AfterValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Repeatable; 9 | import java.lang.annotation.Retention; 10 | import java.lang.annotation.Target; 11 | 12 | import static java.lang.annotation.ElementType.*; 13 | import static java.lang.annotation.ElementType.PARAMETER; 14 | import static java.lang.annotation.ElementType.TYPE_USE; 15 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 16 | 17 | @Documented 18 | @Constraint(validatedBy = AfterValidator.class) 19 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 20 | @Retention(RUNTIME) 21 | @Repeatable(After.List.class) 22 | public @interface After { 23 | String value(); 24 | 25 | String message() default "{javax.validation.constraints.After.message}"; 26 | 27 | Class[] groups() default {}; 28 | 29 | Class[] payload() default {}; 30 | 31 | String format() default "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; 32 | 33 | /** 34 | * Defines several {@code @After} constraints on the same element. 35 | * 36 | * @see After 37 | */ 38 | @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) 39 | @Retention(RUNTIME) 40 | @Documented 41 | public @interface List { 42 | After[] value(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/date/Before.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.date; 2 | 3 | import net.andreinc.jbvext.annotations.date.validator.BeforeValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Repeatable; 9 | import java.lang.annotation.Retention; 10 | import java.lang.annotation.Target; 11 | 12 | import static java.lang.annotation.ElementType.*; 13 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 14 | 15 | @Documented 16 | @Constraint(validatedBy = BeforeValidator.class) 17 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 18 | @Retention(RUNTIME) 19 | @Repeatable(Before.List.class) 20 | public @interface Before { 21 | String value(); 22 | 23 | String message() default "{javax.validation.constraints.Before.message}"; 24 | 25 | Class[] groups() default {}; 26 | 27 | Class[] payload() default {}; 28 | 29 | String format() default "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; 30 | 31 | /** 32 | * Defines several {@code @Before} constraints on the same element. 33 | * 34 | * @see Before 35 | */ 36 | @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) 37 | @Retention(RUNTIME) 38 | @Documented 39 | public @interface List { 40 | Before[] value(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/date/IsDate.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.date; 2 | 3 | import net.andreinc.jbvext.annotations.date.validator.IsDateValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | @Documented 15 | @Constraint(validatedBy = IsDateValidator.class) 16 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 17 | @Retention(RUNTIME) 18 | public @interface IsDate { 19 | String value(); 20 | 21 | String message() default "{date.isdate}"; 22 | 23 | Class[] groups() default {}; 24 | 25 | Class[] payload() default {}; 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/date/validator/AfterValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.date.validator; 2 | 3 | 4 | import net.andreinc.jbvext.annotations.date.After; 5 | 6 | import javax.validation.ConstraintValidator; 7 | import javax.validation.ConstraintValidatorContext; 8 | import java.text.ParseException; 9 | import java.text.SimpleDateFormat; 10 | import java.util.Date; 11 | 12 | public class AfterValidator implements ConstraintValidator { 13 | 14 | private After annotation; 15 | 16 | @Override 17 | public void initialize(After constraintAnnotation) { 18 | this.annotation = constraintAnnotation; 19 | } 20 | 21 | @Override 22 | public boolean isValid(Date value, ConstraintValidatorContext context) { 23 | 24 | if (value == null) { 25 | return false; 26 | } 27 | 28 | SimpleDateFormat sdf = new SimpleDateFormat(annotation.format()); 29 | try { 30 | Date AfterDate = sdf.parse(annotation.value()); 31 | boolean isAfter = value.after(AfterDate); 32 | return isAfter; 33 | } catch (ParseException e) { 34 | return false; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/date/validator/BeforeValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.date.validator; 2 | 3 | 4 | import net.andreinc.jbvext.annotations.date.Before; 5 | import net.andreinc.jbvext.annotations.date.IsDate; 6 | 7 | import javax.validation.ConstraintValidator; 8 | import javax.validation.ConstraintValidatorContext; 9 | import java.text.ParseException; 10 | import java.text.SimpleDateFormat; 11 | import java.util.Date; 12 | 13 | import static org.apache.commons.lang3.StringUtils.isEmpty; 14 | 15 | public class BeforeValidator implements ConstraintValidator { 16 | 17 | private Before annotation; 18 | 19 | @Override 20 | public void initialize(Before constraintAnnotation) { 21 | this.annotation = constraintAnnotation; 22 | } 23 | 24 | @Override 25 | public boolean isValid(Date value, ConstraintValidatorContext context) { 26 | 27 | if (value == null) { 28 | return false; 29 | } 30 | 31 | SimpleDateFormat sdf = new SimpleDateFormat(annotation.format()); 32 | try { 33 | Date beforeDate = sdf.parse(annotation.value()); 34 | boolean isBefore = value.before(beforeDate); 35 | return isBefore; 36 | } catch (ParseException e) { 37 | return false; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/date/validator/IsDateValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.date.validator; 2 | 3 | 4 | import net.andreinc.jbvext.annotations.date.IsDate; 5 | 6 | import javax.validation.ConstraintValidator; 7 | import javax.validation.ConstraintValidatorContext; 8 | import java.text.ParseException; 9 | import java.text.SimpleDateFormat; 10 | 11 | import static org.apache.commons.lang3.StringUtils.isEmpty; 12 | 13 | public class IsDateValidator implements ConstraintValidator { 14 | 15 | private IsDate annotation; 16 | 17 | @Override 18 | public void initialize(IsDate constraintAnnotation) { 19 | this.annotation = constraintAnnotation; 20 | } 21 | 22 | @Override 23 | public boolean isValid(String value, ConstraintValidatorContext context) { 24 | 25 | if (isEmpty(value)) { 26 | return false; 27 | } 28 | 29 | SimpleDateFormat sdf = new SimpleDateFormat(annotation.value()); 30 | try { 31 | sdf.parse(value); 32 | return true; 33 | } catch (ParseException e) { 34 | return false; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/digits/MaxDigits.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.digits; 2 | 3 | import net.andreinc.jbvext.annotations.digits.validator.MaxDigitsDoubleValidator; 4 | import net.andreinc.jbvext.annotations.digits.validator.MinDigitsDoubleValidator; 5 | 6 | import javax.validation.Constraint; 7 | import javax.validation.Payload; 8 | import java.lang.annotation.Documented; 9 | import java.lang.annotation.Repeatable; 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.Target; 12 | 13 | import static java.lang.annotation.ElementType.*; 14 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 15 | 16 | @Documented 17 | @Constraint(validatedBy = MaxDigitsDoubleValidator.class) 18 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 19 | @Retention(RUNTIME) 20 | @Repeatable(MaxDigits.List.class) 21 | public @interface MaxDigits { 22 | 23 | String value(); 24 | 25 | String message() default "{javax.validation.constraints.MaxDigits.message}"; 26 | 27 | Class[] groups() default {}; 28 | 29 | Class[] payload() default {}; 30 | 31 | /** 32 | * Defines several {@code @MaxDigits} constraints on the same element. 33 | * 34 | * @see MaxDigits 35 | */ 36 | @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) 37 | @Retention(RUNTIME) 38 | @Documented 39 | public @interface List { 40 | MaxDigits[] value(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/digits/MinDigits.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.digits; 2 | 3 | import net.andreinc.jbvext.annotations.digits.validator.MinDigitsDoubleValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Repeatable; 9 | import java.lang.annotation.Retention; 10 | import java.lang.annotation.Target; 11 | 12 | import static java.lang.annotation.ElementType.*; 13 | import static java.lang.annotation.ElementType.PARAMETER; 14 | import static java.lang.annotation.ElementType.TYPE_USE; 15 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 16 | 17 | @Documented 18 | @Constraint(validatedBy = MinDigitsDoubleValidator.class) 19 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 20 | @Retention(RUNTIME) 21 | @Repeatable(MinDigits.List.class) 22 | public @interface MinDigits { 23 | 24 | String value(); 25 | 26 | String message() default "{javax.validation.constraints.MinDigits.message}"; 27 | 28 | Class[] groups() default {}; 29 | 30 | Class[] payload() default {}; 31 | 32 | /** 33 | * Defines several {@code @MinDigits} constraints on the same element. 34 | * 35 | * @see net.andreinc.jbvext.annotations.digits.MinDigits 36 | */ 37 | @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) 38 | @Retention(RUNTIME) 39 | @Documented 40 | public @interface List { 41 | MinDigits[] value(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/digits/validator/MaxDigitsDoubleValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.digits.validator; 2 | 3 | 4 | import net.andreinc.jbvext.annotations.digits.MaxDigits; 5 | import net.andreinc.jbvext.annotations.digits.MinDigits; 6 | 7 | import javax.validation.ConstraintValidator; 8 | import javax.validation.ConstraintValidatorContext; 9 | 10 | public class MaxDigitsDoubleValidator implements ConstraintValidator { 11 | 12 | private MaxDigits annotation; 13 | 14 | @Override 15 | public void initialize(MaxDigits constraintAnnotation) { 16 | this.annotation = constraintAnnotation; 17 | } 18 | 19 | @Override 20 | public boolean isValid(Double value, ConstraintValidatorContext context) { 21 | 22 | if (value == null) { 23 | return false; 24 | } 25 | 26 | Double maxDigitsValue = Double.valueOf(this.annotation.value()); 27 | 28 | int result = Double.compare(value, maxDigitsValue); 29 | 30 | if (0 == result){ 31 | // Value is equal to min 32 | return true; 33 | } else if (result < 0){ 34 | // Value is less than min 35 | return true; 36 | } else if (result > 0){ 37 | // Value is greater than min 38 | return false; 39 | } else { 40 | throw new IllegalArgumentException("How this could be possible"); 41 | } 42 | 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/digits/validator/MinDigitsDoubleValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.digits.validator; 2 | 3 | 4 | import net.andreinc.jbvext.annotations.digits.MinDigits; 5 | 6 | import javax.validation.ConstraintValidator; 7 | import javax.validation.ConstraintValidatorContext; 8 | 9 | public class MinDigitsDoubleValidator implements ConstraintValidator { 10 | 11 | private MinDigits annotation; 12 | 13 | @Override 14 | public void initialize(MinDigits constraintAnnotation) { 15 | this.annotation = constraintAnnotation; 16 | } 17 | 18 | @Override 19 | public boolean isValid(Double value, ConstraintValidatorContext context) { 20 | 21 | if (value == null) { 22 | return false; 23 | } 24 | 25 | Double minDigitsValue = Double.valueOf(this.annotation.value()); 26 | 27 | int result = Double.compare(value, minDigitsValue); 28 | 29 | if (0 == result){ 30 | // Value is equal to min 31 | return true; 32 | } else if (result < 0){ 33 | // Value is less than min 34 | return false; 35 | } else if (result > 0){ 36 | // Value is greater than min 37 | return true; 38 | } else { 39 | throw new IllegalArgumentException("How this could be possible"); 40 | } 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/misc/InstanceOf.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.misc; 2 | 3 | import net.andreinc.jbvext.annotations.misc.validators.InstanceOfValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | @Documented 15 | @Constraint(validatedBy = InstanceOfValidator.class) 16 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 17 | @Retention(RUNTIME) 18 | public @interface InstanceOf { 19 | String message() default "{misc.instanceof}"; 20 | Class[] value(); 21 | Class[] groups() default {}; 22 | Class[] payload() default {}; 23 | } 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/misc/NotInstanceOf.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.misc; 2 | 3 | import net.andreinc.jbvext.annotations.misc.validators.NotInstanceOfValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | @Documented 15 | @Constraint(validatedBy = NotInstanceOfValidator.class) 16 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 17 | @Retention(RUNTIME) 18 | public @interface NotInstanceOf { 19 | String message() default "{misc.notinstanceof}"; 20 | Class[] value(); 21 | Class[] groups() default {}; 22 | Class[] payload() default {}; 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/misc/OneOfChars.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.misc; 2 | 3 | import net.andreinc.jbvext.annotations.misc.validators.OneOfCharsValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | @Documented 15 | @Retention(RUNTIME) 16 | @Constraint(validatedBy = OneOfCharsValidator.class) 17 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 18 | public @interface OneOfChars { 19 | String message() default "{misc.oneof.chars}"; 20 | 21 | char[] value() default {}; 22 | 23 | Class[] groups() default {}; 24 | 25 | Class[] payload() default {}; 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/misc/OneOfDoubles.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.misc; 2 | 3 | import net.andreinc.jbvext.annotations.misc.validators.OneOfDoublesValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | @Documented 15 | @Retention(RUNTIME) 16 | @Constraint(validatedBy = OneOfDoublesValidator.class) 17 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 18 | public @interface OneOfDoubles { 19 | String message() default "{misc.oneof.doubles}"; 20 | 21 | double[] value() default {}; 22 | 23 | Class[] groups() default {}; 24 | 25 | Class[] payload() default {}; 26 | } -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/misc/OneOfIntegers.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.misc; 2 | 3 | import net.andreinc.jbvext.annotations.misc.validators.OneOfIntegersValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | @Documented 15 | @Retention(RUNTIME) 16 | @Constraint(validatedBy = OneOfIntegersValidator.class) 17 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 18 | public @interface OneOfIntegers { 19 | String message() default "{misc.oneof.ints}"; 20 | 21 | int[] value() default {}; 22 | 23 | Class[] groups() default {}; 24 | 25 | Class[] payload() default {}; 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/misc/OneOfLongs.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.misc; 2 | 3 | import net.andreinc.jbvext.annotations.misc.validators.OneOfLongsValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | @Documented 15 | @Retention(RUNTIME) 16 | @Constraint(validatedBy = OneOfLongsValidator.class) 17 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 18 | public @interface OneOfLongs { 19 | String message() default "{misc.oneof.longs}"; 20 | 21 | long[] value() default {}; 22 | 23 | Class[] groups() default {}; 24 | 25 | Class[] payload() default {}; 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/misc/OneOfStrings.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.misc; 2 | 3 | import net.andreinc.jbvext.annotations.misc.validators.OneOfStringsValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | @Documented 15 | @Retention(RUNTIME) 16 | @Constraint(validatedBy = OneOfStringsValidator.class) 17 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 18 | public @interface OneOfStrings { 19 | String message() default "{misc.oneof.strings}"; 20 | 21 | String[] value() default {}; 22 | 23 | Class[] groups() default {}; 24 | 25 | Class[] payload() default {}; 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/misc/validators/InstanceOfValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.misc.validators; 2 | 3 | import net.andreinc.jbvext.annotations.misc.InstanceOf; 4 | 5 | import javax.validation.ConstraintValidator; 6 | import javax.validation.ConstraintValidatorContext; 7 | 8 | public class InstanceOfValidator implements ConstraintValidator { 9 | 10 | private InstanceOf annotation; 11 | 12 | @Override 13 | public void initialize(InstanceOf constraintAnnotation) { 14 | this.annotation = constraintAnnotation; 15 | } 16 | 17 | @Override 18 | public boolean isValid(Object value, ConstraintValidatorContext context) { 19 | 20 | if (null == value) { 21 | return false; 22 | } 23 | 24 | Class[] classes = annotation.value(); 25 | 26 | for(Class cls : classes) { 27 | if (cls.isInstance(value)) { 28 | return true; 29 | } 30 | } 31 | 32 | return false; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/misc/validators/NotInstanceOfValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.misc.validators; 2 | 3 | import net.andreinc.jbvext.annotations.misc.NotInstanceOf; 4 | 5 | import javax.validation.ConstraintValidator; 6 | import javax.validation.ConstraintValidatorContext; 7 | 8 | public class NotInstanceOfValidator implements ConstraintValidator { 9 | 10 | private NotInstanceOf annotation; 11 | 12 | @Override 13 | public void initialize(NotInstanceOf constraintAnnotation) { 14 | this.annotation = constraintAnnotation; 15 | } 16 | 17 | @Override 18 | public boolean isValid(Object value, ConstraintValidatorContext context) { 19 | 20 | if (null == value) { 21 | return false; 22 | } 23 | 24 | Class[] classes = annotation.value(); 25 | 26 | for(Class cls : classes) { 27 | if (cls.isInstance(value)) { 28 | return false; 29 | } 30 | } 31 | 32 | return true; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/misc/validators/OneOfCharsValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.misc.validators; 2 | 3 | import net.andreinc.jbvext.annotations.misc.OneOfChars; 4 | import org.apache.commons.lang3.ArrayUtils; 5 | 6 | import javax.validation.ConstraintValidatorContext; 7 | import java.util.Objects; 8 | 9 | public class OneOfCharsValidator extends OneOfValidator { 10 | @Override 11 | public boolean isValid(Character value, ConstraintValidatorContext context) { 12 | return super.isValid(value, ArrayUtils.toObject(annotation.value()), Objects::equals, context); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/misc/validators/OneOfDoublesValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.misc.validators; 2 | 3 | import net.andreinc.jbvext.annotations.misc.OneOfDoubles; 4 | import org.apache.commons.lang3.ArrayUtils; 5 | 6 | import javax.validation.ConstraintValidatorContext; 7 | import java.util.Objects; 8 | 9 | public class OneOfDoublesValidator extends OneOfValidator { 10 | 11 | @Override 12 | public boolean isValid(Double value, ConstraintValidatorContext context) { 13 | return super.isValid(value, ArrayUtils.toObject(annotation.value()), Objects::equals, context); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/misc/validators/OneOfIntegersValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.misc.validators; 2 | 3 | 4 | import net.andreinc.jbvext.annotations.misc.OneOfIntegers; 5 | import org.apache.commons.lang3.ArrayUtils; 6 | 7 | import javax.validation.ConstraintValidatorContext; 8 | import java.util.Objects; 9 | 10 | public class OneOfIntegersValidator extends OneOfValidator { 11 | 12 | @Override 13 | public boolean isValid(Integer value, ConstraintValidatorContext context) { 14 | return super.isValid(value, ArrayUtils.toObject(annotation.value()), Objects::equals, context); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/misc/validators/OneOfLongsValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.misc.validators; 2 | 3 | import net.andreinc.jbvext.annotations.misc.OneOfLongs; 4 | import org.apache.commons.lang3.ArrayUtils; 5 | 6 | import javax.validation.ConstraintValidatorContext; 7 | import java.util.Objects; 8 | 9 | public class OneOfLongsValidator extends OneOfValidator { 10 | 11 | @Override 12 | public boolean isValid(Long value, ConstraintValidatorContext context) { 13 | return super.isValid(value, ArrayUtils.toObject(annotation.value()), Objects::equals, context); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/misc/validators/OneOfStringsValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.misc.validators; 2 | 3 | import net.andreinc.jbvext.annotations.misc.OneOfStrings; 4 | import org.apache.commons.lang3.StringUtils; 5 | 6 | import javax.validation.ConstraintValidatorContext; 7 | 8 | public class OneOfStringsValidator extends OneOfValidator { 9 | 10 | @Override 11 | public boolean isValid(String value, ConstraintValidatorContext context) { 12 | return super.isValid(value, annotation.value(), StringUtils::equals, context); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/misc/validators/OneOfValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.misc.validators; 2 | 3 | import javax.validation.ConstraintValidator; 4 | import javax.validation.ConstraintValidatorContext; 5 | import java.lang.annotation.Annotation; 6 | import java.util.function.BiFunction; 7 | 8 | public abstract class OneOfValidator implements ConstraintValidator { 9 | 10 | protected AType annotation; 11 | 12 | @Override 13 | public void initialize(AType constraintAnnotation) { 14 | this.annotation = constraintAnnotation; 15 | } 16 | 17 | public boolean isValid(VType value, VType[] possibleValues, BiFunction equalsMethod, ConstraintValidatorContext context) { 18 | for (VType possibleVal : possibleValues) { 19 | if (equalsMethod.apply(value, possibleVal)) { 20 | return true; 21 | } 22 | } 23 | return false; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/Alpha.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str; 2 | 3 | import net.andreinc.jbvext.annotations.str.validators.AlphaValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | /** 15 | * Checks if the String contains only unicode letters. 16 | */ 17 | @Documented 18 | @Constraint(validatedBy = AlphaValidator.class) 19 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 20 | @Retention(RUNTIME) 21 | public @interface Alpha { 22 | String message() default "{str.alpha}"; 23 | Class[] groups() default {}; 24 | Class[] payload() default {}; 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/AlphaSpace.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str; 2 | 3 | import net.andreinc.jbvext.annotations.str.validators.AlphaSpaceValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | /** 15 | * Checks if the String contains only unicode letters and space (' ') 16 | */ 17 | @Documented 18 | @Constraint(validatedBy = AlphaSpaceValidator.class) 19 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 20 | @Retention(RUNTIME) 21 | public @interface AlphaSpace { 22 | String message() default "{string.alphaspace}"; 23 | Class[] groups() default {}; 24 | Class[] payload() default {}; 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/Alphanumeric.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str; 2 | 3 | import net.andreinc.jbvext.annotations.str.validators.AlphanumericValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | /** 15 | * Checks if the String contains only unicode letters or digits. 16 | */ 17 | @Documented 18 | @Constraint(validatedBy = AlphanumericValidator.class) 19 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 20 | @Retention(RUNTIME) 21 | public @interface Alphanumeric { 22 | String message() default "{str.alphanumeric}"; 23 | Class[] groups() default {}; 24 | Class[] payload() default {}; 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/AlphanumericSpace.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str; 2 | 3 | 4 | import net.andreinc.jbvext.annotations.str.validators.AlphanumericSpaceValidator; 5 | 6 | import javax.validation.Constraint; 7 | import javax.validation.Payload; 8 | import java.lang.annotation.Documented; 9 | import java.lang.annotation.Retention; 10 | import java.lang.annotation.Target; 11 | 12 | import static java.lang.annotation.ElementType.*; 13 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 14 | 15 | /** 16 | * Checks if the String contains only unicode letters, digits or space (' '). 17 | * Compared to the @Alphanumeric annotation, empty string is also accepted. 18 | */ 19 | @Documented 20 | @Constraint(validatedBy = AlphanumericSpaceValidator.class) 21 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 22 | @Retention(RUNTIME) 23 | public @interface AlphanumericSpace { 24 | String message() default "{str.alphanumericspace}"; 25 | Class[] groups() default {}; 26 | Class[] payload() default {}; 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/AsciiPrintable.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str; 2 | 3 | import net.andreinc.jbvext.annotations.str.validators.AsciiPrintableValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | /** 15 | * Checks if the string contains only ASCII printable characters. 16 | */ 17 | @Documented 18 | @Constraint(validatedBy = AsciiPrintableValidator.class) 19 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 20 | @Retention(RUNTIME) 21 | public @interface AsciiPrintable { 22 | String message() default "{str.asciiprintable}"; 23 | Class[] groups() default {}; 24 | Class[] payload() default {}; 25 | } -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/Blank.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str; 2 | 3 | import net.andreinc.jbvext.annotations.str.validators.BlankValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | /** 15 | * Checks if a String is whitespace, empty ("") or null. 16 | */ 17 | @Documented 18 | @Constraint(validatedBy = BlankValidator.class) 19 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 20 | @Retention(RUNTIME) 21 | public @interface Blank { 22 | String message() default "{str.blank}"; 23 | Class[] groups() default {}; 24 | Class[] payload() default {}; 25 | } -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/CC.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str; 2 | 3 | import net.andreinc.jbvext.annotations.str.validators.CCValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | /** 15 | * Checks if the string can be a valid credit card number. 16 | */ 17 | @Documented 18 | @Constraint(validatedBy = CCValidator.class) 19 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 20 | @Retention(RUNTIME) 21 | public @interface CC { 22 | String message() default "{str.creditcard}"; 23 | CreditCardType[] value() default {CreditCardType.ALL}; 24 | Class[] groups() default {}; 25 | Class[] payload() default {}; 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/CreditCardType.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str; 2 | 3 | import org.apache.commons.validator.routines.CreditCardValidator; 4 | 5 | ; 6 | 7 | public enum CreditCardType { 8 | 9 | AMEX(CreditCardValidator.AMEX), 10 | DINERS(CreditCardValidator.DINERS), 11 | DISCOVER(CreditCardValidator.DISCOVER), 12 | MASTERCARD(CreditCardValidator.MASTERCARD), 13 | VISA(CreditCardValidator.VISA), 14 | VPAY(CreditCardValidator.VPAY), 15 | ALL( 16 | CreditCardValidator.AMEX + 17 | CreditCardValidator.DINERS + 18 | CreditCardValidator.DISCOVER + 19 | CreditCardValidator.MASTERCARD + 20 | CreditCardValidator.VISA + 21 | CreditCardValidator.VPAY 22 | ); 23 | 24 | private Long internalValue; 25 | 26 | CreditCardType(Long internalValue) { 27 | this.internalValue = internalValue; 28 | } 29 | 30 | public Long getInternalValue() { 31 | return internalValue; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/EndsWith.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str; 2 | 3 | import net.andreinc.jbvext.annotations.str.validators.EndsWithValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | @Documented 15 | @Constraint(validatedBy = EndsWithValidator.class) 16 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 17 | @Retention(RUNTIME) 18 | public @interface EndsWith { 19 | String message() default "{string.endswith}"; 20 | String[] value(); 21 | boolean ignoreCase() default false; 22 | Class[] groups() default {}; 23 | Class[] payload() default {}; 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/IPv4.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str; 2 | 3 | import net.andreinc.jbvext.annotations.str.validators.IPv4Validator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | /** 15 | * Checks if the string is a valid IPv4 address. 16 | */ 17 | @Documented 18 | @Constraint(validatedBy = IPv4Validator.class) 19 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 20 | @Retention(RUNTIME) 21 | public @interface IPv4 { 22 | String message() default "{string.ipv4}"; 23 | Class[] groups() default {}; 24 | Class[] payload() default {}; 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/IPv6.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str; 2 | 3 | import net.andreinc.jbvext.annotations.str.validators.IPv6Validator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | @Documented 15 | @Constraint(validatedBy = IPv6Validator.class) 16 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 17 | @Retention(RUNTIME) 18 | public @interface IPv6 { 19 | String message() default "{string.ipv6}"; 20 | Class[] groups() default {}; 21 | Class[] payload() default {}; 22 | } -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/LowerCase.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str; 2 | 3 | import net.andreinc.jbvext.annotations.str.validators.LowerCaseValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | /** 15 | * Checks if the String contains only lowercase characters. 16 | */ 17 | @Documented 18 | @Constraint(validatedBy = LowerCaseValidator.class) 19 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 20 | @Retention(RUNTIME) 21 | public @interface LowerCase { 22 | String message() default "{string.lowercase}"; 23 | Class[] groups() default {}; 24 | Class[] payload() default {}; 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/Numeric.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str; 2 | 3 | import net.andreinc.jbvext.annotations.str.validators.NumericValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | /** 15 | * Checks if the string is numeric 16 | */ 17 | @Documented 18 | @Constraint(validatedBy = NumericValidator.class) 19 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 20 | @Retention(RUNTIME) 21 | public @interface Numeric { 22 | String message() default "{string.numeric}"; 23 | Class[] groups() default {}; 24 | Class[] payload() default {}; 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/Parseable.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str; 2 | 3 | import net.andreinc.jbvext.annotations.str.validators.ParseableValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | @Documented 15 | @Constraint(validatedBy = ParseableValidator.class) 16 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 17 | @Retention(RUNTIME) 18 | public @interface Parseable { 19 | ParseableType value(); 20 | 21 | String message() default "{string.parseable}"; 22 | 23 | Class[] groups() default {}; 24 | 25 | Class[] payload() default {}; 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/ParseableType.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Getter; 5 | 6 | @AllArgsConstructor 7 | public enum ParseableType { 8 | TO_SHORT("Short"), 9 | TO_INT("Integer"), 10 | TO_LONG("Long"), 11 | TO_DOUBLE("Double"), 12 | TO_FLOAT("Float"); 13 | @Getter 14 | private String friendlyName; 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/Password.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str; 2 | 3 | import net.andreinc.jbvext.annotations.str.validators.PasswordValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | @Documented 15 | @Constraint(validatedBy = PasswordValidator.class) 16 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 17 | @Retention(RUNTIME) 18 | public @interface Password { 19 | 20 | boolean containsUpperCase() default true; 21 | 22 | boolean containsLowerCase() default true; 23 | 24 | boolean containsSpecialChar() default true; 25 | 26 | boolean containsDigits() default true; 27 | 28 | boolean allowSpace() default false; 29 | 30 | int minSize() default 8; 31 | 32 | int maxSize() default 32; 33 | 34 | String message() default "{string.password}"; 35 | 36 | Class[] groups() default {}; 37 | 38 | Class[] payload() default {}; 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/StartsWith.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str; 2 | 3 | import net.andreinc.jbvext.annotations.str.validators.StartsWithValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | @Documented 15 | @Constraint(validatedBy = StartsWithValidator.class) 16 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 17 | @Retention(RUNTIME) 18 | public @interface StartsWith { 19 | String message() default "{string.startswith}"; 20 | 21 | String[] value(); 22 | boolean ignoreCase() default false; 23 | Class[] groups() default {}; 24 | Class[] payload() default {}; 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/UpperCase.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str; 2 | 3 | import net.andreinc.jbvext.annotations.str.validators.UpperCaseValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | /** 15 | * Checks if the String contains only uppercase characters. 16 | */ 17 | @Documented 18 | @Constraint(validatedBy = UpperCaseValidator.class) 19 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 20 | @Retention(RUNTIME) 21 | public @interface UpperCase { 22 | String message() default "{string.uppercase}"; 23 | Class[] groups() default {}; 24 | Class[] payload() default {}; 25 | } -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/validators/AlphaSpaceValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str.validators; 2 | 3 | import net.andreinc.jbvext.annotations.str.AlphaSpace; 4 | import org.apache.commons.lang3.StringUtils; 5 | 6 | import java.util.function.Function; 7 | 8 | public class AlphaSpaceValidator extends GenericStringValidator { 9 | @Override 10 | public Function condition() { 11 | return StringUtils::isAlphaSpace; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/validators/AlphaValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str.validators; 2 | 3 | import net.andreinc.jbvext.annotations.str.Alpha; 4 | import org.apache.commons.lang3.StringUtils; 5 | 6 | import java.util.function.Function; 7 | 8 | public class AlphaValidator extends GenericStringValidator { 9 | @Override 10 | public Function condition() { 11 | return StringUtils::isAlpha; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/validators/AlphanumericSpaceValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str.validators; 2 | 3 | import net.andreinc.jbvext.annotations.str.AlphanumericSpace; 4 | import org.apache.commons.lang3.StringUtils; 5 | 6 | import java.util.function.Function; 7 | 8 | public class AlphanumericSpaceValidator extends GenericStringValidator { 9 | @Override 10 | public Function condition() { 11 | return StringUtils::isAlphanumericSpace; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/validators/AlphanumericValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str.validators; 2 | 3 | import net.andreinc.jbvext.annotations.str.Alphanumeric; 4 | import org.apache.commons.lang3.StringUtils; 5 | 6 | import java.util.function.Function; 7 | 8 | public class AlphanumericValidator extends GenericStringValidator { 9 | @Override 10 | public Function condition() { 11 | return StringUtils::isAlphanumeric; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/validators/AsciiPrintableValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str.validators; 2 | 3 | import net.andreinc.jbvext.annotations.str.AsciiPrintable; 4 | import org.apache.commons.lang3.StringUtils; 5 | 6 | import java.util.function.Function; 7 | 8 | public class AsciiPrintableValidator extends GenericStringValidator { 9 | @Override 10 | public Function condition() { 11 | return StringUtils::isAsciiPrintable; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/validators/BlankValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str.validators; 2 | 3 | import net.andreinc.jbvext.annotations.str.Blank; 4 | import org.apache.commons.lang3.StringUtils; 5 | 6 | import java.util.function.Function; 7 | 8 | public class BlankValidator extends GenericStringValidator { 9 | @Override 10 | public Function condition() { 11 | return StringUtils::isBlank; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/validators/CCValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str.validators; 2 | 3 | import net.andreinc.jbvext.annotations.str.CC; 4 | import net.andreinc.jbvext.annotations.str.CreditCardType; 5 | import org.apache.commons.validator.routines.CreditCardValidator; 6 | 7 | import javax.validation.ConstraintValidator; 8 | import javax.validation.ConstraintValidatorContext; 9 | 10 | public class CCValidator implements ConstraintValidator { 11 | 12 | private CC annotation; 13 | 14 | @Override 15 | public void initialize(CC constraintAnnotation) { 16 | this.annotation = constraintAnnotation; 17 | } 18 | 19 | @Override 20 | public boolean isValid(String value, ConstraintValidatorContext context) { 21 | 22 | if (null == value) { 23 | return false; 24 | } 25 | 26 | CreditCardType[] types = annotation.value(); 27 | 28 | for(CreditCardType creditCardType : types) { 29 | CreditCardValidator ccv = 30 | new CreditCardValidator(creditCardType.getInternalValue()); 31 | if (ccv.isValid(value)) { 32 | return true; 33 | } 34 | } 35 | 36 | return false; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/validators/EndsWithValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str.validators; 2 | 3 | import net.andreinc.jbvext.annotations.str.EndsWith; 4 | import org.apache.commons.lang3.StringUtils; 5 | 6 | import javax.validation.ConstraintValidator; 7 | import javax.validation.ConstraintValidatorContext; 8 | import java.util.function.BiFunction; 9 | 10 | public class EndsWithValidator implements ConstraintValidator { 11 | 12 | protected EndsWith annotation; 13 | 14 | @Override 15 | public void initialize(EndsWith constraintAnnotation) { 16 | this.annotation = constraintAnnotation; 17 | } 18 | 19 | @Override 20 | public boolean isValid(String value, ConstraintValidatorContext context) { 21 | String[] prefixes = annotation.value(); 22 | 23 | BiFunction fct = 24 | !annotation.ignoreCase() ? 25 | StringUtils::endsWith : 26 | StringUtils::endsWithIgnoreCase; 27 | 28 | for(String p : prefixes) { 29 | if (fct.apply(value, p)) { 30 | return true; 31 | } 32 | } 33 | 34 | return false; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/validators/GenericStringValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str.validators; 2 | 3 | import javax.validation.ConstraintValidator; 4 | import javax.validation.ConstraintValidatorContext; 5 | import java.lang.annotation.Annotation; 6 | import java.util.function.Function; 7 | 8 | public abstract class GenericStringValidator implements ConstraintValidator { 9 | 10 | protected T annotation; 11 | 12 | @Override 13 | public void initialize(T constraintAnnotation) { 14 | this.annotation = constraintAnnotation; 15 | } 16 | 17 | @Override 18 | public boolean isValid(String value, ConstraintValidatorContext context) { 19 | return condition().apply(value); 20 | } 21 | 22 | public abstract Function condition(); 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/validators/IPv4Validator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str.validators; 2 | 3 | import net.andreinc.jbvext.annotations.str.IPv4; 4 | import org.apache.commons.validator.routines.InetAddressValidator; 5 | 6 | import javax.validation.ConstraintValidator; 7 | import javax.validation.ConstraintValidatorContext; 8 | 9 | public class IPv4Validator implements ConstraintValidator { 10 | 11 | @Override 12 | public boolean isValid(String value, ConstraintValidatorContext context) { 13 | if (value == null) { 14 | return false; 15 | } 16 | return InetAddressValidator 17 | .getInstance() 18 | .isValidInet4Address(value); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/validators/IPv6Validator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str.validators; 2 | 3 | import net.andreinc.jbvext.annotations.str.IPv6; 4 | import org.apache.commons.validator.routines.InetAddressValidator; 5 | 6 | import javax.validation.ConstraintValidator; 7 | import javax.validation.ConstraintValidatorContext; 8 | 9 | public class IPv6Validator implements ConstraintValidator { 10 | 11 | @Override 12 | public boolean isValid(String value, ConstraintValidatorContext context) { 13 | if (value == null) { 14 | return false; 15 | } 16 | return InetAddressValidator 17 | .getInstance() 18 | .isValidInet6Address(value); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/validators/LowerCaseValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str.validators; 2 | 3 | import net.andreinc.jbvext.annotations.str.LowerCase; 4 | import org.apache.commons.lang3.StringUtils; 5 | 6 | import java.util.function.Function; 7 | 8 | public class LowerCaseValidator extends GenericStringValidator { 9 | @Override 10 | public Function condition() { 11 | return StringUtils::isAllLowerCase; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/validators/NumericValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str.validators; 2 | 3 | import net.andreinc.jbvext.annotations.str.Numeric; 4 | import org.apache.commons.lang3.StringUtils; 5 | 6 | import java.util.function.Function; 7 | 8 | public class NumericValidator extends GenericStringValidator { 9 | @Override 10 | public Function condition() { 11 | return StringUtils::isNumeric; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/validators/ParseableValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str.validators; 2 | 3 | import net.andreinc.jbvext.annotations.str.Parseable; 4 | import net.andreinc.jbvext.annotations.str.ParseableType; 5 | 6 | import javax.validation.ConstraintValidator; 7 | import javax.validation.ConstraintValidatorContext; 8 | import java.util.function.Function; 9 | 10 | public class ParseableValidator implements ConstraintValidator { 11 | 12 | private Parseable annotation; 13 | 14 | @Override 15 | public void initialize(Parseable constraintAnnotation) { 16 | this.annotation = constraintAnnotation; 17 | } 18 | 19 | @Override 20 | public boolean isValid(String value, ConstraintValidatorContext context) { 21 | 22 | if (null == value) { 23 | return false; 24 | } 25 | 26 | ParseableType type = annotation.value(); 27 | 28 | switch (type) { 29 | case TO_INT: 30 | return catcher(value, Integer::parseInt); 31 | case TO_DOUBLE: 32 | return catcher(value, Double::parseDouble); 33 | case TO_LONG: 34 | return catcher(value, Long::parseLong); 35 | case TO_SHORT: 36 | return catcher(value, Short::parseShort); 37 | case TO_FLOAT: 38 | return catcher(value, Float::parseFloat); 39 | } 40 | 41 | return false; 42 | } 43 | 44 | private boolean catcher(String value, Function function) { 45 | try { 46 | function.apply(value); 47 | return true; 48 | } catch (Exception e) { 49 | return false; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/validators/PasswordValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str.validators; 2 | 3 | import net.andreinc.jbvext.annotations.str.Password; 4 | 5 | import javax.validation.ConstraintValidator; 6 | import javax.validation.ConstraintValidatorContext; 7 | import java.util.Set; 8 | 9 | import static java.lang.Character.*; 10 | import static java.util.stream.Collectors.toSet; 11 | 12 | public class PasswordValidator implements ConstraintValidator { 13 | 14 | private static final Set SPECIAL_CHARS = "\"/*!@#$%^&*()\\\"{}_[]|\\\\?/<>,.\"" 15 | .chars() 16 | .mapToObj(i -> (char) i) 17 | .collect(toSet()); 18 | 19 | private Password annotation; 20 | 21 | @Override 22 | public void initialize(Password constraintAnnotation) { 23 | this.annotation = constraintAnnotation; 24 | } 25 | 26 | @Override 27 | public boolean isValid(String value, ConstraintValidatorContext context) { 28 | 29 | if (null == value) { 30 | return false; 31 | } 32 | 33 | if (value.length() < annotation.minSize() || value.length() >= annotation.maxSize()) { 34 | return false; 35 | } 36 | 37 | int upperCase = 0; 38 | int lowerCase = 0; 39 | int specialChar = 0; 40 | int digits = 0; 41 | 42 | for (int i = 0; i < value.length(); ++i) { 43 | char chr = value.charAt(i); 44 | 45 | if (isUpperCase(chr)) upperCase++; 46 | 47 | else if (isLowerCase(chr)) lowerCase++; 48 | 49 | else if (SPECIAL_CHARS.contains(chr)) specialChar++; 50 | 51 | else if (isDigit(chr)) digits++; 52 | 53 | else if (chr == ' ' && annotation.allowSpace()) return false; 54 | } 55 | 56 | if (annotation.containsDigits() && digits <= 0) return false; 57 | if (annotation.containsLowerCase() && lowerCase <= 0) return false; 58 | if (annotation.containsUpperCase() && upperCase <= 0) return false; 59 | if (annotation.containsSpecialChar() && specialChar <= 0) return false; 60 | 61 | return true; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/validators/StartsWithValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str.validators; 2 | 3 | import net.andreinc.jbvext.annotations.str.StartsWith; 4 | import org.apache.commons.lang3.StringUtils; 5 | 6 | import javax.validation.ConstraintValidator; 7 | import javax.validation.ConstraintValidatorContext; 8 | import java.util.function.BiFunction; 9 | 10 | public class StartsWithValidator implements ConstraintValidator { 11 | 12 | protected StartsWith annotation; 13 | 14 | @Override 15 | public void initialize(StartsWith constraintAnnotation) { 16 | this.annotation = constraintAnnotation; 17 | } 18 | 19 | @Override 20 | public boolean isValid(String value, ConstraintValidatorContext context) { 21 | String[] prefixes = annotation.value(); 22 | 23 | BiFunction fct = 24 | !annotation.ignoreCase() ? 25 | StringUtils::startsWith : 26 | StringUtils::startsWithIgnoreCase; 27 | 28 | for(String p : prefixes) { 29 | if (fct.apply(value, p)) { 30 | return true; 31 | } 32 | } 33 | 34 | return false; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/annotations/str/validators/UpperCaseValidator.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.annotations.str.validators; 2 | 3 | import net.andreinc.jbvext.annotations.str.UpperCase; 4 | import org.apache.commons.lang3.StringUtils; 5 | 6 | import java.util.function.Function; 7 | 8 | public class UpperCaseValidator extends GenericStringValidator { 9 | 10 | @Override 11 | public Function condition() { 12 | return StringUtils::isAllUpperCase; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/utils/BeanValidationException.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.utils; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | 6 | public class BeanValidationException extends RuntimeException { 7 | 8 | public BeanValidationException(String message) { 9 | super(message); 10 | this.message = message; 11 | } 12 | 13 | public BeanValidationException(String message, Throwable cause) { 14 | super(message, cause); 15 | } 16 | 17 | @Getter 18 | @Setter 19 | private String message; 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/utils/SimpleValidation.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.utils; 2 | 3 | import javax.validation.ConstraintViolation; 4 | import javax.validation.Validation; 5 | import javax.validation.Validator; 6 | import javax.validation.ValidatorFactory; 7 | import java.util.Set; 8 | 9 | public class SimpleValidation { 10 | 11 | public static void validate(T object) { 12 | 13 | ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); 14 | Validator validator = factory.getValidator(); 15 | Set> validatorSet = validator.validate(object); 16 | 17 | validatorSet.forEach(cv -> { 18 | throw new BeanValidationException(cv.getPropertyPath() + " - " + cv.getMessage()); 19 | }); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/utils/TimeAction.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.utils; 2 | 3 | import java.util.function.Function; 4 | import java.util.function.Supplier; 5 | 6 | public class TimeAction { 7 | 8 | public static TimeActionResponse recordTimeAndDo(Function function, Input val) { 9 | long t1 = System.currentTimeMillis(); 10 | Ret result = function.apply(val); 11 | long t2 = System.currentTimeMillis() - t1; 12 | return new TimeActionResponse<>(result, t2); 13 | } 14 | 15 | public static TimeActionResponse recordTimeAndDo(Supplier supplier) { 16 | long t1 =System.currentTimeMillis(); 17 | Ret result = supplier.get(); 18 | long t2 = System.currentTimeMillis() - t1; 19 | return new TimeActionResponse<>(result, t2); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/net/andreinc/jbvext/utils/TimeActionResponse.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.utils; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | @Data 8 | @AllArgsConstructor 9 | @NoArgsConstructor 10 | public class TimeActionResponse { 11 | private T response; 12 | private long time; 13 | } 14 | -------------------------------------------------------------------------------- /src/main/resources/ValidationMessages.properties: -------------------------------------------------------------------------------- 1 | str.allblank=Validated value '${validatedValue}' doesn't meet the conditions. The string needs to be empty (""), null or whitespace only. 2 | str.alpha=Validated value '${validatedValue}' doesn't meet the conditions. The string needs to contain only unicode letters. 3 | str.alphanumeric=Validated value '${validatedValue}' doesn't meet the conditions. The string needs to contain only unicode letters or digits. 4 | str.alphanumericspace=Validated value '${validatedValue}' doesn't meet the conditions. The string needs to contain only unicode letters, digits or spaces (' '). 5 | string.alphaspace=Validated value '${validatedValue}' doesn't meet the conditions. The string needs to contain only unicode letters or spaces (' '). 6 | str.asciiprintable=Validated value '${validatedValue}' doesn't meet the conditions. The string needs to contain only printable ascii characters. 7 | str.blank=Validated value '${validatedValue}' doesn't meet the conditions. The string needs to contain only spaces (' '), empty ('') or null characters. 8 | str.creditcard=Validated value '${validatedValue}' doesn't meet the conditions. The string needs to represent a valid credit card number of type(s): '{value}'. 9 | string.ipv4=Validated value '${validatedValue}' doesn't meet the required conditions. The string needs to represent a valid IPv4 address. 10 | string.ipv6=Validated value '${validatedValue}' doesn't meet the required conditions. The string needs to represent a valid IPv6 address. 11 | string.lowercase=Validated value '${validatedValue}' doesn't meet the required conditions. The string needs to contain only lower case letters. 12 | string.numeric=Validated value '${validatedValue}' doesn't meet the required conditions. The string needs to ne a number. 13 | string.startswith=Validated value '${validatedValue}' doesn't meet the required conditions. The string needs to start with the give prefix(es): '{value}'. 14 | string.endswith=Validated value '${validatedValue}' doesn't meet the required conditions. The string needs to end with the give suffix(es): '{value}'. 15 | string.uppercase=Validated value '${validatedValue}' doesn't meet the required conditions. The string needs to contain only upper case letters. 16 | string.parseable=Validated value '${validatedValue == null ? "" : validatedValue}' cannot be parsed to '${value.getFriendlyName()}'. 17 | string.password=Validate value '${validatedValue}' doesn't meet the password security policies. 18 | misc.instanceof=Validated value '${validatedValue}' is an instance of the required classes: '{value}'. 19 | misc.notinstanceof=Validated value '${validatedValue}' is not an instance of the excluded classes: '{value}'. 20 | misc.oneof.chars=Validated value '${validatedValue}' cannot be found in the list: '{value}'. 21 | misc.oneof.doubles=Validated value '${validatedValue}' cannot be found in the list: '{value}'. 22 | misc.oneof.ints=Validated value '${validatedValue}' cannot be found in the list: '{value}'. 23 | misc.oneof.longs=Validated value '${validatedValue}' cannot be found in the list: '{value}'. 24 | misc.oneof.strings=Validated value '${validatedValue}' cannot be found in the list: '{value}'. 25 | exp.js=Validated value '${validatedValue}' doesn't match the condition imposed by the Java Script expression: '{value}'. 26 | date.isdate=Validated value '${validatedValue}' doesn't match the format '{value}'. -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/AfterBeforeTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.Data; 4 | import net.andreinc.jbvext.annotations.date.After; 5 | import net.andreinc.jbvext.annotations.date.Before; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import javax.validation.ConstraintViolation; 10 | import javax.validation.Validation; 11 | import javax.validation.Validator; 12 | import java.util.Date; 13 | import java.util.Set; 14 | 15 | public class AfterBeforeTest { 16 | 17 | private Validator validator = Validation 18 | .buildDefaultValidatorFactory() 19 | .getValidator(); 20 | 21 | @Test 22 | public void testFieldOk() { 23 | AfterBeforeBean afterBeforeBean = new AfterBeforeBean(); 24 | 25 | Set> violations = 26 | validator.validate(afterBeforeBean); 27 | 28 | Assert.assertEquals(0, violations.size()); 29 | } 30 | } 31 | 32 | @Data 33 | class AfterBeforeBean { 34 | @After(value = "2018-01-01", format = "yyyy-MM-dd") 35 | @Before(value = "3000-01-01", format = "yyyy-MM-dd") 36 | private Date isAfter = new Date(1522399999911L); // Passes // Date = 2018-03-30 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/AfterTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.Data; 4 | import net.andreinc.jbvext.annotations.date.After; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import javax.validation.ConstraintViolation; 9 | import javax.validation.Validation; 10 | import javax.validation.Validator; 11 | import java.util.Arrays; 12 | import java.util.Date; 13 | import java.util.List; 14 | import java.util.Set; 15 | 16 | public class AfterTest { 17 | 18 | private Validator validator = Validation 19 | .buildDefaultValidatorFactory() 20 | .getValidator(); 21 | 22 | @Test 23 | public void testFieldOk() { 24 | AfterBean AfterBean = new AfterBean(); 25 | 26 | Set> violations = 27 | validator.validate(AfterBean); 28 | 29 | Assert.assertEquals(0, violations.size()); 30 | } 31 | 32 | 33 | @Test 34 | public void testFieldKo() { 35 | NotAfterBean notAfterBean = new NotAfterBean(); 36 | 37 | Set> violations = 38 | validator.validate(notAfterBean); 39 | 40 | Assert.assertEquals(1, violations.size()); 41 | } 42 | 43 | @Test 44 | public void testMethods() { 45 | AfterBeanMethods AfterBeanMethods = new AfterBeanMethods(); 46 | 47 | Set> violations = 48 | validator.validate(AfterBeanMethods); 49 | 50 | Assert.assertEquals(1, violations.size()); 51 | } 52 | 53 | @Test 54 | public void testType() { 55 | AfterBeanType AfterBeanType = new AfterBeanType(); 56 | 57 | Set> violations = 58 | validator.validate(AfterBeanType); 59 | 60 | Assert.assertEquals(1, violations.size()); 61 | } 62 | } 63 | 64 | @Data 65 | class AfterBean { 66 | @After(value = "2018-01-01", format = "yyyy-MM-dd") 67 | private Date isAfter = new Date(1522399999911L); // Passes // Date = 2018-03-30 68 | } 69 | 70 | 71 | @Data 72 | class NotAfterBean { 73 | @After(value = "2018-12-01", format = "yyyy-MM-dd") 74 | private Date isNotAfter = new Date(1522399999911L); // Doesn't Passes Date = 2018-03-30 75 | } 76 | 77 | 78 | @Data 79 | class AfterBeanMethods { 80 | 81 | @After(value = "2018-12-01", format = "yyyy-MM-dd") 82 | private Date getAfter(){ 83 | return new Date(1522399999911L); // Passes 84 | } 85 | 86 | @After(value = "2017-12-01", format = "yyyy-MM-dd") 87 | private Date getNotAfter(){ 88 | return new Date(1522399999911L); // Doesn't Passes 89 | } 90 | } 91 | 92 | @Data 93 | class AfterBeanType { 94 | 95 | private List<@After(value = "2018-01-01", format = "yyyy-MM-dd") Date> list = Arrays.asList(new Date(1512082800000L), new Date(1522399999911L)); 96 | 97 | } -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/AlphaSpaceTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.Data; 4 | import net.andreinc.jbvext.annotations.str.AlphaSpace; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import javax.validation.ConstraintViolation; 9 | import javax.validation.Validation; 10 | import javax.validation.Validator; 11 | import java.util.Arrays; 12 | import java.util.List; 13 | import java.util.Set; 14 | 15 | public class AlphaSpaceTest { 16 | 17 | private Validator validator = 18 | Validation.buildDefaultValidatorFactory().getValidator(); 19 | 20 | @Test 21 | public void testFields() { 22 | AlphaSpaceBeanFields alphaSpaceBeanFields = new AlphaSpaceBeanFields(); 23 | 24 | Set> violations = 25 | validator.validate(alphaSpaceBeanFields); 26 | 27 | Assert.assertEquals(2, violations.size()); 28 | } 29 | 30 | @Test 31 | public void testMethods() { 32 | AlphaSpaceBeanMethods alphaSpaceBeanMethods = new AlphaSpaceBeanMethods(); 33 | 34 | Set> violations = 35 | validator.validate(alphaSpaceBeanMethods); 36 | 37 | Assert.assertEquals(2, violations.size()); 38 | } 39 | 40 | @Test 41 | public void testType() { 42 | AlphaSpaceList alphaSpaceList = new AlphaSpaceList(); 43 | 44 | Set> violations = 45 | validator.validate(alphaSpaceList); 46 | 47 | Assert.assertEquals(2, violations.size()); 48 | } 49 | } 50 | 51 | @Data 52 | class AlphaSpaceBeanFields { 53 | 54 | @AlphaSpace 55 | private String alphaSpace = "abc ABC"; 56 | 57 | @AlphaSpace 58 | private String alphaSpaceEmpty = ""; 59 | 60 | @AlphaSpace 61 | private String alphaSpaceNull = null; 62 | 63 | @AlphaSpace 64 | private String alphaSpaceWrong = "abc 123"; 65 | } 66 | 67 | @Data 68 | class AlphaSpaceBeanMethods { 69 | 70 | @AlphaSpace 71 | private String getAlphaSpace() { 72 | return "abc ABC"; 73 | } 74 | 75 | @AlphaSpace 76 | private String getAlphaSpaceEmpty() { 77 | return ""; 78 | } 79 | 80 | @AlphaSpace 81 | private String getAlphaSpaceNull() { 82 | return null; 83 | } 84 | 85 | @AlphaSpace 86 | private String getAlphaSpaceWrong() { 87 | return "abc 123"; 88 | } 89 | } 90 | 91 | @Data 92 | class AlphaSpaceList { 93 | private List<@AlphaSpace String> strings = Arrays.asList("abc ABC", "", null, "abc 123"); 94 | } 95 | -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/AlphaTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.Data; 4 | import lombok.NoArgsConstructor; 5 | import net.andreinc.jbvext.annotations.str.Alpha; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import javax.validation.ConstraintViolation; 10 | import javax.validation.Validation; 11 | import javax.validation.Validator; 12 | import java.util.Arrays; 13 | import java.util.List; 14 | import java.util.Set; 15 | 16 | public class AlphaTest { 17 | 18 | private Validator validator = 19 | Validation.buildDefaultValidatorFactory().getValidator(); 20 | 21 | @Test 22 | public void testField() { 23 | 24 | AlphaBeanField alphaBeanField = new AlphaBeanField(); 25 | 26 | Set> violations = 27 | validator.validate(alphaBeanField); 28 | 29 | Assert.assertEquals(3, violations.size()); 30 | } 31 | 32 | @Test 33 | public void testTypeParam() { 34 | AlphaBeanType alphaBeanType = new AlphaBeanType(); 35 | 36 | Set> violations = 37 | validator.validate(alphaBeanType); 38 | 39 | Assert.assertEquals(3, violations.size()); 40 | } 41 | 42 | @Test 43 | public void testGetters() { 44 | AlphaBeanGetters alphaBeanGetters = new AlphaBeanGetters(); 45 | 46 | Set> violations = 47 | validator.validate(alphaBeanGetters); 48 | 49 | Assert.assertEquals(3, violations.size()); 50 | } 51 | } 52 | 53 | @Data 54 | class AlphaBeanGetters { 55 | @Alpha 56 | private final String getAlphaGood() { 57 | return "abcAAbb"; 58 | } 59 | 60 | @Alpha 61 | private final String getAlphaNull() { 62 | return null; 63 | } 64 | 65 | @Alpha 66 | private final String getAlphaBadNumbers() { 67 | return "abc111"; 68 | } 69 | 70 | @Alpha 71 | private final String getAlphaEmpty() { 72 | return ""; 73 | } 74 | } 75 | 76 | @Data 77 | class AlphaBeanField { 78 | @Alpha 79 | private final String alphaGood = "abcAAbb"; 80 | 81 | @Alpha 82 | private final String alphaNull = null; 83 | 84 | @Alpha 85 | private final String alphaBadNumbers = "abc111"; 86 | 87 | @Alpha 88 | private final String alphaEmpty = ""; 89 | } 90 | 91 | @NoArgsConstructor 92 | class AlphaBeanType { 93 | private List<@Alpha String> list = Arrays.asList("abcAAbb", null, "abc111", ""); 94 | } -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/AlphanumericSpaceTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.Data; 4 | import net.andreinc.jbvext.annotations.str.AlphanumericSpace; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import javax.validation.ConstraintViolation; 9 | import javax.validation.Validation; 10 | import javax.validation.Validator; 11 | import java.util.Arrays; 12 | import java.util.List; 13 | import java.util.Set; 14 | 15 | public class AlphanumericSpaceTest { 16 | 17 | private Validator validator = 18 | Validation.buildDefaultValidatorFactory().getValidator(); 19 | 20 | @Test 21 | public void testMethods() { 22 | AlphanumericSpaceBeanGetters alphanumericSpaceBeanGetters = new AlphanumericSpaceBeanGetters(); 23 | 24 | Set> violations = 25 | validator.validate(alphanumericSpaceBeanGetters); 26 | 27 | Assert.assertEquals(2, violations.size()); 28 | 29 | } 30 | 31 | @Test 32 | public void testFields() { 33 | AlphanumericSpaceFields alphanumericSpaceFields = new AlphanumericSpaceFields(); 34 | 35 | Set> violations = 36 | validator.validate(alphanumericSpaceFields); 37 | 38 | Assert.assertEquals(2, violations.size()); 39 | 40 | } 41 | 42 | @Test 43 | public void testType() { 44 | AlphanumericSpaceType alphanumericSpaceType = new AlphanumericSpaceType(); 45 | 46 | Set> violations = 47 | validator.validate(alphanumericSpaceType); 48 | 49 | Assert.assertEquals(2, violations.size()); 50 | } 51 | } 52 | 53 | @Data 54 | class AlphanumericSpaceFields { 55 | 56 | @AlphanumericSpace 57 | private String alphanumericSpace = " aA12 21 zz"; 58 | 59 | @AlphanumericSpace 60 | private String alphanumericSpaceEmpty = ""; 61 | 62 | @AlphanumericSpace 63 | private String alphanumericSpaceNull = null; 64 | 65 | @AlphanumericSpace 66 | private String alphanumericSpaceWrong = "; adsds 11 ZZZ AA"; 67 | } 68 | 69 | @Data 70 | class AlphanumericSpaceBeanGetters { 71 | 72 | @AlphanumericSpace 73 | public String getAlphanumericSpace() { 74 | return " aA12 21 zz"; 75 | } 76 | 77 | @AlphanumericSpace 78 | public String getAlphanumericSpaceEmpty() { 79 | return ""; 80 | } 81 | 82 | @AlphanumericSpace 83 | public String getAlphanumericSpaceNull() { 84 | return null; 85 | } 86 | 87 | @AlphanumericSpace 88 | public String getAlphanumericSpaceWrong() { 89 | return "; adsds 11 ZZZ AA"; 90 | } 91 | } 92 | 93 | @Data 94 | class AlphanumericSpaceType { 95 | private List<@AlphanumericSpace String> list = Arrays.asList(" aA12 21 zz", "", null, "; adsds 11 ZZZ AA"); 96 | } 97 | -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/AlphanumericTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.Data; 4 | import net.andreinc.jbvext.annotations.str.Alphanumeric; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import javax.validation.ConstraintViolation; 9 | import javax.validation.Validation; 10 | import javax.validation.Validator; 11 | import java.util.List; 12 | import java.util.Set; 13 | 14 | import static java.util.Arrays.asList; 15 | 16 | public class AlphanumericTest { 17 | private Validator validator = 18 | Validation.buildDefaultValidatorFactory().getValidator(); 19 | 20 | @Test 21 | public void testFields() { 22 | AlphanumericBeanField alphanumericBeanField = new AlphanumericBeanField(); 23 | 24 | Set> violations = 25 | validator.validate(alphanumericBeanField); 26 | 27 | Assert.assertEquals(3, violations.size()); 28 | } 29 | 30 | @Test 31 | public void testTypeParam() { 32 | AlphanumericList alphanumericList = new AlphanumericList(); 33 | 34 | Set> violations = 35 | validator.validate(alphanumericList); 36 | 37 | Assert.assertEquals(3, violations.size()); 38 | } 39 | 40 | @Test 41 | public void testMethods() { 42 | AlphanumericBeanMethods alphanumericBeanMethods = new AlphanumericBeanMethods(); 43 | 44 | Set> violations = 45 | validator.validate(alphanumericBeanMethods); 46 | 47 | Assert.assertEquals(3, violations.size()); 48 | } 49 | } 50 | 51 | @Data 52 | class AlphanumericBeanField { 53 | 54 | @Alphanumeric 55 | private String alphanumeric = "123AAAaaa111"; 56 | 57 | @Alphanumeric 58 | private String alphanumericNull = null; 59 | 60 | @Alphanumeric 61 | private String alphanumericEmpty = ""; 62 | 63 | @Alphanumeric 64 | private String alphanumericWrong = " aaa11AA2"; 65 | } 66 | 67 | @Data 68 | class AlphanumericBeanMethods { 69 | 70 | @Alphanumeric 71 | private String getAlphanumeric() { 72 | return "123AAAaaa111"; 73 | } 74 | 75 | @Alphanumeric 76 | private String getAlphanumericNull() { 77 | return null; 78 | } 79 | 80 | @Alphanumeric 81 | private String getAlphanumericEmpty() { 82 | return ""; 83 | } 84 | 85 | @Alphanumeric 86 | private String getAlphanumericWrong() { 87 | return " aaa11AA2"; 88 | } 89 | } 90 | 91 | @Data 92 | class AlphanumericList { 93 | private List<@Alphanumeric String> strings = asList("123AAAaaa111", null, "", " aaa11AA2"); 94 | } -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/AsciiPrintableTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.Data; 4 | import net.andreinc.jbvext.annotations.str.AsciiPrintable; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import javax.validation.ConstraintViolation; 9 | import javax.validation.Validation; 10 | import javax.validation.Validator; 11 | import java.util.Arrays; 12 | import java.util.List; 13 | import java.util.Set; 14 | 15 | import static net.andreinc.jbvext.test.AsciiPrintableTest.EOF; 16 | 17 | public class AsciiPrintableTest { 18 | public static final String EOF = "\u0003"; 19 | 20 | private Validator validator = 21 | Validation.buildDefaultValidatorFactory().getValidator(); 22 | 23 | @Test 24 | public void testFields() { 25 | AsciiPrintableBeanFields asciiPrintableBeanFields = new AsciiPrintableBeanFields(); 26 | 27 | Set> violations = 28 | validator.validate(asciiPrintableBeanFields); 29 | 30 | Assert.assertEquals(1, violations.size()); 31 | } 32 | 33 | @Test 34 | public void testMethods() { 35 | AsciiPrintableBeanMethods asciiPrintableBeanMethods = new AsciiPrintableBeanMethods(); 36 | 37 | Set> violations = 38 | validator.validate(asciiPrintableBeanMethods); 39 | 40 | Assert.assertEquals(1, violations.size()); 41 | } 42 | 43 | public void testType() { 44 | AsciiPrintableType asciiPrintableType = new AsciiPrintableType(); 45 | 46 | Set> violations = 47 | validator.validate(asciiPrintableType); 48 | 49 | Assert.assertEquals(1, violations.size()); 50 | } 51 | } 52 | 53 | @Data 54 | class AsciiPrintableBeanFields { 55 | 56 | @AsciiPrintable 57 | private String printable = "abc"; 58 | 59 | @AsciiPrintable 60 | private String nonPrintable = "abc" + EOF; 61 | } 62 | 63 | @Data 64 | class AsciiPrintableBeanMethods { 65 | 66 | @AsciiPrintable 67 | private String getPrintable() { 68 | return "abc"; 69 | } 70 | 71 | ; 72 | 73 | @AsciiPrintable 74 | private String getNonPrintable() { 75 | return "abc" + EOF; 76 | } 77 | } 78 | 79 | @Data 80 | class AsciiPrintableType { 81 | private List<@AsciiPrintable String> list = Arrays.asList("abc", EOF); 82 | } 83 | 84 | -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/BeforeTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.Data; 4 | import net.andreinc.jbvext.annotations.date.Before; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import javax.validation.ConstraintViolation; 9 | import javax.validation.Validation; 10 | import javax.validation.Validator; 11 | import java.util.Arrays; 12 | import java.util.Date; 13 | import java.util.List; 14 | import java.util.Set; 15 | 16 | public class BeforeTest { 17 | 18 | private Validator validator = Validation 19 | .buildDefaultValidatorFactory() 20 | .getValidator(); 21 | 22 | @Test 23 | public void testFieldOk() { 24 | BeforeBean beforeBean = new BeforeBean(); 25 | 26 | Set> violations = 27 | validator.validate(beforeBean); 28 | 29 | Assert.assertEquals(0, violations.size()); 30 | } 31 | 32 | 33 | @Test 34 | public void testFieldKo() { 35 | NotBeforeBean notBeforeBean = new NotBeforeBean(); 36 | 37 | Set> violations = 38 | validator.validate(notBeforeBean); 39 | 40 | Assert.assertEquals(1, violations.size()); 41 | } 42 | 43 | @Test 44 | public void testMethods() { 45 | BeforeBeanMethods beforeBeanMethods = new BeforeBeanMethods(); 46 | 47 | Set> violations = 48 | validator.validate(beforeBeanMethods); 49 | 50 | Assert.assertEquals(1, violations.size()); 51 | } 52 | 53 | @Test 54 | public void testType() { 55 | BeforeBeanType beforeBeanType = new BeforeBeanType(); 56 | 57 | Set> violations = 58 | validator.validate(beforeBeanType); 59 | 60 | Assert.assertEquals(1, violations.size()); 61 | } 62 | } 63 | 64 | @Data 65 | class BeforeBean { 66 | @Before(value = "2018-12-01", format = "yyyy-MM-dd") 67 | private Date isBefore = new Date(1522399999911L); // Passes // Date = 2018-03-30 68 | } 69 | 70 | 71 | @Data 72 | class NotBeforeBean { 73 | @Before(value = "2017-12-01", format = "yyyy-MM-dd") 74 | private Date isNotBefore = new Date(1522399999911L); // Doesn't Passes Date = 2018-03-30 75 | } 76 | 77 | 78 | @Data 79 | class BeforeBeanMethods { 80 | 81 | @Before(value = "2018-12-01", format = "yyyy-MM-dd") 82 | private Date getBefore(){ 83 | return new Date(1522399999911L); // Passes 84 | } 85 | 86 | @Before(value = "2017-12-01", format = "yyyy-MM-dd") 87 | private Date getNotBefore(){ 88 | return new Date(1522399999911L); // Doesn't Passes 89 | } 90 | } 91 | 92 | @Data 93 | class BeforeBeanType { 94 | 95 | private List<@Before(value = "2018-01-01", format = "yyyy-MM-dd") Date> list = Arrays.asList(new Date(1512082800000L), new Date(1522399999911L)); 96 | 97 | } -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/BlankTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.Data; 4 | import net.andreinc.jbvext.annotations.str.Blank; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import javax.validation.ConstraintViolation; 9 | import javax.validation.Validation; 10 | import javax.validation.Validator; 11 | import java.util.Arrays; 12 | import java.util.List; 13 | import java.util.Set; 14 | 15 | public class BlankTest { 16 | 17 | private Validator validator = 18 | Validation.buildDefaultValidatorFactory().getValidator(); 19 | 20 | 21 | @Test 22 | public void testFields() { 23 | BlankBeanFields blankBeanFields = new BlankBeanFields(); 24 | 25 | Set> violations = 26 | validator.validate(blankBeanFields); 27 | 28 | Assert.assertEquals(1, violations.size()); 29 | } 30 | 31 | @Test 32 | public void testMethods() { 33 | BlankBeanMethods blankBeanMethods = new BlankBeanMethods(); 34 | 35 | Set> violations = 36 | validator.validate(blankBeanMethods); 37 | 38 | Assert.assertEquals(1, violations.size()); 39 | } 40 | 41 | @Test 42 | public void testType() { 43 | BlankBeanType blankBeanType = new BlankBeanType(); 44 | 45 | Set> violations = 46 | validator.validate(blankBeanType); 47 | 48 | Assert.assertEquals(1, violations.size()); 49 | } 50 | } 51 | 52 | @Data 53 | class BlankBeanFields { 54 | 55 | @Blank 56 | private String strBlank = ""; 57 | 58 | @Blank 59 | private String strSpaces = " "; 60 | 61 | @Blank 62 | private String strNull = null; 63 | 64 | @Blank 65 | private String strNotBlank = "abc"; 66 | } 67 | 68 | @Data 69 | class BlankBeanMethods { 70 | 71 | @Blank 72 | private String getStrBlank() { 73 | return ""; 74 | } 75 | 76 | @Blank 77 | private String getStrSpaces() { 78 | return " "; 79 | } 80 | 81 | @Blank 82 | private String getStrNull() { 83 | return null; 84 | } 85 | 86 | @Blank 87 | private String getStrNotBlank() { 88 | return "abc"; 89 | } 90 | } 91 | 92 | @Data 93 | class BlankBeanType { 94 | 95 | private List<@Blank String> list = Arrays.asList("", " ", null, "abc"); 96 | 97 | } -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/CCTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.Data; 4 | import net.andreinc.jbvext.annotations.str.CC; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import javax.validation.ConstraintViolation; 9 | import javax.validation.Validation; 10 | import javax.validation.Validator; 11 | import java.util.Set; 12 | 13 | import static net.andreinc.jbvext.annotations.str.CreditCardType.AMEX; 14 | import static net.andreinc.jbvext.annotations.str.CreditCardType.VISA; 15 | 16 | public class CCTest { 17 | 18 | public static final String AMEX_CC = "340000000000009"; 19 | public static final String VISA_CC = "4111111111111111"; 20 | 21 | private Validator validator = 22 | Validation.buildDefaultValidatorFactory().getValidator(); 23 | 24 | @Test 25 | public void testFields() { 26 | CCBeanFields blankBeanFields = new CCBeanFields(); 27 | 28 | Set> violations = 29 | validator.validate(blankBeanFields); 30 | 31 | Assert.assertEquals(0, violations.size()); 32 | } 33 | } 34 | 35 | 36 | @Data 37 | class CCBeanFields { 38 | @CC(AMEX) 39 | private String amex = CCTest.AMEX_CC; 40 | 41 | @CC({AMEX, VISA}) 42 | private String amexOrVisa = CCTest.AMEX_CC; 43 | 44 | @CC({VISA}) 45 | private String visa = CCTest.VISA_CC; 46 | } -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/EndsWithTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.Data; 4 | import net.andreinc.jbvext.annotations.str.EndsWith; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import javax.validation.ConstraintViolation; 9 | import javax.validation.Validation; 10 | import javax.validation.Validator; 11 | import java.util.Arrays; 12 | import java.util.List; 13 | import java.util.Set; 14 | 15 | public class EndsWithTest { 16 | 17 | private Validator validator = 18 | Validation.buildDefaultValidatorFactory().getValidator(); 19 | 20 | @Test 21 | public void testFields() { 22 | EndsWithBeanFields endsWithBeanFields = new EndsWithBeanFields(); 23 | 24 | Set> violations = 25 | validator.validate(endsWithBeanFields); 26 | 27 | Assert.assertEquals(1, violations.size()); 28 | } 29 | 30 | @Test 31 | public void testMethods() { 32 | EndsWithBeanMethods endsWithBeanMethods = new EndsWithBeanMethods(); 33 | 34 | Set> violations = 35 | validator.validate(endsWithBeanMethods); 36 | 37 | Assert.assertEquals(1, violations.size()); 38 | } 39 | 40 | @Test 41 | public void testType() { 42 | EndsWithBeanType endsWithBeanType = new EndsWithBeanType(); 43 | 44 | Set> violations = 45 | validator.validate(endsWithBeanType); 46 | 47 | Assert.assertEquals(0, violations.size()); 48 | } 49 | } 50 | 51 | 52 | @Data 53 | class EndsWithBeanFields { 54 | 55 | @EndsWith("abc") 56 | private String correct = "abc"; 57 | 58 | @EndsWith("abc") 59 | private String incorrect = "abcd"; 60 | } 61 | 62 | @Data 63 | class EndsWithBeanMethods { 64 | 65 | @EndsWith("abc") 66 | private String getCorrect() { 67 | return "abc"; 68 | } 69 | 70 | @EndsWith("abc") 71 | private String getIncorrect() { 72 | return "abcd"; 73 | } 74 | } 75 | 76 | @Data 77 | class EndsWithBeanType { 78 | 79 | private List<@EndsWith("abc") String> getStrs = Arrays.asList("abc", "1abc", "0abc"); 80 | } -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/IPv4Test.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | 4 | import lombok.Data; 5 | import net.andreinc.jbvext.annotations.str.IPv4; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import javax.validation.ConstraintViolation; 10 | import javax.validation.Validation; 11 | import javax.validation.Validator; 12 | import java.util.Arrays; 13 | import java.util.List; 14 | import java.util.Set; 15 | 16 | public class IPv4Test { 17 | 18 | private Validator validator = 19 | Validation.buildDefaultValidatorFactory().getValidator(); 20 | 21 | @Test 22 | public void testFields() { 23 | IPv4BeanFields iPv4BeanFields = new IPv4BeanFields(); 24 | 25 | Set> violations = 26 | validator.validate(iPv4BeanFields); 27 | 28 | Assert.assertEquals(1, violations.size()); 29 | } 30 | 31 | @Test 32 | public void testMethods() { 33 | IPv4BeanMethods iPv4BeanMethods = new IPv4BeanMethods(); 34 | 35 | Set> violations = 36 | validator.validate(iPv4BeanMethods); 37 | 38 | Assert.assertEquals(1, violations.size()); 39 | } 40 | 41 | @Test 42 | public void testType() { 43 | IPv4BeanType iPv4BeanType = new IPv4BeanType(); 44 | 45 | Set> violations = 46 | validator.validate(iPv4BeanType); 47 | 48 | Assert.assertEquals(0, violations.size()); 49 | } 50 | } 51 | 52 | @Data 53 | class IPv4BeanFields { 54 | 55 | @IPv4 56 | private String correct = "127.0.0.1"; 57 | 58 | @IPv4 59 | private String incorrect = "abc"; 60 | } 61 | 62 | @Data 63 | class IPv4BeanMethods { 64 | 65 | @IPv4 66 | private String getCorrect() { 67 | return "127.0.0.1"; 68 | } 69 | 70 | @IPv4 71 | private String getIncorrect() { 72 | return "abc"; 73 | } 74 | } 75 | 76 | @Data 77 | class IPv4BeanType { 78 | private List<@IPv4 String> ips = Arrays.asList("127.0.0.1"); 79 | } -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/IPv6Test.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.Data; 4 | import net.andreinc.jbvext.annotations.str.IPv6; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import javax.validation.ConstraintViolation; 9 | import javax.validation.Validation; 10 | import javax.validation.Validator; 11 | import java.util.Arrays; 12 | import java.util.List; 13 | import java.util.Set; 14 | 15 | public class IPv6Test { 16 | 17 | private Validator validator = 18 | Validation.buildDefaultValidatorFactory().getValidator(); 19 | 20 | @Test 21 | public void testFields() { 22 | IPv6BeanFields IPv6BeanFields = new IPv6BeanFields(); 23 | 24 | Set> violations = 25 | validator.validate(IPv6BeanFields); 26 | 27 | Assert.assertEquals(1, violations.size()); 28 | } 29 | 30 | @Test 31 | public void testMethods() { 32 | IPv6BeanMethods IPv6BeanMethods = new IPv6BeanMethods(); 33 | 34 | Set> violations = 35 | validator.validate(IPv6BeanMethods); 36 | 37 | Assert.assertEquals(1, violations.size()); 38 | } 39 | 40 | @Test 41 | public void testType() { 42 | IPv6BeanType IPv6BeanType = new IPv6BeanType(); 43 | 44 | Set> violations = 45 | validator.validate(IPv6BeanType); 46 | 47 | Assert.assertEquals(0, violations.size()); 48 | } 49 | } 50 | 51 | @Data 52 | class IPv6BeanFields { 53 | 54 | @IPv6 55 | private String correct = "::1"; 56 | 57 | @IPv6 58 | private String incorrect = "abc"; 59 | } 60 | 61 | @Data 62 | class IPv6BeanMethods { 63 | 64 | @IPv6 65 | private String getCorrect() { 66 | return "::1"; 67 | } 68 | 69 | @IPv6 70 | private String getIncorrect() { 71 | return "abc"; 72 | } 73 | } 74 | 75 | @Data 76 | class IPv6BeanType { 77 | private List<@IPv6 String> ips = Arrays.asList("::1"); 78 | } -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/InstanceOfTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.Data; 4 | import lombok.EqualsAndHashCode; 5 | import net.andreinc.jbvext.annotations.misc.InstanceOf; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import javax.validation.ConstraintViolation; 10 | import javax.validation.Validator; 11 | import java.util.Arrays; 12 | import java.util.List; 13 | import java.util.Set; 14 | 15 | import static javax.validation.Validation.buildDefaultValidatorFactory; 16 | 17 | @Data 18 | @EqualsAndHashCode(callSuper = false) 19 | class IofEveAndAdam { 20 | } 21 | 22 | @Data 23 | @EqualsAndHashCode(callSuper = false) 24 | class IofCain extends IofEveAndAdam { 25 | } 26 | 27 | @Data 28 | @EqualsAndHashCode(callSuper = false) 29 | class IofAbel extends IofEveAndAdam { 30 | } 31 | 32 | @Data 33 | @EqualsAndHashCode(callSuper = false) 34 | class IofSeth extends IofEveAndAdam { 35 | } 36 | 37 | public class InstanceOfTest { 38 | 39 | private Validator validator = 40 | buildDefaultValidatorFactory().getValidator(); 41 | 42 | @Test 43 | public void testFields() { 44 | InstanceOfBeanFields instanceOfBeanFields = new InstanceOfBeanFields(); 45 | 46 | Set> violations = 47 | validator.validate(instanceOfBeanFields); 48 | 49 | Assert.assertEquals(1, violations.size()); 50 | } 51 | 52 | @Test 53 | public void testMethods() { 54 | InstanceOfBeanMethods instanceOfBeanMethods = new InstanceOfBeanMethods(); 55 | 56 | Set> violations = 57 | validator.validate(instanceOfBeanMethods); 58 | 59 | Assert.assertEquals(1, violations.size()); 60 | } 61 | 62 | @Test 63 | public void testType() { 64 | InstanceOfBeanType instanceOfBeanType = new InstanceOfBeanType(); 65 | 66 | Set> violations = 67 | validator.validate(instanceOfBeanType); 68 | 69 | Assert.assertEquals(0, violations.size()); 70 | } 71 | } 72 | 73 | 74 | @Data 75 | class InstanceOfBeanFields { 76 | 77 | @InstanceOf({IofCain.class, IofAbel.class}) 78 | private IofEveAndAdam correct = new IofCain(); 79 | 80 | @InstanceOf({IofSeth.class}) 81 | private IofEveAndAdam incorrect = new IofAbel(); 82 | 83 | } 84 | 85 | @Data 86 | class InstanceOfBeanMethods { 87 | 88 | @InstanceOf({IofCain.class, IofAbel.class}) 89 | private IofEveAndAdam getCorrect() { 90 | return new IofCain(); 91 | } 92 | 93 | @InstanceOf({IofSeth.class}) 94 | private IofEveAndAdam getIncorrect() { 95 | return new IofAbel(); 96 | } 97 | 98 | } 99 | 100 | @Data 101 | class InstanceOfBeanType { 102 | private List<@InstanceOf({IofCain.class, IofAbel.class, IofSeth.class}) IofEveAndAdam> list = 103 | Arrays.asList(new IofCain(), new IofAbel(), new IofSeth()); 104 | } -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/IsDateTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.Data; 4 | import net.andreinc.jbvext.annotations.date.IsDate; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import javax.validation.ConstraintViolation; 9 | import javax.validation.Validation; 10 | import javax.validation.Validator; 11 | import java.util.Set; 12 | 13 | public class IsDateTest { 14 | 15 | private Validator validator = Validation 16 | .buildDefaultValidatorFactory() 17 | .getValidator(); 18 | 19 | @Test 20 | public void testField() { 21 | IsDateBean isDateBean = new IsDateBean(); 22 | 23 | Set> violations = 24 | validator.validate(isDateBean); 25 | 26 | Assert.assertEquals(0, violations.size()); 27 | } 28 | } 29 | 30 | @Data 31 | class IsDateBean { 32 | @IsDate("yyyy-MM-dd") 33 | private String isDate = "2018-12-01"; // Passes 34 | } -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/LowerCaseTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.Data; 4 | import net.andreinc.jbvext.annotations.str.LowerCase; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import javax.validation.ConstraintViolation; 9 | import javax.validation.Validation; 10 | import javax.validation.Validator; 11 | import java.util.Arrays; 12 | import java.util.List; 13 | import java.util.Set; 14 | 15 | public class LowerCaseTest { 16 | private Validator validator = 17 | Validation.buildDefaultValidatorFactory().getValidator(); 18 | 19 | @Test 20 | public void testFields() { 21 | LowerCaseBeanFields lowerCaseBeanFields = new LowerCaseBeanFields(); 22 | 23 | Set> violations = 24 | validator.validate(lowerCaseBeanFields); 25 | 26 | Assert.assertEquals(1, violations.size()); 27 | } 28 | 29 | @Test 30 | public void testMethods() { 31 | LowerCaseBeanMethods lowerCaseBeanMethods = new LowerCaseBeanMethods(); 32 | 33 | Set> violations = 34 | validator.validate(lowerCaseBeanMethods); 35 | 36 | Assert.assertEquals(1, violations.size()); 37 | } 38 | 39 | @Test 40 | public void testType() { 41 | LowerCaseBeanType lowerCaseBeanType = new LowerCaseBeanType(); 42 | 43 | Set> violations = 44 | validator.validate(lowerCaseBeanType); 45 | 46 | Assert.assertEquals(0, violations.size()); 47 | } 48 | } 49 | 50 | @Data 51 | class LowerCaseBeanFields { 52 | 53 | @LowerCase 54 | private String correct = "abc"; 55 | 56 | @LowerCase 57 | private String incorrect = "Abc"; 58 | } 59 | 60 | @Data 61 | class LowerCaseBeanMethods { 62 | 63 | @LowerCase 64 | private String getCorrect() { 65 | return "abc"; 66 | } 67 | 68 | @LowerCase 69 | private String getIncorrect() { 70 | return "Abc"; 71 | } 72 | } 73 | 74 | @Data 75 | class LowerCaseBeanType { 76 | private List<@LowerCase String> sts = Arrays.asList("abc", "axxx"); 77 | } 78 | 79 | -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/MaxDigitsDoubleValidatorTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.Data; 4 | import net.andreinc.jbvext.annotations.digits.MaxDigits; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import javax.validation.ConstraintViolation; 9 | import javax.validation.Validation; 10 | import javax.validation.Validator; 11 | import java.util.Arrays; 12 | import java.util.List; 13 | import java.util.Set; 14 | 15 | public class MaxDigitsDoubleValidatorTest { 16 | 17 | private Validator validator = Validation 18 | .buildDefaultValidatorFactory() 19 | .getValidator(); 20 | 21 | @Test 22 | public void testFields() { 23 | MaxDigitsDoubleBean MaxDigitsDoubleBean = new MaxDigitsDoubleBean(); 24 | 25 | Set> violations = 26 | validator.validate(MaxDigitsDoubleBean); 27 | 28 | Assert.assertEquals(1, violations.size()); 29 | } 30 | 31 | @Test 32 | public void testMethods() { 33 | MaxDigitsDoubleBeanMethods MaxDigitsDoubleBean = new MaxDigitsDoubleBeanMethods(); 34 | 35 | Set> violations = 36 | validator.validate(MaxDigitsDoubleBean); 37 | 38 | Assert.assertEquals(1, violations.size()); 39 | } 40 | 41 | @Test 42 | public void testType() { 43 | MaxDigitsDoubleBeanType MaxDigitsDoubleBean = new MaxDigitsDoubleBeanType(); 44 | 45 | Set> violations = 46 | validator.validate(MaxDigitsDoubleBean); 47 | 48 | Assert.assertEquals(1, violations.size()); 49 | } 50 | } 51 | 52 | @Data 53 | class MaxDigitsDoubleBean { 54 | @MaxDigits(value = "10.5") 55 | private Double isKo = new Double(11.0); // Do not Pass 56 | 57 | @MaxDigits(value = "10.5") 58 | private Double isOk = new Double(10.0); // Passes 59 | } 60 | 61 | 62 | @Data 63 | class MaxDigitsDoubleBeanMethods { 64 | 65 | @MaxDigits(value = "10.5") 66 | private Double getOk(){ 67 | return new Double(10.0); // Passes 68 | } 69 | 70 | @MaxDigits(value = "10.5") 71 | private Double getKo(){ 72 | return new Double(11.0); // Do not Pass 73 | } 74 | 75 | 76 | } 77 | 78 | @Data 79 | class MaxDigitsDoubleBeanType { 80 | 81 | private List<@MaxDigits(value = "10.5") Double> list = Arrays.asList( 82 | new Double(11.0), 83 | new Double(10.5), 84 | new Double(10.0) 85 | ); 86 | 87 | } 88 | -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/MinDigitsDoubleValidatorTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.Data; 4 | import net.andreinc.jbvext.annotations.digits.MinDigits; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import javax.validation.ConstraintViolation; 9 | import javax.validation.Validation; 10 | import javax.validation.Validator; 11 | import java.util.Arrays; 12 | import java.util.List; 13 | import java.util.Set; 14 | 15 | public class MinDigitsDoubleValidatorTest { 16 | 17 | private Validator validator = Validation 18 | .buildDefaultValidatorFactory() 19 | .getValidator(); 20 | 21 | @Test 22 | public void testFields() { 23 | MinDigitsDoubleBean minDigitsDoubleBean = new MinDigitsDoubleBean(); 24 | 25 | Set> violations = 26 | validator.validate(minDigitsDoubleBean); 27 | 28 | Assert.assertEquals(1, violations.size()); 29 | } 30 | 31 | @Test 32 | public void testMethods() { 33 | MinDigitsDoubleBeanMethods minDigitsDoubleBean = new MinDigitsDoubleBeanMethods(); 34 | 35 | Set> violations = 36 | validator.validate(minDigitsDoubleBean); 37 | 38 | Assert.assertEquals(1, violations.size()); 39 | } 40 | 41 | @Test 42 | public void testType() { 43 | MinDigitsDoubleBeanType minDigitsDoubleBean = new MinDigitsDoubleBeanType(); 44 | 45 | Set> violations = 46 | validator.validate(minDigitsDoubleBean); 47 | 48 | Assert.assertEquals(1, violations.size()); 49 | } 50 | } 51 | 52 | @Data 53 | class MinDigitsDoubleBean { 54 | @MinDigits(value = "10.5") 55 | private Double isOk = new Double(11.0); // Passes 56 | 57 | @MinDigits(value = "10.5") 58 | private Double isKo = new Double(10.0); // Do not Pass 59 | } 60 | 61 | 62 | @Data 63 | class MinDigitsDoubleBeanMethods { 64 | 65 | @MinDigits(value = "10.5") 66 | private Double getOk(){ 67 | return new Double(11.0); // Passes 68 | } 69 | 70 | @MinDigits(value = "10.5") 71 | private Double getKo(){ 72 | return new Double(10.0); // Do not Pass 73 | } 74 | } 75 | 76 | @Data 77 | class MinDigitsDoubleBeanType { 78 | 79 | private List<@MinDigits(value = "10.5") Double> list = Arrays.asList( 80 | new Double(11.0), 81 | new Double(10.5), 82 | new Double(10.0) 83 | ); 84 | 85 | } 86 | -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/MinDigitsMaxDigitsTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.Data; 4 | import net.andreinc.jbvext.annotations.digits.MaxDigits; 5 | import net.andreinc.jbvext.annotations.digits.MinDigits; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import javax.validation.ConstraintViolation; 10 | import javax.validation.Validation; 11 | import javax.validation.Validator; 12 | import java.util.Set; 13 | 14 | public class MinDigitsMaxDigitsTest { 15 | 16 | private Validator validator = Validation 17 | .buildDefaultValidatorFactory() 18 | .getValidator(); 19 | 20 | @Test 21 | public void testFields() { 22 | MinDigitsMaxDigitsDoubleBean minDigitsMaxDigitsDoubleBean = new MinDigitsMaxDigitsDoubleBean(); 23 | 24 | 25 | Set> violations = 26 | validator.validate(minDigitsMaxDigitsDoubleBean); 27 | 28 | Assert.assertEquals(2, violations.size()); 29 | } 30 | 31 | @Data 32 | class MinDigitsMaxDigitsDoubleBean { 33 | @MinDigits(value = "10.5") 34 | @MaxDigits(value = "11.5") 35 | private Double isOk = new Double(11.0); // Passes 36 | 37 | @MinDigits(value = "9.5") 38 | @MaxDigits(value = "9.6") 39 | private Double isTooHigh = new Double(10.0); // Do not Pass 40 | 41 | 42 | @MinDigits(value = "9.5") 43 | @MaxDigits(value = "9.6") 44 | private Double isTooLow = new Double(9.2); // Do not Pass 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/NotInstanceOfTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.Data; 4 | import lombok.EqualsAndHashCode; 5 | import net.andreinc.jbvext.annotations.misc.NotInstanceOf; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import javax.validation.ConstraintViolation; 10 | import javax.validation.Validator; 11 | import java.util.Arrays; 12 | import java.util.List; 13 | import java.util.Set; 14 | 15 | import static javax.validation.Validation.buildDefaultValidatorFactory; 16 | 17 | @Data 18 | @EqualsAndHashCode(callSuper = false) 19 | class NIofEveAndAdam { 20 | } 21 | 22 | @Data 23 | @EqualsAndHashCode(callSuper = false) 24 | class NIofCain extends NIofEveAndAdam { 25 | } 26 | 27 | @Data 28 | @EqualsAndHashCode(callSuper = false) 29 | class NIofAbel extends NIofEveAndAdam { 30 | } 31 | 32 | @Data 33 | @EqualsAndHashCode(callSuper = false) 34 | class NIofSeth extends NIofEveAndAdam { 35 | } 36 | 37 | public class NotInstanceOfTest { 38 | 39 | private Validator validator = 40 | buildDefaultValidatorFactory().getValidator(); 41 | 42 | @Test 43 | public void testFields() { 44 | NotInstanceOfBeanFields notInstanceOfBeanFields = new NotInstanceOfBeanFields(); 45 | 46 | Set> violations = 47 | validator.validate(notInstanceOfBeanFields); 48 | 49 | Assert.assertEquals(1, violations.size()); 50 | } 51 | 52 | @Test 53 | public void testMethods() { 54 | InstanceOfBeanMethods instanceOfBeanMethods = new InstanceOfBeanMethods(); 55 | 56 | Set> violations = 57 | validator.validate(instanceOfBeanMethods); 58 | 59 | Assert.assertEquals(1, violations.size()); 60 | } 61 | 62 | @Test 63 | public void testType() { 64 | NotInstanceOfType notInstanceOfType = new NotInstanceOfType(); 65 | 66 | Set> violations = 67 | validator.validate(notInstanceOfType); 68 | 69 | Assert.assertEquals(0, violations.size()); 70 | } 71 | } 72 | 73 | @Data 74 | class NotInstanceOfBeanFields { 75 | 76 | @NotInstanceOf({NIofSeth.class, NIofCain.class}) 77 | private NIofEveAndAdam correct = new NIofAbel(); 78 | 79 | @NotInstanceOf({NIofSeth.class, NIofCain.class}) 80 | private NIofEveAndAdam incorrect = new NIofCain(); 81 | 82 | } 83 | 84 | @Data 85 | class NotInstanceOfBeanMethods { 86 | 87 | @NotInstanceOf({NIofSeth.class, NIofCain.class}) 88 | private NIofEveAndAdam getCorrect() { 89 | return new NIofAbel(); 90 | } 91 | 92 | @NotInstanceOf({NIofSeth.class, NIofCain.class}) 93 | private NIofEveAndAdam getIncorrect() { 94 | return new NIofCain(); 95 | } 96 | 97 | } 98 | 99 | @Data 100 | class NotInstanceOfType { 101 | @NotInstanceOf({NIofSeth.class, NIofCain.class}) 102 | private List sons = Arrays.asList(new NIofAbel()); 103 | } -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/NumericTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.Data; 4 | import net.andreinc.jbvext.annotations.str.Numeric; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import javax.validation.ConstraintViolation; 9 | import javax.validation.Validation; 10 | import javax.validation.Validator; 11 | import java.util.Arrays; 12 | import java.util.List; 13 | import java.util.Set; 14 | 15 | public class NumericTest { 16 | 17 | private Validator validator = 18 | Validation.buildDefaultValidatorFactory().getValidator(); 19 | 20 | @Test 21 | public void testFields() { 22 | NumericBeanFields numericBeanFields = new NumericBeanFields(); 23 | 24 | Set> violations = 25 | validator.validate(numericBeanFields); 26 | 27 | Assert.assertEquals(1, violations.size()); 28 | } 29 | 30 | @Test 31 | public void testMethods() { 32 | NumericBeanMethods numericBeanMethods = new NumericBeanMethods(); 33 | 34 | Set> violations = 35 | validator.validate(numericBeanMethods); 36 | 37 | Assert.assertEquals(1, violations.size()); 38 | } 39 | 40 | @Test 41 | public void testType() { 42 | NumericBeanType numericBeanType = new NumericBeanType(); 43 | 44 | Set> violations = 45 | validator.validate(numericBeanType); 46 | 47 | Assert.assertEquals(0, violations.size()); 48 | } 49 | } 50 | 51 | 52 | @Data 53 | class NumericBeanFields { 54 | 55 | @Numeric 56 | private String correct = "123"; 57 | 58 | @Numeric 59 | private String incorrect = "a123"; 60 | } 61 | 62 | @Data 63 | class NumericBeanMethods { 64 | @Numeric 65 | private String getCorrect() { 66 | return "123"; 67 | } 68 | 69 | @Numeric 70 | private String getIncorrect() { 71 | return "a123"; 72 | } 73 | } 74 | 75 | @Data 76 | class NumericBeanType { 77 | private List<@Numeric String> nums = Arrays.asList("123", "0"); 78 | } -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/OneOfStringsTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import net.andreinc.jbvext.annotations.misc.OneOfStrings; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import javax.validation.ConstraintViolation; 10 | import javax.validation.Valid; 11 | import javax.validation.Validation; 12 | import javax.validation.Validator; 13 | import java.util.Arrays; 14 | import java.util.List; 15 | import java.util.Set; 16 | 17 | public class OneOfStringsTest { 18 | private Validator validator = 19 | Validation.buildDefaultValidatorFactory().getValidator(); 20 | 21 | @Test 22 | public void testFields() { 23 | OneOfStringsBeanFields oneOfStringsBeanFields = new OneOfStringsBeanFields("A"); 24 | 25 | Set> violations = 26 | validator.validate(oneOfStringsBeanFields); 27 | 28 | Assert.assertEquals(0, violations.size()); 29 | } 30 | 31 | @Test 32 | public void testFieldsIncorrect() { 33 | OneOfStringsBeanFields oneOfStringsBeanFields = new OneOfStringsBeanFields("Z"); 34 | 35 | Set> violations = 36 | validator.validate(oneOfStringsBeanFields); 37 | 38 | Assert.assertEquals(1, violations.size()); 39 | } 40 | 41 | @Test 42 | public void testFieldsWithNull() { 43 | OneOfStringsBeanFields oneOfStringsBeanFields = new OneOfStringsBeanFields(null); 44 | 45 | Set> violations = 46 | validator.validate(oneOfStringsBeanFields); 47 | 48 | Assert.assertEquals(1, violations.size()); 49 | } 50 | 51 | @Test 52 | public void testMethods() { 53 | OneOfStringsBeanMethods oneOfStringsBeanMethods = new OneOfStringsBeanMethods("A"); 54 | 55 | Set> violations = 56 | validator.validate(oneOfStringsBeanMethods); 57 | 58 | Assert.assertEquals(0, violations.size()); 59 | } 60 | 61 | @Test 62 | public void testType() { 63 | OneOfStringsBeanType oneOfStringsBeanType = new OneOfStringsBeanType(Arrays.asList("A")); 64 | 65 | Set> violations = 66 | validator.validate(oneOfStringsBeanType); 67 | 68 | Assert.assertEquals(0, violations.size()); 69 | } 70 | } 71 | 72 | @Data 73 | @AllArgsConstructor 74 | @Valid class OneOfStringsBeanFields { 75 | @OneOfStrings({"A", "B"}) 76 | private String aString; 77 | } 78 | 79 | @Data 80 | @AllArgsConstructor 81 | class OneOfStringsBeanMethods { 82 | 83 | private String aString; 84 | 85 | @OneOfStrings({"A", "B"}) 86 | private String getAString() { 87 | return aString; 88 | } 89 | } 90 | 91 | @Data 92 | @AllArgsConstructor 93 | class OneOfStringsBeanType { 94 | private List<@OneOfStrings({"A", "B"}) String> strs; 95 | } -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/ParseableTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Builder; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | import net.andreinc.jbvext.annotations.str.Parseable; 8 | import org.junit.Assert; 9 | import org.junit.Test; 10 | 11 | import javax.validation.ConstraintViolation; 12 | import javax.validation.Validation; 13 | import javax.validation.Validator; 14 | import java.util.Set; 15 | 16 | import static net.andreinc.jbvext.annotations.str.ParseableType.*; 17 | 18 | public class ParseableTest { 19 | 20 | private Validator validator = 21 | Validation.buildDefaultValidatorFactory().getValidator(); 22 | 23 | 24 | @Test 25 | public void testFields() { 26 | ParseableBeanFields startsWithBeanFields = ParseableBeanFields 27 | .builder() 28 | .parseInt("12") 29 | .parseDouble("12.21") 30 | .parseFloat("12.2") 31 | .parseLong("22121") 32 | .parseShort("121") 33 | .build(); 34 | 35 | Set> violations = 36 | validator.validate(startsWithBeanFields); 37 | 38 | Assert.assertEquals(0, violations.size()); 39 | } 40 | 41 | @Test 42 | public void testFieldsAllNull() { 43 | ParseableBeanFields startsWithBeanFields = new ParseableBeanFields(); 44 | 45 | Set> violations = 46 | validator.validate(startsWithBeanFields); 47 | 48 | Assert.assertEquals(5, violations.size()); 49 | } 50 | } 51 | 52 | 53 | @Data 54 | @Builder 55 | @NoArgsConstructor 56 | @AllArgsConstructor 57 | class ParseableBeanFields { 58 | 59 | @Parseable(TO_INT) 60 | private String parseInt; 61 | 62 | @Parseable(TO_DOUBLE) 63 | private String parseDouble; 64 | 65 | @Parseable(TO_LONG) 66 | private String parseLong; 67 | 68 | @Parseable(TO_FLOAT) 69 | private String parseFloat; 70 | 71 | @Parseable(TO_SHORT) 72 | private String parseShort; 73 | 74 | } -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/PasswordTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | import net.andreinc.jbvext.annotations.str.Password; 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import javax.validation.ConstraintViolation; 11 | import javax.validation.Validation; 12 | import javax.validation.Validator; 13 | import java.util.Set; 14 | 15 | public class PasswordTest { 16 | 17 | private Validator validator = Validation 18 | .buildDefaultValidatorFactory() 19 | .getValidator(); 20 | 21 | @Test 22 | public void nullPasswordTest() { 23 | PassBean1 passBean1 = new PassBean1(null); 24 | Set> violations = validator.validate(passBean1); 25 | Assert.assertEquals(1, violations.size()); 26 | } 27 | 28 | @Test 29 | public void minSizeTest() { 30 | PassBean2 p = new PassBean2(); 31 | Set> violations = validator.validate(p); 32 | Assert.assertEquals(1, violations.size()); 33 | } 34 | 35 | @Test 36 | public void badPass() { 37 | PassBean3 p = new PassBean3(); 38 | Set> violations = validator.validate(p); 39 | Assert.assertEquals(1, violations.size()); 40 | } 41 | 42 | @Test 43 | public void goodPass() { 44 | PassBean4 p = new PassBean4(); 45 | Set> violations = validator.validate(p); 46 | Assert.assertEquals(0, violations.size()); 47 | } 48 | } 49 | 50 | @AllArgsConstructor 51 | @Data 52 | class PassBean1 { 53 | @Password() 54 | private String pass1 = null; 55 | } 56 | 57 | @AllArgsConstructor 58 | @Data 59 | @NoArgsConstructor 60 | class PassBean2 { 61 | @Password(minSize = 3, containsDigits = false) 62 | private String pass1 = "aA@"; 63 | 64 | @Password(minSize = 3, containsDigits = false, containsSpecialChar = false, containsUpperCase = false) 65 | private String pass2 = "ab"; 66 | } 67 | 68 | @AllArgsConstructor 69 | @Data 70 | @NoArgsConstructor 71 | class PassBean3 { 72 | @Password 73 | private String badPass = "AAbbCCddEE1"; 74 | } 75 | 76 | @AllArgsConstructor 77 | @Data 78 | @NoArgsConstructor 79 | class PassBean4 { 80 | @Password 81 | private String goodPass = "Ab1_abcde"; 82 | } -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/StartsWithTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.Data; 4 | import net.andreinc.jbvext.annotations.str.StartsWith; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import javax.validation.ConstraintViolation; 9 | import javax.validation.Validation; 10 | import javax.validation.Validator; 11 | import java.util.Arrays; 12 | import java.util.List; 13 | import java.util.Set; 14 | 15 | public class StartsWithTest { 16 | 17 | private Validator validator = 18 | Validation.buildDefaultValidatorFactory().getValidator(); 19 | 20 | @Test 21 | public void testFields() { 22 | StartsWithBeanFields startsWithBeanFields = new StartsWithBeanFields(); 23 | 24 | Set> violations = 25 | validator.validate(startsWithBeanFields); 26 | 27 | Assert.assertEquals(1, violations.size()); 28 | } 29 | 30 | @Test 31 | public void testMethods() { 32 | StartsWithBeanMethods startsWithBeanMethods = new StartsWithBeanMethods(); 33 | 34 | Set> violations = 35 | validator.validate(startsWithBeanMethods); 36 | 37 | Assert.assertEquals(1, violations.size()); 38 | } 39 | 40 | @Test 41 | public void testType() { 42 | StartsWithBeanType startsWithBeanType = new StartsWithBeanType(); 43 | 44 | Set> violations = 45 | validator.validate(startsWithBeanType); 46 | 47 | Assert.assertEquals(0, violations.size()); 48 | } 49 | 50 | } 51 | 52 | @Data 53 | class StartsWithBeanFields { 54 | 55 | @StartsWith("A") 56 | private String correct = "ABC"; 57 | 58 | @StartsWith("B") 59 | private String incorrect = "ABC"; 60 | } 61 | 62 | @Data 63 | class StartsWithBeanMethods { 64 | 65 | @StartsWith("A") 66 | private String getCorrect() { 67 | return "ABC"; 68 | } 69 | 70 | @StartsWith("B") 71 | private String getIncorrect() { 72 | return "ABC"; 73 | } 74 | } 75 | 76 | class StartsWithBeanType { 77 | private List<@StartsWith("A") String> strs = Arrays.asList("ABC", "AND"); 78 | } -------------------------------------------------------------------------------- /src/test/java/net/andreinc/jbvext/test/UpperCaseTest.java: -------------------------------------------------------------------------------- 1 | package net.andreinc.jbvext.test; 2 | 3 | import lombok.Data; 4 | import net.andreinc.jbvext.annotations.str.UpperCase; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import javax.validation.ConstraintViolation; 9 | import javax.validation.Validation; 10 | import javax.validation.Validator; 11 | import java.util.Arrays; 12 | import java.util.List; 13 | import java.util.Set; 14 | 15 | public class UpperCaseTest { 16 | private Validator validator = 17 | Validation.buildDefaultValidatorFactory().getValidator(); 18 | 19 | @Test 20 | public void testFields() { 21 | UpperCaseBeanFields upperCaseBeanFields = new UpperCaseBeanFields(); 22 | 23 | Set> violations = 24 | validator.validate(upperCaseBeanFields); 25 | 26 | Assert.assertEquals(1, violations.size()); 27 | } 28 | 29 | @Test 30 | public void testMethods() { 31 | UpperCaseBeanMethods upperCaseBeanMethods = new UpperCaseBeanMethods(); 32 | 33 | Set> violations = 34 | validator.validate(upperCaseBeanMethods); 35 | 36 | Assert.assertEquals(1, violations.size()); 37 | } 38 | 39 | @Test 40 | public void testType() { 41 | UpperCaseBeanType upperCaseBeanType = new UpperCaseBeanType(); 42 | 43 | Set> violations = 44 | validator.validate(upperCaseBeanType); 45 | 46 | Assert.assertEquals(0, violations.size()); 47 | } 48 | } 49 | 50 | @Data 51 | class UpperCaseBeanFields { 52 | 53 | @UpperCase 54 | private String correct = "ABC"; 55 | 56 | @UpperCase 57 | private String incorrect = "aBC"; 58 | } 59 | 60 | @Data 61 | class UpperCaseBeanMethods { 62 | 63 | @UpperCase 64 | private String getCorrect() { 65 | return "ABC"; 66 | } 67 | 68 | @UpperCase 69 | private String getIncorrect() { 70 | return "aBC"; 71 | } 72 | } 73 | 74 | @Data 75 | class UpperCaseBeanType { 76 | private List<@UpperCase String> sts = Arrays.asList("ABC", "AXXX"); 77 | } 78 | 79 | 80 | 81 | --------------------------------------------------------------------------------