├── .github ├── dependabot.yml └── workflows │ ├── gradle-build.yml │ └── update-gradle-wrapper.yml ├── .gitignore ├── LICENSE.txt ├── README.md ├── build-logic ├── build.gradle.kts ├── settings.gradle.kts └── src │ └── main │ └── kotlin │ ├── beanvalidators.code-quality-conventions.gradle.kts │ ├── beanvalidators.coveralls-conventions.gradle.kts │ ├── beanvalidators.documentation-conventions.gradle.kts │ ├── beanvalidators.java-conventions.gradle.kts │ └── beanvalidators.publishing-conventions.gradle.kts ├── build.gradle.kts ├── gradle.properties ├── gradle ├── libs.versions.toml └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle.kts └── src ├── docs └── asciidoc │ ├── index.adoc │ └── parts │ ├── test-fixtures.adoc │ ├── validators.adoc │ └── validators │ ├── empty.adoc │ ├── files.adoc │ ├── net.adoc │ └── strings.adoc ├── main ├── java │ └── com │ │ └── github │ │ └── britter │ │ └── beanvalidators │ │ ├── Empty.java │ │ ├── Present.java │ │ ├── file │ │ ├── Absolute.java │ │ ├── Directory.java │ │ ├── Executable.java │ │ ├── Existing.java │ │ ├── Hidden.java │ │ ├── IsFile.java │ │ ├── NotAbsolute.java │ │ ├── NotDirectory.java │ │ ├── NotExecutable.java │ │ ├── NotExisting.java │ │ ├── NotFile.java │ │ ├── NotHidden.java │ │ ├── NotReadable.java │ │ ├── NotWritable.java │ │ ├── Readable.java │ │ ├── Writable.java │ │ ├── internal │ │ │ ├── AbsoluteFileConstraintValidator.java │ │ │ ├── AbsoluteStringFileConstraintValidator.java │ │ │ ├── DirectoryFileConstraintValidator.java │ │ │ ├── DirectoryStringFileConstraintValidator.java │ │ │ ├── ExecutableFileConstraintValidator.java │ │ │ ├── ExecutableStringFileConstraintValidator.java │ │ │ ├── ExistingFileConstraintValidator.java │ │ │ ├── ExistingStringFileConstraintValidator.java │ │ │ ├── HiddenFileConstraintValidator.java │ │ │ ├── HiddenStringFileConstraintValidator.java │ │ │ ├── IsFileFileConstraintValidator.java │ │ │ ├── IsFileStringFileConstraintValidator.java │ │ │ ├── NotAbsoluteFileConstraintValidator.java │ │ │ ├── NotAbsoluteStringFileConstraintValidator.java │ │ │ ├── NotDirectoryFileConstraintValidator.java │ │ │ ├── NotDirectoryStringFileConstraintValidator.java │ │ │ ├── NotExecutableFileConstraintValidator.java │ │ │ ├── NotExecutableStringFileConstraintValidator.java │ │ │ ├── NotExistingFileConstraintValidator.java │ │ │ ├── NotExistingStringFileConstraintValidator.java │ │ │ ├── NotFileFileConstraintValidator.java │ │ │ ├── NotFileStringFileConstraintValidator.java │ │ │ ├── NotHiddenFileConstraintValidator.java │ │ │ ├── NotHiddenStringFileConstraintValidator.java │ │ │ ├── NotReadableFileConstraintValidator.java │ │ │ ├── NotReadableStringFileConstraintValidator.java │ │ │ ├── NotWritableFileConstraintValidator.java │ │ │ ├── NotWritableStringFileConstraintValidator.java │ │ │ ├── ReadableFileConstraintValidator.java │ │ │ ├── ReadableStringFileConstraintValidator.java │ │ │ ├── StringToFileConvertingConstraintValidator.java │ │ │ ├── WritableFileConstraintValidator.java │ │ │ └── WritableStringFileConstraintValidator.java │ │ └── package-info.java │ │ ├── internal │ │ ├── BlankStringAcceptingConstraintValidator.java │ │ ├── EmptyArrayConstraintValidator.java │ │ ├── EmptyCollectionConstraintValidator.java │ │ ├── EmptyMapConstraintValidator.java │ │ ├── EmptyOptionalConstraintValidator.java │ │ ├── EmptyStringConstraintValidator.java │ │ ├── NullAcceptingConstraintValidator.java │ │ └── PresentConstraintValidator.java │ │ ├── net │ │ ├── Domain.java │ │ ├── IP.java │ │ ├── IPType.java │ │ ├── Port.java │ │ ├── internal │ │ │ ├── DomainConstraintValidator.java │ │ │ ├── IPConstraintValidator.java │ │ │ ├── PortIntegerConstraintValidator.java │ │ │ ├── PortRange.java │ │ │ └── PortStringConstraintValidator.java │ │ └── package-info.java │ │ ├── package-info.java │ │ ├── strings │ │ ├── ASCII.java │ │ ├── AlphaNumeric.java │ │ ├── Alphabetic.java │ │ ├── Blank.java │ │ ├── ISBN.java │ │ ├── ISBNType.java │ │ ├── JavaNumber.java │ │ ├── Numeric.java │ │ ├── internal │ │ │ ├── ASCIIConstraintValidator.java │ │ │ ├── AlphaNumericConstraintValidator.java │ │ │ ├── AlphabeticConstraintValidator.java │ │ │ ├── BlankConstraintValidator.java │ │ │ ├── ISBNConstraintValidator.java │ │ │ ├── JavaNumberConstraintValidator.java │ │ │ └── NumericConstraintValidator.java │ │ └── package-info.java │ │ └── time │ │ ├── AfterNow.java │ │ ├── BeforeNow.java │ │ ├── internal │ │ ├── AfterNowInstantConstraintValidator.java │ │ ├── AfterNowLocalDateConstraintValidator.java │ │ ├── AfterNowLocalDateTimeConstraintValidator.java │ │ ├── AfterNowLocalTimeConstraintValidator.java │ │ ├── AfterNowOffsetDateTimeConstraintValidator.java │ │ ├── AfterNowOffsetTimeConstraintValidator.java │ │ ├── AfterNowYearConstraintValidator.java │ │ ├── AfterNowYearMonthConstraintValidator.java │ │ ├── AfterNowZonedDateTimeConstraintValidator.java │ │ ├── BeforeNowInstantConstraintValidator.java │ │ ├── BeforeNowLocalDateConstraintValidator.java │ │ ├── BeforeNowLocalDateTimeConstraintValidator.java │ │ ├── BeforeNowLocalTimeConstraintValidator.java │ │ ├── BeforeNowOffsetDateTimeConstraintValidator.java │ │ ├── BeforeNowOffsetTimeConstraintValidator.java │ │ ├── BeforeNowYearConstraintValidator.java │ │ ├── BeforeNowYearMonthConstraintValidator.java │ │ └── BeforeNowZonedDateTimeConstraintValidator.java │ │ └── package-info.java └── resources │ └── ValidationMessages.properties ├── test └── java │ └── com │ └── github │ └── britter │ └── beanvalidators │ ├── EmptyTest.java │ ├── PresentTest.java │ ├── file │ ├── AbsoluteTest.java │ ├── BaseFileTest.java │ ├── DirectoryTest.java │ ├── ExecutableTest.java │ ├── ExistingTest.java │ ├── HiddenTest.java │ ├── IsFileTest.java │ ├── NotAbsoluteTest.java │ ├── NotDirectoryTest.java │ ├── NotExecutableTest.java │ ├── NotExistingTest.java │ ├── NotFileTest.java │ ├── NotHiddenTest.java │ ├── NotReadableTest.java │ ├── NotWritableTest.java │ ├── ReadableTest.java │ └── WritableTest.java │ ├── net │ ├── DomainTest.java │ ├── IPTest.java │ └── PortTest.java │ ├── strings │ ├── ASCIITest.java │ ├── AlphaNumericTest.java │ ├── AlphabeticTest.java │ ├── BlankTest.java │ ├── ISBNTest.java │ ├── JavaNumberTest.java │ └── NumericTest.java │ └── time │ ├── AfterNowTest.java │ └── internal │ └── BeforeNowTest.java └── testFixtures └── java └── com └── github └── britter └── beanvalidators ├── BeanValidatorsAsserts.java ├── ConstraintViolationAssert.java └── ValidationWrapper.java /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "github-actions" 4 | directory: "/" 5 | schedule: 6 | interval: "weekly" 7 | - package-ecosystem: "gradle" 8 | directory: "/" 9 | schedule: 10 | interval: "weekly" 11 | - package-ecosystem: "gradle" 12 | directory: "build-logic/" 13 | schedule: 14 | interval: "weekly" 15 | -------------------------------------------------------------------------------- /.github/workflows/gradle-build.yml: -------------------------------------------------------------------------------- 1 | name: CI Build 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | branches: 9 | - main 10 | 11 | jobs: 12 | validation: 13 | name: "Validate Gradle Wrapper" 14 | runs-on: ubuntu-latest 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: gradle/wrapper-validation-action@v2.0.0 18 | build: 19 | strategy: 20 | fail-fast: false 21 | matrix: 22 | os: [ubuntu-latest, macos-latest, windows-latest] 23 | runs-on: ${{ matrix.os }} 24 | name: Check on ${{ matrix.os }} 25 | steps: 26 | - name: git clone 27 | uses: actions/checkout@v4 28 | - name: Set up JDK 11 29 | uses: actions/setup-java@v4 30 | with: 31 | distribution: zulu 32 | java-version: 11 33 | - name: Setup Gradle 34 | uses: gradle/gradle-build-action@v2.12.0 35 | - run: "./gradlew check" 36 | id: gradle 37 | - name: Coveralls 38 | if: matrix.os == 'ubuntu-latest' 39 | run: "./gradlew jacocoTestReport coveralls" 40 | env: 41 | COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }} 42 | - name: "Comment build scan url" 43 | uses: actions/github-script@v7 44 | if: github.event_name == 'pull_request' && failure() 45 | with: 46 | github-token: ${{secrets.GITHUB_TOKEN}} 47 | script: | 48 | github.rest.issues.createComment({ 49 | issue_number: context.issue.number, 50 | owner: context.repo.owner, 51 | repo: context.repo.repo, 52 | body: '❌ ${{ job.name }} failed: ${{ steps.gradle.outputs.build-scan-url }}' 53 | }) 54 | -------------------------------------------------------------------------------- /.github/workflows/update-gradle-wrapper.yml: -------------------------------------------------------------------------------- 1 | name: Update Gradle Wrapper 2 | 3 | on: 4 | schedule: 5 | - cron: "0 0 * * *" 6 | 7 | jobs: 8 | update-gradle-wrapper: 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - uses: actions/checkout@v4 13 | 14 | - name: Update Gradle Wrapper 15 | uses: gradle-update/update-gradle-wrapper-action@v1 16 | with: 17 | repo-token: ${{ secrets.GRADLE_UPDATE_PAT }} 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### https://raw.github.com/github/gitignore/f2ce448f2ba7a092da05482ceca99209127c0884/Gradle.gitignore 2 | 3 | .gradle 4 | build/ 5 | 6 | # Ignore Gradle GUI config 7 | gradle-app.setting 8 | 9 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 10 | !gradle-wrapper.jar 11 | 12 | # Cache of project 13 | .gradletasknamecache 14 | 15 | # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 16 | # gradle/wrapper/gradle-wrapper.properties 17 | 18 | 19 | ### https://raw.github.com/github/gitignore/f2ce448f2ba7a092da05482ceca99209127c0884/Gradle.gitignore 20 | 21 | .gradle 22 | /build/ 23 | 24 | # Ignore Gradle GUI config 25 | gradle-app.setting 26 | 27 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 28 | !gradle-wrapper.jar 29 | 30 | # Cache of project 31 | .gradletasknamecache 32 | 33 | # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 34 | # gradle/wrapper/gradle-wrapper.properties 35 | 36 | # marker file to accept publishing build scans 37 | gradle/.build-scans-tos-agree 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bean-validators 2 | 3 | [![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fbritter%2Fbean-validators%2Fbadge%3Fref%3Dmain&style=flat)](https://actions-badge.atrox.dev/britter/bean-validators/goto?ref=main) 4 | [![Coverage Status](https://coveralls.io/repos/britter/bean-validators/badge.svg)](https://coveralls.io/r/britter/bean-validators) 5 | [![Codacy Badge](https://app.codacy.com/project/badge/Grade/9008af3b60554d3a8759c36356f42a7c)](https://www.codacy.com/gh/britter/bean-validators/dashboard?utm_source=github.com&utm_medium=referral&utm_content=britter/bean-validators&utm_campaign=Badge_Grade) 6 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.britter/bean-validators/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.britter/bean-validators/) 7 | [![License](https://img.shields.io/:license-apache-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0.html) 8 | 9 | Additional validator implementations for [javax.validation](https://beanvalidation.org/). 10 | Most validators use [Apache Commons Lang3](https://commons.apache.org/lang/) and 11 | [Apache Commons Validator](https://commons.apache.org/validator/) functionality to implement various validation routines. 12 | 13 | More information can be found on the [project website](https://britter.github.io/bean-validators). 14 | 15 | ## How to release 16 | 17 | - Set the version to be released in `build.gradle.kts` and commit and tag the change 18 | - Sign an arbitrary file to force gpg-agent to store the password for the gpg key (see https://github.com/gradle/gradle/issues/11706) 19 | - Run `./gradlew publishToSonatype closeAndReleaseSonatypeStagingRepository` 20 | - Deploy the site for the new version `./gradlew gitPublishPush` 21 | - Set the version to the next SNAPSHOT version in `build.gradle.kts` and commit and push the change 22 | 23 | ## License 24 | 25 | Code is under the [Apache Licence v2](https://www.apache.org/licenses/LICENSE-2.0.txt). 26 | 27 | ## Additional Resources 28 | 29 | + [javax.validation documentation](https://beanvalidation.org/) 30 | + [javax.validation reference implementation](https://hibernate.org/validator/) 31 | + [Apache Commons Lang3](https://commons.apache.org/lang/) 32 | + [Apache Commons Validator](https://commons.apache.org/validator/) 33 | -------------------------------------------------------------------------------- /build-logic/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | plugins { 17 | `kotlin-dsl` 18 | } 19 | 20 | repositories { 21 | gradlePluginPortal() 22 | } 23 | 24 | dependencies { 25 | implementation(libs.coverallsPlugin) 26 | implementation(libs.asciidoctorPlugin) 27 | implementation(libs.gitPublishPlugin) 28 | implementation(libs.nexusPublishPlugin) 29 | implementation(libs.spotlessPlugin) 30 | } 31 | -------------------------------------------------------------------------------- /build-logic/settings.gradle.kts: -------------------------------------------------------------------------------- 1 | dependencyResolutionManagement { 2 | versionCatalogs { 3 | create("libs") { 4 | from(files("../gradle/libs.versions.toml")) 5 | } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /build-logic/src/main/kotlin/beanvalidators.code-quality-conventions.gradle.kts: -------------------------------------------------------------------------------- 1 | import org.gradle.api.tasks.SourceSetContainer 2 | import org.gradle.kotlin.dsl.pmd 3 | 4 | plugins { 5 | pmd 6 | id("com.github.spotbugs") 7 | } 8 | 9 | pmd { 10 | sourceSets = listOf(the()["main"]) 11 | } 12 | 13 | tasks.named("spotbugsTest") { 14 | enabled = false 15 | } 16 | -------------------------------------------------------------------------------- /build-logic/src/main/kotlin/beanvalidators.coveralls-conventions.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | plugins { 17 | jacoco 18 | id("com.github.kt3k.coveralls") 19 | } 20 | 21 | tasks { 22 | val jacocoTestReport = named("jacocoTestReport") { 23 | reports { 24 | xml.required.set(true) 25 | } 26 | } 27 | 28 | coveralls { 29 | jacocoReportPath = jacocoTestReport.map { it.reports.xml.outputLocation.asFile.get().path } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /build-logic/src/main/kotlin/beanvalidators.documentation-conventions.gradle.kts: -------------------------------------------------------------------------------- 1 | import org.ajoberstar.gradle.git.publish.tasks.GitPublishCommit 2 | import org.ajoberstar.gradle.git.publish.tasks.GitPublishReset 3 | /* 4 | * Copyright 2019 Benedikt Ritter 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 | plugins { 19 | id("org.asciidoctor.jvm.convert") 20 | id("org.ajoberstar.git-publish") 21 | } 22 | 23 | tasks { 24 | named("javadoc", Javadoc::class) { 25 | exclude("**/internal/**") 26 | } 27 | 28 | asciidoctor { 29 | outputOptions { 30 | separateOutputDirs = false 31 | } 32 | 33 | attributes(mapOf( 34 | "source-highlighter" to "coderay", 35 | "tabsize" to "4", 36 | "toc" to "left", 37 | "icons" to "font", 38 | "sectanchors" to true, 39 | "idprefix" to "", 40 | "idseparator" to "-" 41 | )) 42 | } 43 | } 44 | 45 | val asciidoctor by tasks.getting 46 | val javadoc by tasks.getting 47 | val testFixturesJavadoc by tasks.getting 48 | 49 | gitPublish { 50 | repoUri.set("https://github.com/britter/bean-validators") 51 | branch.set("gh-pages") 52 | sign.set(false) 53 | 54 | contents { 55 | from(asciidoctor) 56 | from(javadoc) { 57 | into("apidocs/main") 58 | } 59 | from(testFixturesJavadoc) { 60 | into("apidocs/test-fixtures") 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /build-logic/src/main/kotlin/beanvalidators.java-conventions.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | import java.nio.charset.StandardCharsets 17 | 18 | plugins { 19 | `java-library` 20 | `java-test-fixtures` 21 | } 22 | 23 | java { 24 | toolchain { 25 | languageVersion.set(JavaLanguageVersion.of(8)) 26 | } 27 | withJavadocJar() 28 | withSourcesJar() 29 | } 30 | 31 | tasks { 32 | withType { 33 | options.encoding = StandardCharsets.UTF_8.name() 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /build-logic/src/main/kotlin/beanvalidators.publishing-conventions.gradle.kts: -------------------------------------------------------------------------------- 1 | import java.time.Duration 2 | 3 | /* 4 | * Copyright 2019 Benedikt Ritter 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 | plugins { 19 | `maven-publish` 20 | signing 21 | id("io.github.gradle-nexus.publish-plugin") 22 | } 23 | 24 | publishing { 25 | publications { 26 | create("maven") { 27 | from(components["java"]) 28 | 29 | pom { 30 | name.set(provider { project.name }) 31 | description.set(provider { project.description }) 32 | url.set("https://britter.github.io/bean-validators") 33 | licenses { 34 | license { 35 | name.set("The Apache License, Version 2.0") 36 | url.set("https://www.apache.org/licenses/LICENSE-2.0.txt") 37 | } 38 | } 39 | developers { 40 | developer { 41 | id.set("britter") 42 | name.set("Benedikt Ritter") 43 | email.set("beneritter@gmail.com") 44 | } 45 | } 46 | scm { 47 | connection.set("scm:git:git://github.com/britter/bean-validators.git") 48 | developerConnection.set("scm:git:ssh://git@github.com/britter/bean-validators.git") 49 | url.set("https://github.com/britter/bean-validators") 50 | } 51 | } 52 | } 53 | } 54 | } 55 | 56 | nexusPublishing { 57 | connectTimeout.set(Duration.ofMinutes(5)) 58 | clientTimeout.set(Duration.ofMinutes(5)) 59 | repositories { 60 | sonatype() 61 | } 62 | } 63 | 64 | signing { 65 | useGpgCmd() 66 | sign(publishing.publications["maven"]) 67 | } 68 | -------------------------------------------------------------------------------- /build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | description = "Additional validator implementations for javax.validation" 17 | group = "com.github.britter" 18 | version = "0.8.1-SNAPSHOT" 19 | 20 | plugins { 21 | id("beanvalidators.java-conventions") 22 | id("beanvalidators.coveralls-conventions") 23 | id("beanvalidators.documentation-conventions") 24 | id("beanvalidators.publishing-conventions") 25 | id("beanvalidators.code-quality-conventions") 26 | id("org.nosphere.gradle.github.actions") version "1.4.0" 27 | } 28 | 29 | repositories { 30 | mavenCentral() 31 | } 32 | 33 | dependencies { 34 | api(libs.javax.validation) 35 | implementation(libs.commons.lang) 36 | implementation(libs.commons.validator) 37 | 38 | testImplementation(libs.junit.jupiter.api) 39 | testRuntimeOnly(libs.junit.jupiter.engine) 40 | testRuntimeOnly(libs.hibernate.validator) 41 | 42 | testFixturesApi(libs.assertj.core) 43 | testFixturesApi(libs.javax.elApi) 44 | testFixturesImplementation(libs.guava) 45 | testFixturesImplementation(libs.javax.jaxbApi) 46 | } 47 | 48 | tasks { 49 | val testTasks = listOf(8, 11, 17).map { 50 | register("testJdk$it") { 51 | javaLauncher.set(project.javaToolchains.launcherFor { 52 | languageVersion.set(JavaLanguageVersion.of(it)) 53 | }) 54 | } 55 | } 56 | 57 | val testAll by registering { 58 | dependsOn(test, testTasks) 59 | } 60 | 61 | withType { 62 | useJUnitPlatform() 63 | } 64 | 65 | check { 66 | dependsOn(testAll) 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | signing.gnupg.keyName=394546A47BB40E12 2 | signing.gnupg.executable=gpg 3 | 4 | org.gradle.parallel=true 5 | org.gradle.caching=true 6 | org.gradle.vfs.watch=true 7 | 8 | org.gradle.java.installations.fromEnv=JDK8 9 | 10 | -------------------------------------------------------------------------------- /gradle/libs.versions.toml: -------------------------------------------------------------------------------- 1 | [libraries] 2 | # main build 3 | assertj-core = { module = "org.assertj:assertj-core", version = "3.25.3"} 4 | commons-lang = { module = "org.apache.commons:commons-lang3", version = "3.14.0" } 5 | commons-validator = { module = "commons-validator:commons-validator", version = "1.8.0" } 6 | guava = { module = "com.google.guava:guava", version= "33.0.0-jre" } 7 | hibernate-validator = { module = "org.hibernate:hibernate-validator", version = "5.1.3.Final" } 8 | javax-elApi = { module = "javax.el:javax.el-api", version = "3.0.0" } 9 | javax-jaxbApi = { module = "javax.xml.bind:jaxb-api", version = "2.3.1" } 10 | javax-validation = { module = "javax.validation:validation-api", version = "1.1.0.Final" } 11 | junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version = "5.10.2" } 12 | junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version = "5.10.1" } 13 | 14 | # buildSrc 15 | asciidoctorPlugin = { module = "org.asciidoctor:asciidoctor-gradle-jvm", version = "2.4.0" } 16 | coverallsPlugin = { module = "gradle.plugin.org.kt3k.gradle.plugin:coveralls-gradle-plugin", version = "2.11.0" } 17 | gitPublishPlugin = { module = "org.ajoberstar:gradle-git-publish", version = "3.0.0-rc.1" } 18 | nexusPublishPlugin = { module = "io.github.gradle-nexus:publish-plugin", version = "1.1.0" } 19 | spotlessPlugin = { module = "com.github.spotbugs.snom:spotbugs-gradle-plugin", version = "5.1.3" } 20 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/britter/bean-validators/841daa8e9f0b937fc0a3e63e7386b7391ed68dab/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-8.6-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /settings.gradle.kts: -------------------------------------------------------------------------------- 1 | rootProject.name = "bean-validators" 2 | 3 | plugins { 4 | id("com.gradle.enterprise") version "3.16.2" 5 | id("com.gradle.common-custom-user-data-gradle-plugin") version "1.12.1" 6 | } 7 | 8 | includeBuild("build-logic") 9 | 10 | gradleEnterprise { 11 | buildScan { 12 | val isCi = !System.getenv("GITHUB_WORKFLOW").isNullOrEmpty() 13 | if(isCi || file("gradle/.build-scans-tos-agree").exists()) { 14 | termsOfServiceUrl = "https://gradle.com/terms-of-service" 15 | termsOfServiceAgree = "yes" 16 | publishAlways() 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/docs/asciidoc/parts/test-fixtures.adoc: -------------------------------------------------------------------------------- 1 | //// 2 | Copyright 2019 Benedikt Ritter 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | https://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | //// 16 | 17 | Since the 0.7.0 release this project also publishes a https://docs.gradle.org/current/userguide/java_testing.html#sec:java_test_fixtures[test fixtures variant]. 18 | The test fixtures variant provides functionality which is useful for testing custom validators. 19 | Please refer to the link:apidocs/test-fixtures/index.html[Test Fixtures JavaDoc] for more information on how to use. 20 | 21 | To use the test fixtures in your tests add them to your Gradle build: 22 | 23 | .build.gradle.kts 24 | [source,java,subs="attributes+"] 25 | ---- 26 | dependencies { 27 | tesImplementation(testFixtures("{gradle-project-group}:{gradle-project-name}:{gradle-project-version}")) 28 | } 29 | ---- 30 | 31 | If you use Maven you can add them by adding the corresponding classifer: 32 | 33 | .pom.xml 34 | [source,xml,subs="attributes+"] 35 | ---- 36 | 37 | {gradle-project-group} 38 | {gradle-project-name} 39 | {gradle-project-version} 40 | test-fixtures 41 | 42 | ---- 43 | -------------------------------------------------------------------------------- /src/docs/asciidoc/parts/validators.adoc: -------------------------------------------------------------------------------- 1 | //// 2 | Copyright 2019 Benedikt Ritter 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | https://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | //// 16 | All Validators will treat null and blank input strings as valid. If you want to make sure fields are not null/blank 17 | you have to use `@NotNull` or `@NotBlank` in addition. 18 | 19 | # @Empty 20 | 21 | include::{includedir}/parts/validators/empty.adoc[] 22 | 23 | # Strings 24 | 25 | include::{includedir}/parts/validators/strings.adoc[] 26 | 27 | # Files 28 | 29 | include::{includedir}/parts/validators/files.adoc[] 30 | 31 | # Net 32 | 33 | include::{includedir}/parts/validators/net.adoc[] 34 | -------------------------------------------------------------------------------- /src/docs/asciidoc/parts/validators/empty.adoc: -------------------------------------------------------------------------------- 1 | //// 2 | Copyright 2019 Benedikt Ritter 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | https://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | //// 16 | 17 | The `@Empty` annotation is the opposite of `org.hibernate.validator.constraints.NotEmpty` and works on String, 18 | Collections, Maps and arrays. If any other type is annotated with `@Empty` a `javax.validation.ValidationException` will 19 | be thrown. 20 | 21 | [source,java] 22 | ---- 23 | /** 24 | * valid: 25 | * null 26 | * "" 27 | * 28 | * invalid: 29 | * " " 30 | * "abcd" 31 | */ 32 | @Empty 33 | private String empty; 34 | 35 | /** 36 | * valid: 37 | * null 38 | * empty.isEmpty() == true 39 | * 40 | * invalid: 41 | * empty.isEmpty() == false 42 | */ 43 | @Empty 44 | private Collection empty; 45 | 46 | /** 47 | * valid: 48 | * null 49 | * empty.isEmpty() == true 50 | * 51 | * invalid: 52 | * empty.isEmpty() == false 53 | */ 54 | @Empty 55 | private Map empty; 56 | 57 | /** 58 | * valid: 59 | * null 60 | * empty.length == 0 61 | * 62 | * invalid: 63 | * empty.length != 0 64 | */ 65 | @Empty 66 | private T[] empty; 67 | ---- 68 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/Empty.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators; 17 | 18 | import com.github.britter.beanvalidators.internal.EmptyArrayConstraintValidator; 19 | import com.github.britter.beanvalidators.internal.EmptyCollectionConstraintValidator; 20 | import com.github.britter.beanvalidators.internal.EmptyMapConstraintValidator; 21 | import com.github.britter.beanvalidators.internal.EmptyOptionalConstraintValidator; 22 | import com.github.britter.beanvalidators.internal.EmptyStringConstraintValidator; 23 | 24 | import javax.validation.Constraint; 25 | import javax.validation.Payload; 26 | import java.lang.annotation.Documented; 27 | import java.lang.annotation.Retention; 28 | import java.lang.annotation.Target; 29 | 30 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 31 | import static java.lang.annotation.ElementType.FIELD; 32 | import static java.lang.annotation.ElementType.METHOD; 33 | import static java.lang.annotation.ElementType.PARAMETER; 34 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 35 | 36 | /** 37 | * Opposite of {@code org.hibernate.validator.constraints.NotEmpty} 38 | * 39 | *

Works on String, Collections, Maps, arrays and Optionals. If any other type is annotated with @Empty 40 | * a {@link javax.validation.ValidationException} will be thrown.

41 | */ 42 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 43 | @Retention(RUNTIME) 44 | @Constraint(validatedBy = { 45 | EmptyArrayConstraintValidator.class, 46 | EmptyCollectionConstraintValidator.class, 47 | EmptyMapConstraintValidator.class, 48 | EmptyOptionalConstraintValidator.class, 49 | EmptyStringConstraintValidator.class 50 | }) 51 | @Documented 52 | public @interface Empty { 53 | 54 | String message() default "{com.github.britter.beanvalidators.Empty.message}"; 55 | 56 | Class[] groups() default {}; 57 | 58 | Class[] payload() default {}; 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/Present.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators; 17 | 18 | import com.github.britter.beanvalidators.internal.PresentConstraintValidator; 19 | 20 | import javax.validation.Constraint; 21 | import javax.validation.Payload; 22 | import java.lang.annotation.Documented; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.Target; 25 | 26 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 27 | import static java.lang.annotation.ElementType.FIELD; 28 | import static java.lang.annotation.ElementType.METHOD; 29 | import static java.lang.annotation.ElementType.PARAMETER; 30 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 31 | 32 | 33 | /** 34 | * Makes sure the validated optional is present. 35 | * 36 | * @see java.util.Optional#isPresent() 37 | * @since 1.0.0 38 | */ 39 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 40 | @Retention(RUNTIME) 41 | @Constraint(validatedBy = PresentConstraintValidator.class) 42 | @Documented 43 | public @interface Present { 44 | 45 | String message() default "{com.github.britter.beanvalidators.Present.message}"; 46 | 47 | Class[] groups() default {}; 48 | 49 | Class[] payload() default {}; 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/Absolute.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file; 17 | 18 | import com.github.britter.beanvalidators.file.internal.AbsoluteFileConstraintValidator; 19 | import com.github.britter.beanvalidators.file.internal.AbsoluteStringFileConstraintValidator; 20 | 21 | import javax.validation.Constraint; 22 | import javax.validation.Payload; 23 | import java.lang.annotation.Documented; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.Target; 26 | 27 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 28 | import static java.lang.annotation.ElementType.FIELD; 29 | import static java.lang.annotation.ElementType.METHOD; 30 | import static java.lang.annotation.ElementType.PARAMETER; 31 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 32 | 33 | /** 34 | * Makes sure the validated file or path represents an absolute path in the file system. 35 | * 36 | * @see java.io.File#isAbsolute() 37 | * @since 0.5.0 38 | */ 39 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 40 | @Retention(RUNTIME) 41 | @Constraint(validatedBy = { 42 | AbsoluteFileConstraintValidator.class, 43 | AbsoluteStringFileConstraintValidator.class 44 | }) 45 | @Documented 46 | public @interface Absolute { 47 | 48 | String message() default "{com.github.britter.beanvalidators.file.Absolute.message}"; 49 | 50 | Class[] groups() default {}; 51 | 52 | Class[] payload() default {}; 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/Directory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file; 17 | 18 | import com.github.britter.beanvalidators.file.internal.DirectoryFileConstraintValidator; 19 | import com.github.britter.beanvalidators.file.internal.DirectoryStringFileConstraintValidator; 20 | 21 | import javax.validation.Constraint; 22 | import javax.validation.Payload; 23 | import java.lang.annotation.Documented; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.Target; 26 | 27 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 28 | import static java.lang.annotation.ElementType.FIELD; 29 | import static java.lang.annotation.ElementType.METHOD; 30 | import static java.lang.annotation.ElementType.PARAMETER; 31 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 32 | 33 | /** 34 | * Makes sure the validated file or path represents a directory. 35 | * 36 | * @see java.io.File#isDirectory() 37 | * @since 0.5.0 38 | */ 39 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 40 | @Retention(RUNTIME) 41 | @Constraint(validatedBy = { 42 | DirectoryFileConstraintValidator.class, 43 | DirectoryStringFileConstraintValidator.class, 44 | }) 45 | @Documented 46 | public @interface Directory { 47 | 48 | String message() default "{com.github.britter.beanvalidators.file.Directory.message}"; 49 | 50 | Class[] groups() default {}; 51 | 52 | Class[] payload() default {}; 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/Executable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file; 17 | 18 | import com.github.britter.beanvalidators.file.internal.ExecutableFileConstraintValidator; 19 | import com.github.britter.beanvalidators.file.internal.ExecutableStringFileConstraintValidator; 20 | 21 | import javax.validation.Constraint; 22 | import javax.validation.Payload; 23 | import java.lang.annotation.Documented; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.Target; 26 | 27 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 28 | import static java.lang.annotation.ElementType.FIELD; 29 | import static java.lang.annotation.ElementType.METHOD; 30 | import static java.lang.annotation.ElementType.PARAMETER; 31 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 32 | 33 | /** 34 | * Makes sure the validated file or path has executable permission in the file system. 35 | * 36 | * @see java.io.File#canExecute() 37 | * @since 0.5.0 38 | */ 39 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 40 | @Retention(RUNTIME) 41 | @Constraint(validatedBy = { 42 | ExecutableFileConstraintValidator.class, 43 | ExecutableStringFileConstraintValidator.class, 44 | }) 45 | @Documented 46 | public @interface Executable { 47 | 48 | String message() default "{com.github.britter.beanvalidators.file.Executable.message}"; 49 | 50 | Class[] groups() default {}; 51 | 52 | Class[] payload() default {}; 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/Existing.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file; 17 | 18 | import com.github.britter.beanvalidators.file.internal.ExistingFileConstraintValidator; 19 | import com.github.britter.beanvalidators.file.internal.ExistingStringFileConstraintValidator; 20 | 21 | import javax.validation.Constraint; 22 | import javax.validation.Payload; 23 | import java.lang.annotation.Documented; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.Target; 26 | 27 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 28 | import static java.lang.annotation.ElementType.FIELD; 29 | import static java.lang.annotation.ElementType.METHOD; 30 | import static java.lang.annotation.ElementType.PARAMETER; 31 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 32 | 33 | /** 34 | * Makes sure the validated file or path exists. 35 | * 36 | * @see java.io.File#exists() 37 | * @since 0.5.0 38 | */ 39 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 40 | @Retention(RUNTIME) 41 | @Constraint(validatedBy = { 42 | ExistingFileConstraintValidator.class, 43 | ExistingStringFileConstraintValidator.class, 44 | }) 45 | @Documented 46 | public @interface Existing { 47 | 48 | String message() default "{com.github.britter.beanvalidators.file.Existing.message}"; 49 | 50 | Class[] groups() default {}; 51 | 52 | Class[] payload() default {}; 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/Hidden.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file; 17 | 18 | import com.github.britter.beanvalidators.file.internal.HiddenFileConstraintValidator; 19 | import com.github.britter.beanvalidators.file.internal.HiddenStringFileConstraintValidator; 20 | 21 | import javax.validation.Constraint; 22 | import javax.validation.Payload; 23 | import java.lang.annotation.Documented; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.Target; 26 | 27 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 28 | import static java.lang.annotation.ElementType.FIELD; 29 | import static java.lang.annotation.ElementType.METHOD; 30 | import static java.lang.annotation.ElementType.PARAMETER; 31 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 32 | 33 | /** 34 | * Makes sure the validated file or path is hidden. 35 | * 36 | *

37 | * Note that the definition of a hidden files various between operating systems. For examples in an UNIX-like OS, all 38 | * files and directories starting with a dot are considered to be hidden, whereas on Windows the hidden attribute of the 39 | * file has to be set to true. 40 | *

41 | * 42 | * @see java.io.File#isHidden() 43 | * @since 0.5.0 44 | */ 45 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 46 | @Retention(RUNTIME) 47 | @Constraint(validatedBy = { 48 | HiddenFileConstraintValidator.class, 49 | HiddenStringFileConstraintValidator.class, 50 | }) 51 | @Documented 52 | public @interface Hidden { 53 | 54 | String message() default "{com.github.britter.beanvalidators.file.Hidden.message}"; 55 | 56 | Class[] groups() default {}; 57 | 58 | Class[] payload() default {}; 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/IsFile.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file; 17 | 18 | import com.github.britter.beanvalidators.file.internal.IsFileFileConstraintValidator; 19 | import com.github.britter.beanvalidators.file.internal.IsFileStringFileConstraintValidator; 20 | 21 | import javax.validation.Constraint; 22 | import javax.validation.Payload; 23 | import java.lang.annotation.Documented; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.Target; 26 | 27 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 28 | import static java.lang.annotation.ElementType.FIELD; 29 | import static java.lang.annotation.ElementType.METHOD; 30 | import static java.lang.annotation.ElementType.PARAMETER; 31 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 32 | 33 | /** 34 | * Makes sure the validated file or path represents a file in the file system. 35 | * 36 | * @see java.io.File#isFile() 37 | * @since 0.5.0 38 | */ 39 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 40 | @Retention(RUNTIME) 41 | @Constraint(validatedBy = { 42 | IsFileFileConstraintValidator.class, 43 | IsFileStringFileConstraintValidator.class 44 | }) 45 | @Documented 46 | public @interface IsFile { 47 | 48 | String message() default "{com.github.britter.beanvalidators.file.IsFile.message}"; 49 | 50 | Class[] groups() default {}; 51 | 52 | Class[] payload() default {}; 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/NotAbsolute.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file; 17 | 18 | import com.github.britter.beanvalidators.file.internal.NotAbsoluteFileConstraintValidator; 19 | import com.github.britter.beanvalidators.file.internal.NotAbsoluteStringFileConstraintValidator; 20 | 21 | import javax.validation.Constraint; 22 | import javax.validation.Payload; 23 | import java.lang.annotation.Documented; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.Target; 26 | 27 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 28 | import static java.lang.annotation.ElementType.FIELD; 29 | import static java.lang.annotation.ElementType.METHOD; 30 | import static java.lang.annotation.ElementType.PARAMETER; 31 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 32 | 33 | /** 34 | * Makes sure the validated file or path does not represent an absolute path in the file system. 35 | * 36 | * @see java.io.File#isAbsolute() 37 | * @since 0.5.0 38 | */ 39 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 40 | @Retention(RUNTIME) 41 | @Constraint(validatedBy = { 42 | NotAbsoluteFileConstraintValidator.class, 43 | NotAbsoluteStringFileConstraintValidator.class, 44 | }) 45 | @Documented 46 | public @interface NotAbsolute { 47 | 48 | String message() default "{com.github.britter.beanvalidators.file.NotAbsolute.message}"; 49 | 50 | Class[] groups() default {}; 51 | 52 | Class[] payload() default {}; 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/NotDirectory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file; 17 | 18 | import com.github.britter.beanvalidators.file.internal.NotDirectoryFileConstraintValidator; 19 | import com.github.britter.beanvalidators.file.internal.NotDirectoryStringFileConstraintValidator; 20 | 21 | import javax.validation.Constraint; 22 | import javax.validation.Payload; 23 | import java.lang.annotation.Documented; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.Target; 26 | 27 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 28 | import static java.lang.annotation.ElementType.FIELD; 29 | import static java.lang.annotation.ElementType.METHOD; 30 | import static java.lang.annotation.ElementType.PARAMETER; 31 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 32 | 33 | /** 34 | * Makes sure the validated file or path does not represent a directory. 35 | * 36 | * @see java.io.File#isDirectory() 37 | * @since 0.5.0 38 | */ 39 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 40 | @Retention(RUNTIME) 41 | @Constraint(validatedBy = { 42 | NotDirectoryFileConstraintValidator.class, 43 | NotDirectoryStringFileConstraintValidator.class, 44 | }) 45 | @Documented 46 | public @interface NotDirectory { 47 | 48 | String message() default "{com.github.britter.beanvalidators.file.NotDirectory.message}"; 49 | 50 | Class[] groups() default {}; 51 | 52 | Class[] payload() default {}; 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/NotExecutable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file; 17 | 18 | import com.github.britter.beanvalidators.file.internal.NotExecutableFileConstraintValidator; 19 | import com.github.britter.beanvalidators.file.internal.NotExecutableStringFileConstraintValidator; 20 | 21 | import javax.validation.Constraint; 22 | import javax.validation.Payload; 23 | import java.lang.annotation.Documented; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.Target; 26 | 27 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 28 | import static java.lang.annotation.ElementType.FIELD; 29 | import static java.lang.annotation.ElementType.METHOD; 30 | import static java.lang.annotation.ElementType.PARAMETER; 31 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 32 | 33 | /** 34 | * Makes sure the validated file or path does not have executable permission in the file system. 35 | * 36 | * @see java.io.File#canExecute() 37 | * @since 0.5.0 38 | */ 39 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 40 | @Retention(RUNTIME) 41 | @Constraint(validatedBy = { 42 | NotExecutableFileConstraintValidator.class, 43 | NotExecutableStringFileConstraintValidator.class, 44 | }) 45 | @Documented 46 | public @interface NotExecutable { 47 | 48 | String message() default "{com.github.britter.beanvalidators.file.NotExecutable.message}"; 49 | 50 | Class[] groups() default {}; 51 | 52 | Class[] payload() default {}; 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/NotExisting.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file; 17 | 18 | import com.github.britter.beanvalidators.file.internal.NotExistingFileConstraintValidator; 19 | import com.github.britter.beanvalidators.file.internal.NotExistingStringFileConstraintValidator; 20 | 21 | import javax.validation.Constraint; 22 | import javax.validation.Payload; 23 | import java.lang.annotation.Documented; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.Target; 26 | 27 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 28 | import static java.lang.annotation.ElementType.FIELD; 29 | import static java.lang.annotation.ElementType.METHOD; 30 | import static java.lang.annotation.ElementType.PARAMETER; 31 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 32 | 33 | /** 34 | * Makes sure the validated file or path does not exist. 35 | * 36 | * @see java.io.File#exists() 37 | * @since 0.5.0 38 | */ 39 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 40 | @Retention(RUNTIME) 41 | @Constraint(validatedBy = { 42 | NotExistingFileConstraintValidator.class, 43 | NotExistingStringFileConstraintValidator.class, 44 | }) 45 | @Documented 46 | public @interface NotExisting { 47 | 48 | String message() default "{com.github.britter.beanvalidators.file.NotExisting.message}"; 49 | 50 | Class[] groups() default {}; 51 | 52 | Class[] payload() default {}; 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/NotFile.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file; 17 | 18 | import com.github.britter.beanvalidators.file.internal.NotFileFileConstraintValidator; 19 | import com.github.britter.beanvalidators.file.internal.NotFileStringFileConstraintValidator; 20 | 21 | import javax.validation.Constraint; 22 | import javax.validation.Payload; 23 | import java.lang.annotation.Documented; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.Target; 26 | 27 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 28 | import static java.lang.annotation.ElementType.FIELD; 29 | import static java.lang.annotation.ElementType.METHOD; 30 | import static java.lang.annotation.ElementType.PARAMETER; 31 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 32 | 33 | /** 34 | * Makes sure the validated file or path does not represent a file in the file system. 35 | * 36 | *

37 | * Note that both {@link java.io.File#isFile()} and {@link java.io.File#isDirectory()} can return false, for example for 38 | * file objects representing sockets, mounts or pipes. 39 | *

40 | * 41 | * @see java.io.File#isFile() 42 | * @since 0.5.0 43 | */ 44 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 45 | @Retention(RUNTIME) 46 | @Constraint(validatedBy = { 47 | NotFileFileConstraintValidator.class, 48 | NotFileStringFileConstraintValidator.class, 49 | }) 50 | @Documented 51 | public @interface NotFile { 52 | 53 | String message() default "{com.github.britter.beanvalidators.file.NotFile.message}"; 54 | 55 | Class[] groups() default {}; 56 | 57 | Class[] payload() default {}; 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/NotHidden.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file; 17 | 18 | import com.github.britter.beanvalidators.file.internal.NotHiddenFileConstraintValidator; 19 | import com.github.britter.beanvalidators.file.internal.NotHiddenStringFileConstraintValidator; 20 | 21 | import javax.validation.Constraint; 22 | import javax.validation.Payload; 23 | import java.lang.annotation.Documented; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.Target; 26 | 27 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 28 | import static java.lang.annotation.ElementType.FIELD; 29 | import static java.lang.annotation.ElementType.METHOD; 30 | import static java.lang.annotation.ElementType.PARAMETER; 31 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 32 | 33 | /** 34 | * Makes sure the validated file or path is not hidden. 35 | * 36 | *

37 | * Note that the definition of a hidden files various between operating systems. For examples in an UNIX-like OS, all 38 | * files and directories starting with a dot are considered to be hidden, whereas on Windows the hidden attribute of the 39 | * file has to be set to true. 40 | *

41 | * 42 | * @see java.io.File#isHidden() 43 | * @since 0.5.0 44 | */ 45 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 46 | @Retention(RUNTIME) 47 | @Constraint(validatedBy = { 48 | NotHiddenFileConstraintValidator.class, 49 | NotHiddenStringFileConstraintValidator.class, 50 | }) 51 | @Documented 52 | public @interface NotHidden { 53 | 54 | String message() default "{com.github.britter.beanvalidators.file.NotHidden.message}"; 55 | 56 | Class[] groups() default {}; 57 | 58 | Class[] payload() default {}; 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/NotReadable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file; 17 | 18 | import com.github.britter.beanvalidators.file.internal.NotReadableFileConstraintValidator; 19 | import com.github.britter.beanvalidators.file.internal.NotReadableStringFileConstraintValidator; 20 | 21 | import javax.validation.Constraint; 22 | import javax.validation.Payload; 23 | import java.lang.annotation.Documented; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.Target; 26 | 27 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 28 | import static java.lang.annotation.ElementType.FIELD; 29 | import static java.lang.annotation.ElementType.METHOD; 30 | import static java.lang.annotation.ElementType.PARAMETER; 31 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 32 | 33 | /** 34 | * Makes sure the validated file or path is not readable. 35 | * 36 | * @see java.io.File#canRead() 37 | * @since 0.5.0 38 | */ 39 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 40 | @Retention(RUNTIME) 41 | @Constraint(validatedBy = { 42 | NotReadableFileConstraintValidator.class, 43 | NotReadableStringFileConstraintValidator.class, 44 | }) 45 | @Documented 46 | public @interface NotReadable { 47 | 48 | String message() default "{com.github.britter.beanvalidators.file.NotReadable.message}"; 49 | 50 | Class[] groups() default {}; 51 | 52 | Class[] payload() default {}; 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/NotWritable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file; 17 | 18 | import com.github.britter.beanvalidators.file.internal.NotWritableFileConstraintValidator; 19 | import com.github.britter.beanvalidators.file.internal.NotWritableStringFileConstraintValidator; 20 | 21 | import javax.validation.Constraint; 22 | import javax.validation.Payload; 23 | import java.lang.annotation.Documented; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.Target; 26 | 27 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 28 | import static java.lang.annotation.ElementType.FIELD; 29 | import static java.lang.annotation.ElementType.METHOD; 30 | import static java.lang.annotation.ElementType.PARAMETER; 31 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 32 | 33 | /** 34 | * Makes sure the validated file or path is not writable. 35 | * 36 | * @see java.io.File#canWrite() 37 | * @since 0.5.0 38 | */ 39 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 40 | @Retention(RUNTIME) 41 | @Constraint(validatedBy = { 42 | NotWritableFileConstraintValidator.class, 43 | NotWritableStringFileConstraintValidator.class, 44 | }) 45 | @Documented 46 | public @interface NotWritable { 47 | 48 | String message() default "{com.github.britter.beanvalidators.file.NotWritable.message}"; 49 | 50 | Class[] groups() default {}; 51 | 52 | Class[] payload() default {}; 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/Readable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file; 17 | 18 | import com.github.britter.beanvalidators.file.internal.ReadableFileConstraintValidator; 19 | import com.github.britter.beanvalidators.file.internal.ReadableStringFileConstraintValidator; 20 | 21 | import javax.validation.Constraint; 22 | import javax.validation.Payload; 23 | import java.lang.annotation.Documented; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.Target; 26 | 27 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 28 | import static java.lang.annotation.ElementType.FIELD; 29 | import static java.lang.annotation.ElementType.METHOD; 30 | import static java.lang.annotation.ElementType.PARAMETER; 31 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 32 | 33 | /** 34 | * Makes sure the validated file or path is not readable. 35 | * 36 | * @see java.io.File#canRead() 37 | * @since 0.5.0 38 | */ 39 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 40 | @Retention(RUNTIME) 41 | @Constraint(validatedBy = { 42 | ReadableFileConstraintValidator.class, 43 | ReadableStringFileConstraintValidator.class, 44 | }) 45 | @Documented 46 | public @interface Readable { 47 | 48 | String message() default "{com.github.britter.beanvalidators.file.Readable.message}"; 49 | 50 | Class[] groups() default {}; 51 | 52 | Class[] payload() default {}; 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/Writable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file; 17 | 18 | import com.github.britter.beanvalidators.file.internal.WritableFileConstraintValidator; 19 | import com.github.britter.beanvalidators.file.internal.WritableStringFileConstraintValidator; 20 | 21 | import javax.validation.Constraint; 22 | import javax.validation.Payload; 23 | import java.lang.annotation.Documented; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.Target; 26 | 27 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 28 | import static java.lang.annotation.ElementType.FIELD; 29 | import static java.lang.annotation.ElementType.METHOD; 30 | import static java.lang.annotation.ElementType.PARAMETER; 31 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 32 | 33 | /** 34 | * Makes sure the validated file or path is writable. 35 | * 36 | * @see java.io.File#canWrite() 37 | * @since 0.5.0 38 | */ 39 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 40 | @Retention(RUNTIME) 41 | @Constraint(validatedBy = { 42 | WritableFileConstraintValidator.class, 43 | WritableStringFileConstraintValidator.class, 44 | }) 45 | @Documented 46 | public @interface Writable { 47 | 48 | String message() default "{com.github.britter.beanvalidators.file.Writable.message}"; 49 | 50 | Class[] groups() default {}; 51 | 52 | Class[] payload() default {}; 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/AbsoluteFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.Absolute; 19 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.io.File; 23 | 24 | public final class AbsoluteFileConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(File value, ConstraintValidatorContext context) { 28 | return value.isAbsolute(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/AbsoluteStringFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.Absolute; 19 | 20 | import javax.validation.ConstraintValidatorContext; 21 | import java.io.File; 22 | 23 | public final class AbsoluteStringFileConstraintValidator implements StringToFileConvertingConstraintValidator { 24 | 25 | @Override 26 | public boolean isValidNonNullFile(File file, ConstraintValidatorContext context) { 27 | return file.isAbsolute(); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/DirectoryFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.Directory; 19 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.io.File; 23 | 24 | public final class DirectoryFileConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(File value, ConstraintValidatorContext context) { 28 | return value.isDirectory(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/DirectoryStringFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.Directory; 19 | 20 | import javax.validation.ConstraintValidatorContext; 21 | import java.io.File; 22 | 23 | public final class DirectoryStringFileConstraintValidator implements StringToFileConvertingConstraintValidator { 24 | 25 | @Override 26 | public boolean isValidNonNullFile(File file, ConstraintValidatorContext context) { 27 | return file.isDirectory(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/ExecutableFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.Executable; 19 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.io.File; 23 | 24 | public final class ExecutableFileConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(File value, ConstraintValidatorContext context) { 28 | return value.canExecute(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/ExecutableStringFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.Executable; 19 | 20 | import javax.validation.ConstraintValidatorContext; 21 | import java.io.File; 22 | 23 | public final class ExecutableStringFileConstraintValidator implements StringToFileConvertingConstraintValidator { 24 | 25 | @Override 26 | public boolean isValidNonNullFile(File file, ConstraintValidatorContext context) { 27 | return file.canExecute(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/ExistingFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.Existing; 19 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.io.File; 23 | 24 | public final class ExistingFileConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(File value, ConstraintValidatorContext context) { 28 | return value.exists(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/ExistingStringFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.Existing; 19 | 20 | import javax.validation.ConstraintValidatorContext; 21 | import java.io.File; 22 | 23 | public final class ExistingStringFileConstraintValidator implements StringToFileConvertingConstraintValidator { 24 | 25 | @Override 26 | public boolean isValidNonNullFile(File file, ConstraintValidatorContext context) { 27 | return file.exists(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/HiddenFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.Hidden; 19 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.io.File; 23 | 24 | public final class HiddenFileConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(File value, ConstraintValidatorContext context) { 28 | return value.isHidden(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/HiddenStringFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.Hidden; 19 | 20 | import javax.validation.ConstraintValidatorContext; 21 | import java.io.File; 22 | 23 | public final class HiddenStringFileConstraintValidator implements StringToFileConvertingConstraintValidator { 24 | 25 | @Override 26 | public boolean isValidNonNullFile(File file, ConstraintValidatorContext context) { 27 | return file.isHidden(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/IsFileFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.IsFile; 19 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.io.File; 23 | 24 | public final class IsFileFileConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(File value, ConstraintValidatorContext context) { 28 | return value.isFile(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/IsFileStringFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.IsFile; 19 | 20 | import javax.validation.ConstraintValidatorContext; 21 | import java.io.File; 22 | 23 | public final class IsFileStringFileConstraintValidator implements StringToFileConvertingConstraintValidator { 24 | 25 | @Override 26 | public boolean isValidNonNullFile(File file, ConstraintValidatorContext context) { 27 | return file.isFile(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/NotAbsoluteFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.NotAbsolute; 19 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.io.File; 23 | 24 | public final class NotAbsoluteFileConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(File value, ConstraintValidatorContext context) { 28 | return !value.isAbsolute(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/NotAbsoluteStringFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.NotAbsolute; 19 | 20 | import javax.validation.ConstraintValidatorContext; 21 | import java.io.File; 22 | 23 | public final class NotAbsoluteStringFileConstraintValidator implements StringToFileConvertingConstraintValidator { 24 | 25 | @Override 26 | public boolean isValidNonNullFile(File file, ConstraintValidatorContext context) { 27 | return !file.isAbsolute(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/NotDirectoryFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.NotDirectory; 19 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.io.File; 23 | 24 | public final class NotDirectoryFileConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(File value, ConstraintValidatorContext context) { 28 | return !value.isDirectory(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/NotDirectoryStringFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.NotDirectory; 19 | 20 | import javax.validation.ConstraintValidatorContext; 21 | import java.io.File; 22 | 23 | public final class NotDirectoryStringFileConstraintValidator implements StringToFileConvertingConstraintValidator { 24 | 25 | @Override 26 | public boolean isValidNonNullFile(File file, ConstraintValidatorContext context) { 27 | return !file.isDirectory(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/NotExecutableFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.NotExecutable; 19 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.io.File; 23 | 24 | public final class NotExecutableFileConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(File value, ConstraintValidatorContext context) { 28 | return !value.canExecute(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/NotExecutableStringFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.NotExecutable; 19 | 20 | import javax.validation.ConstraintValidatorContext; 21 | import java.io.File; 22 | 23 | public final class NotExecutableStringFileConstraintValidator implements StringToFileConvertingConstraintValidator { 24 | 25 | @Override 26 | public boolean isValidNonNullFile(File file, ConstraintValidatorContext context) { 27 | return !file.canExecute(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/NotExistingFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.NotExisting; 19 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.io.File; 23 | 24 | public final class NotExistingFileConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(File value, ConstraintValidatorContext context) { 28 | return !value.exists(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/NotExistingStringFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.NotExisting; 19 | 20 | import javax.validation.ConstraintValidatorContext; 21 | import java.io.File; 22 | 23 | public final class NotExistingStringFileConstraintValidator implements StringToFileConvertingConstraintValidator { 24 | 25 | @Override 26 | public boolean isValidNonNullFile(File file, ConstraintValidatorContext context) { 27 | return !file.exists(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/NotFileFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.NotFile; 19 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.io.File; 23 | 24 | public final class NotFileFileConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(File value, ConstraintValidatorContext context) { 28 | return !value.isFile(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/NotFileStringFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.NotFile; 19 | 20 | import javax.validation.ConstraintValidatorContext; 21 | import java.io.File; 22 | 23 | public final class NotFileStringFileConstraintValidator implements StringToFileConvertingConstraintValidator { 24 | 25 | @Override 26 | public boolean isValidNonNullFile(File file, ConstraintValidatorContext context) { 27 | return !file.isFile(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/NotHiddenFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.NotHidden; 19 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.io.File; 23 | 24 | public final class NotHiddenFileConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(File value, ConstraintValidatorContext context) { 28 | return !value.isHidden(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/NotHiddenStringFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.NotHidden; 19 | 20 | import javax.validation.ConstraintValidatorContext; 21 | import java.io.File; 22 | 23 | public final class NotHiddenStringFileConstraintValidator implements StringToFileConvertingConstraintValidator { 24 | 25 | @Override 26 | public boolean isValidNonNullFile(File file, ConstraintValidatorContext context) { 27 | return !file.isHidden(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/NotReadableFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.NotReadable; 19 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.io.File; 23 | 24 | public final class NotReadableFileConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(File value, ConstraintValidatorContext context) { 28 | return !value.canRead(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/NotReadableStringFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.NotReadable; 19 | 20 | import javax.validation.ConstraintValidatorContext; 21 | import java.io.File; 22 | 23 | public final class NotReadableStringFileConstraintValidator implements StringToFileConvertingConstraintValidator { 24 | 25 | @Override 26 | public boolean isValidNonNullFile(File file, ConstraintValidatorContext context) { 27 | return !file.canRead(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/NotWritableFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.NotWritable; 19 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.io.File; 23 | 24 | public final class NotWritableFileConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(File value, ConstraintValidatorContext context) { 28 | return !value.canWrite(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/NotWritableStringFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.NotWritable; 19 | 20 | import javax.validation.ConstraintValidatorContext; 21 | import java.io.File; 22 | 23 | public final class NotWritableStringFileConstraintValidator implements StringToFileConvertingConstraintValidator { 24 | 25 | @Override 26 | public boolean isValidNonNullFile(File file, ConstraintValidatorContext context) { 27 | return !file.canWrite(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/ReadableFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.Readable; 19 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.io.File; 23 | 24 | public final class ReadableFileConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(File value, ConstraintValidatorContext context) { 28 | return value.canRead(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/ReadableStringFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.Readable; 19 | 20 | import javax.validation.ConstraintValidatorContext; 21 | import java.io.File; 22 | 23 | public final class ReadableStringFileConstraintValidator implements StringToFileConvertingConstraintValidator { 24 | 25 | @Override 26 | public boolean isValidNonNullFile(File file, ConstraintValidatorContext context) { 27 | return file.canRead(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/StringToFileConvertingConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 19 | 20 | import javax.validation.ConstraintValidatorContext; 21 | import java.io.File; 22 | import java.lang.annotation.Annotation; 23 | 24 | interface StringToFileConvertingConstraintValidator extends NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | default boolean isValidNonNullValue(final String value, final ConstraintValidatorContext context) { 28 | return isValidNonNullFile(new File(value), context); 29 | } 30 | 31 | boolean isValidNonNullFile(File file, ConstraintValidatorContext context); 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/WritableFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.Writable; 19 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.io.File; 23 | 24 | public final class WritableFileConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(File value, ConstraintValidatorContext context) { 28 | return value.canWrite(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/internal/WritableStringFileConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file.internal; 17 | 18 | import com.github.britter.beanvalidators.file.Writable; 19 | 20 | import javax.validation.ConstraintValidatorContext; 21 | import java.io.File; 22 | 23 | public final class WritableStringFileConstraintValidator implements StringToFileConvertingConstraintValidator { 24 | 25 | @Override 26 | public boolean isValidNonNullFile(File file, ConstraintValidatorContext context) { 27 | return file.canWrite(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/file/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * Constraints for validating all properties of {@link java.io.File} objects. 18 | * 19 | *

20 | * There is an annotation for each property as well as one for the negation of each property. 21 | * All annotations can also be applied to Strings. The validators will construct a new File instance using 22 | * {@link java.io.File#File(java.lang.String)} and use that instance for validation. 23 | *

24 | * 25 | * @since 0.5.0 26 | */ 27 | package com.github.britter.beanvalidators.file; 28 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/internal/BlankStringAcceptingConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.internal; 17 | 18 | import org.apache.commons.lang3.StringUtils; 19 | 20 | import javax.validation.ConstraintValidator; 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.lang.annotation.Annotation; 23 | 24 | public interface BlankStringAcceptingConstraintValidator
extends ConstraintValidator { 25 | 26 | @Override 27 | default void initialize(final A constraintAnnotation) { 28 | } 29 | 30 | @Override 31 | default boolean isValid(final String value, final ConstraintValidatorContext context) { 32 | // Don't validate null, empty and blank strings, since these are validated by @NotNull, @NotEmpty and @NotBlank 33 | return StringUtils.isBlank(value) || isValidNonBlankValue(value, context); 34 | } 35 | 36 | boolean isValidNonBlankValue(final String value, final ConstraintValidatorContext context); 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/internal/EmptyArrayConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.internal; 17 | 18 | import com.github.britter.beanvalidators.Empty; 19 | 20 | import javax.validation.ConstraintValidatorContext; 21 | import javax.validation.ValidationException; 22 | import java.lang.reflect.Array; 23 | 24 | public final class EmptyArrayConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(Object value, ConstraintValidatorContext context) { 28 | if (value.getClass().isArray()) { 29 | return Array.getLength(value) == 0; 30 | } else { 31 | // Is this the correct behavior? 32 | throw new ValidationException("@Empty can not be applied to objects of type " + value.getClass()); 33 | } 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/internal/EmptyCollectionConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.internal; 17 | 18 | import com.github.britter.beanvalidators.Empty; 19 | 20 | import javax.validation.ConstraintValidatorContext; 21 | import java.util.Collection; 22 | 23 | public final class EmptyCollectionConstraintValidator implements NullAcceptingConstraintValidator> { 24 | 25 | @Override 26 | public boolean isValidNonNullValue(Collection value, ConstraintValidatorContext context) { 27 | return value.isEmpty(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/internal/EmptyMapConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.internal; 17 | 18 | import com.github.britter.beanvalidators.Empty; 19 | 20 | import javax.validation.ConstraintValidatorContext; 21 | import java.util.Map; 22 | 23 | public final class EmptyMapConstraintValidator implements NullAcceptingConstraintValidator> { 24 | 25 | @Override 26 | public boolean isValidNonNullValue(Map value, ConstraintValidatorContext context) { 27 | return value.isEmpty(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/internal/EmptyOptionalConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.internal; 17 | 18 | import com.github.britter.beanvalidators.Empty; 19 | 20 | import javax.validation.ConstraintValidatorContext; 21 | import java.util.Optional; 22 | 23 | public final class EmptyOptionalConstraintValidator implements NullAcceptingConstraintValidator> { 24 | 25 | @Override 26 | public boolean isValidNonNullValue(Optional value, ConstraintValidatorContext context) { 27 | return !value.isPresent(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/internal/EmptyStringConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.internal; 17 | 18 | import com.github.britter.beanvalidators.Empty; 19 | import org.apache.commons.lang3.StringUtils; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | 23 | public final class EmptyStringConstraintValidator implements NullAcceptingConstraintValidator { 24 | 25 | @Override 26 | public boolean isValidNonNullValue(String value, ConstraintValidatorContext context) { 27 | return StringUtils.isEmpty(value); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/internal/NullAcceptingConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.internal; 17 | 18 | import javax.validation.ConstraintValidator; 19 | import javax.validation.ConstraintValidatorContext; 20 | import java.lang.annotation.Annotation; 21 | 22 | public interface NullAcceptingConstraintValidator extends ConstraintValidator { 23 | 24 | @Override 25 | default void initialize(final A constraintAnnotation) { 26 | } 27 | 28 | @Override 29 | default boolean isValid(final T value, final ConstraintValidatorContext context) { 30 | // Don't validate null, since these are validated by @NotNull 31 | return value == null || isValidNonNullValue(value, context); 32 | } 33 | 34 | boolean isValidNonNullValue(final T value, final ConstraintValidatorContext context); 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/internal/PresentConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.internal; 17 | 18 | import com.github.britter.beanvalidators.Present; 19 | 20 | import javax.validation.ConstraintValidatorContext; 21 | import java.util.Optional; 22 | 23 | public final class PresentConstraintValidator implements NullAcceptingConstraintValidator> { 24 | 25 | @Override 26 | public boolean isValidNonNullValue(Optional value, ConstraintValidatorContext context) { 27 | return value.isPresent(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/net/Domain.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.net; 17 | 18 | import com.github.britter.beanvalidators.net.internal.DomainConstraintValidator; 19 | 20 | import javax.validation.Constraint; 21 | import javax.validation.Payload; 22 | import java.lang.annotation.Documented; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.Target; 25 | 26 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 27 | import static java.lang.annotation.ElementType.FIELD; 28 | import static java.lang.annotation.ElementType.METHOD; 29 | import static java.lang.annotation.ElementType.PARAMETER; 30 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 31 | 32 | /** 33 | * Makes sure a string represents a valid domain. 34 | * 35 | *

Note that the list of valid top level domains is changes frequently. Since 36 | * this validator uses {@code org.apache.commons.validator.routines.DomainValidator} 37 | * under the hood, there may be domains which 38 | * are actually valid reported as invalid, because the Apache Commons Validator TLP list is not up to date.

39 | * 40 | * @since 0.3 41 | */ 42 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 43 | @Retention(RUNTIME) 44 | @Constraint(validatedBy = DomainConstraintValidator.class) 45 | @Documented 46 | public @interface Domain { 47 | 48 | String message() default "{com.github.britter.beanvalidators.net.Domain.message}"; 49 | 50 | Class[] groups() default {}; 51 | 52 | Class[] payload() default {}; 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/net/IP.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.net; 17 | 18 | import com.github.britter.beanvalidators.net.internal.IPConstraintValidator; 19 | 20 | import javax.validation.Constraint; 21 | import javax.validation.Payload; 22 | import java.lang.annotation.Documented; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.Target; 25 | 26 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 27 | import static java.lang.annotation.ElementType.FIELD; 28 | import static java.lang.annotation.ElementType.METHOD; 29 | import static java.lang.annotation.ElementType.PARAMETER; 30 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 31 | 32 | /** 33 | * Makes sure a String represents a valid Internet Protocol (IP) address. 34 | * 35 | *

There are two kinds of IP addresses: IPv4 and the newer IPv6. The default is to validate against both versions. 36 | * This behavior can be controlled by the {@link IPType} enum.

37 | * 38 | * @since 0.3 39 | */ 40 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 41 | @Retention(RUNTIME) 42 | @Constraint(validatedBy = IPConstraintValidator.class) 43 | @Documented 44 | public @interface IP { 45 | 46 | IPType type() default IPType.ALL; 47 | 48 | String message() default "{com.github.britter.beanvalidators.net.IP.message}"; 49 | 50 | Class[] groups() default {}; 51 | 52 | Class[] payload() default {}; 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/net/IPType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.net; 17 | 18 | /** 19 | * Enum controlling the behavior of {@link IP @IP}. 20 | * 21 | * @since 0.3 22 | */ 23 | public enum IPType { 24 | 25 | /** 26 | * Only validate IPv4 addresses. These are addresses of the kind: {@code 192.168.0.1}. 27 | */ 28 | IP_V4, 29 | /** 30 | * Only validate IPv6 addresses. These are addresses of the kind: {@code fe80::8a1f:a1ff:fe11:9326}. 31 | */ 32 | IP_V6, 33 | /** 34 | * Validate both, IPv4 and IPv6 addresses. 35 | */ 36 | ALL 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/net/Port.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.net; 17 | 18 | 19 | import com.github.britter.beanvalidators.net.internal.PortIntegerConstraintValidator; 20 | import com.github.britter.beanvalidators.net.internal.PortStringConstraintValidator; 21 | 22 | import javax.validation.Constraint; 23 | import javax.validation.Payload; 24 | import java.lang.annotation.Documented; 25 | import java.lang.annotation.Retention; 26 | import java.lang.annotation.Target; 27 | 28 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 29 | import static java.lang.annotation.ElementType.FIELD; 30 | import static java.lang.annotation.ElementType.METHOD; 31 | import static java.lang.annotation.ElementType.PARAMETER; 32 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 33 | 34 | /** 35 | * Makes sure a String, int or Integer represents a valid port 36 | * number. 37 | * 38 | *

A port number is a 16-bit unsigned integer, thus ranging from 0 to 65535. {@code @Port} can be applied to String, 39 | * ints and Integers. When applied to any other type, a {@link javax.validation.ValidationException} will be thrown.

40 | * 41 | * @since 0.3 42 | */ 43 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 44 | @Retention(RUNTIME) 45 | @Constraint(validatedBy = { 46 | PortIntegerConstraintValidator.class, 47 | PortStringConstraintValidator.class 48 | }) 49 | @Documented 50 | public @interface Port { 51 | 52 | String message() default "{com.github.britter.beanvalidators.net.Port.message}"; 53 | 54 | Class[] groups() default {}; 55 | 56 | Class[] payload() default {}; 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/net/internal/DomainConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.net.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.BlankStringAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.net.Domain; 20 | import org.apache.commons.validator.routines.DomainValidator; 21 | 22 | import javax.validation.ConstraintValidatorContext; 23 | 24 | public final class DomainConstraintValidator implements BlankStringAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonBlankValue(String value, ConstraintValidatorContext context) { 28 | return DomainValidator.getInstance().isValid(value); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/net/internal/IPConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.net.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.BlankStringAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.net.IP; 20 | import com.github.britter.beanvalidators.net.IPType; 21 | import org.apache.commons.validator.routines.InetAddressValidator; 22 | 23 | import javax.validation.ConstraintValidatorContext; 24 | 25 | @SuppressWarnings("PMD.BeanMembersShouldSerialize") 26 | public final class IPConstraintValidator implements BlankStringAcceptingConstraintValidator { 27 | 28 | private IPType type; 29 | 30 | @Override 31 | public void initialize(final IP constraintAnnotation) { 32 | this.type = constraintAnnotation.type(); 33 | } 34 | 35 | @Override 36 | public boolean isValidNonBlankValue(String value, ConstraintValidatorContext context) { 37 | switch (type) { 38 | case IP_V4: 39 | return InetAddressValidator.getInstance().isValidInet4Address(value); 40 | case IP_V6: 41 | return InetAddressValidator.getInstance().isValidInet6Address(value); 42 | default: 43 | return InetAddressValidator.getInstance().isValid(value); 44 | } 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/net/internal/PortIntegerConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.net.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.net.Port; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | 23 | public final class PortIntegerConstraintValidator implements NullAcceptingConstraintValidator { 24 | 25 | @Override 26 | public boolean isValidNonNullValue(Integer value, ConstraintValidatorContext context) { 27 | return PortRange.isContained(value); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/net/internal/PortRange.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.net.internal; 17 | 18 | final class PortRange { 19 | 20 | private static final int PORT_MIN_VALUE = 0; 21 | private static final int PORT_MAX_VALUE = 65536; 22 | 23 | private PortRange() { 24 | } 25 | 26 | static boolean isContained(int value) { 27 | return value >= PORT_MIN_VALUE && value <= PORT_MAX_VALUE; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/net/internal/PortStringConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.net.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.BlankStringAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.net.Port; 20 | import org.apache.commons.lang3.math.NumberUtils; 21 | 22 | import javax.validation.ConstraintValidatorContext; 23 | 24 | public final class PortStringConstraintValidator implements BlankStringAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonBlankValue(String value, ConstraintValidatorContext context) { 28 | if (NumberUtils.isDigits(value)) { 29 | return PortRange.isContained(Integer.parseInt(value)); 30 | } else { 31 | return false; 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/net/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * Constraints dealing with networking. 18 | */ 19 | package com.github.britter.beanvalidators.net; 20 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * Additional constraints and validators for javax.validator a.k.a. JSR-303. 18 | */ 19 | package com.github.britter.beanvalidators; 20 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/strings/ASCII.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.strings; 17 | 18 | 19 | import com.github.britter.beanvalidators.strings.internal.ASCIIConstraintValidator; 20 | 21 | import javax.validation.Constraint; 22 | import javax.validation.Payload; 23 | import java.lang.annotation.Documented; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.Target; 26 | 27 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 28 | import static java.lang.annotation.ElementType.FIELD; 29 | import static java.lang.annotation.ElementType.METHOD; 30 | import static java.lang.annotation.ElementType.PARAMETER; 31 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 32 | 33 | /** 34 | * Makes sure a String only contains ASCII printable characters (all characters have to be in the range 32 to 126). 35 | * 36 | * @since 0.4 37 | */ 38 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 39 | @Retention(RUNTIME) 40 | @Constraint(validatedBy = ASCIIConstraintValidator.class) 41 | @Documented 42 | public @interface ASCII { 43 | 44 | String message() default "{com.github.britter.beanvalidators.strings.ASCII.message}"; 45 | 46 | Class[] groups() default {}; 47 | 48 | Class[] payload() default {}; 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/strings/AlphaNumeric.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.strings; 17 | 18 | import com.github.britter.beanvalidators.strings.internal.AlphaNumericConstraintValidator; 19 | 20 | import javax.validation.Constraint; 21 | import javax.validation.Payload; 22 | import java.lang.annotation.Documented; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.Target; 25 | 26 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 27 | import static java.lang.annotation.ElementType.FIELD; 28 | import static java.lang.annotation.ElementType.METHOD; 29 | import static java.lang.annotation.ElementType.PARAMETER; 30 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 31 | 32 | 33 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 34 | @Retention(RUNTIME) 35 | @Constraint(validatedBy = AlphaNumericConstraintValidator.class) 36 | @Documented 37 | public @interface AlphaNumeric { 38 | 39 | boolean allowSpaces() default false; 40 | 41 | String message() default "{com.github.britter.beanvalidators.strings.AlphaNumeric.message}"; 42 | 43 | Class[] groups() default {}; 44 | 45 | Class[] payload() default {}; 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/strings/Alphabetic.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.strings; 17 | 18 | import com.github.britter.beanvalidators.strings.internal.AlphabeticConstraintValidator; 19 | 20 | import javax.validation.Constraint; 21 | import javax.validation.Payload; 22 | import java.lang.annotation.Documented; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.Target; 25 | 26 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 27 | import static java.lang.annotation.ElementType.FIELD; 28 | import static java.lang.annotation.ElementType.METHOD; 29 | import static java.lang.annotation.ElementType.PARAMETER; 30 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 31 | 32 | 33 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 34 | @Retention(RUNTIME) 35 | @Constraint(validatedBy = AlphabeticConstraintValidator.class) 36 | @Documented 37 | public @interface Alphabetic { 38 | 39 | boolean allowSpaces() default false; 40 | 41 | String message() default "{com.github.britter.beanvalidators.strings.Alphabetic.message}"; 42 | 43 | Class[] groups() default {}; 44 | 45 | Class[] payload() default {}; 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/strings/Blank.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.strings; 17 | 18 | 19 | import com.github.britter.beanvalidators.strings.internal.BlankConstraintValidator; 20 | 21 | import javax.validation.Constraint; 22 | import javax.validation.Payload; 23 | import java.lang.annotation.Documented; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.Target; 26 | 27 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 28 | import static java.lang.annotation.ElementType.FIELD; 29 | import static java.lang.annotation.ElementType.METHOD; 30 | import static java.lang.annotation.ElementType.PARAMETER; 31 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 32 | 33 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 34 | @Retention(RUNTIME) 35 | @Constraint(validatedBy = BlankConstraintValidator.class) 36 | @Documented 37 | public @interface Blank { 38 | 39 | String message() default "{com.github.britter.beanvalidators.strings.Blank.message}"; 40 | 41 | Class[] groups() default {}; 42 | 43 | Class[] payload() default {}; 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/strings/ISBN.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.strings; 17 | 18 | import com.github.britter.beanvalidators.strings.internal.ISBNConstraintValidator; 19 | 20 | import javax.validation.Constraint; 21 | import javax.validation.Payload; 22 | import java.lang.annotation.Documented; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.Target; 25 | 26 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 27 | import static java.lang.annotation.ElementType.FIELD; 28 | import static java.lang.annotation.ElementType.METHOD; 29 | import static java.lang.annotation.ElementType.PARAMETER; 30 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 31 | 32 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 33 | @Retention(RUNTIME) 34 | @Constraint(validatedBy = ISBNConstraintValidator.class) 35 | @Documented 36 | public @interface ISBN { 37 | 38 | ISBNType type() default ISBNType.ALL; 39 | 40 | String message() default "{com.github.britter.beanvalidators.strings.ISBN.message}"; 41 | 42 | Class[] groups() default {}; 43 | 44 | Class[] payload() default {}; 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/strings/ISBNType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.strings; 17 | 18 | public enum ISBNType { 19 | 20 | ISBN_10, 21 | ISBN_13, 22 | ALL 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/strings/JavaNumber.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.strings; 17 | 18 | 19 | import com.github.britter.beanvalidators.strings.internal.JavaNumberConstraintValidator; 20 | 21 | import javax.validation.Constraint; 22 | import javax.validation.Payload; 23 | import java.lang.annotation.Documented; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.Target; 26 | 27 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 28 | import static java.lang.annotation.ElementType.FIELD; 29 | import static java.lang.annotation.ElementType.METHOD; 30 | import static java.lang.annotation.ElementType.PARAMETER; 31 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 32 | 33 | /** 34 | * Makes sure a String contains a valid Java number. Note that this doesn't mean the given String can be parsed by the 35 | * various {@code parse} and {@code valueOf} methods defined on {@link Integer} etc. It just means, that the String 36 | * could be used to define a number in Java code. For more information see 37 | * {@code org.apache.commons.lang3.math.NumberUtils.isNumber(String)}]. 38 | * 39 | * @since 0.4 40 | */ 41 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 42 | @Retention(RUNTIME) 43 | @Constraint(validatedBy = JavaNumberConstraintValidator.class) 44 | @Documented 45 | public @interface JavaNumber { 46 | 47 | String message() default "{com.github.britter.beanvalidators.strings.JavaNumber.message}"; 48 | 49 | Class[] groups() default {}; 50 | 51 | Class[] payload() default {}; 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/strings/Numeric.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.strings; 17 | 18 | import com.github.britter.beanvalidators.strings.internal.NumericConstraintValidator; 19 | 20 | import javax.validation.Constraint; 21 | import javax.validation.Payload; 22 | import java.lang.annotation.Documented; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.Target; 25 | 26 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 27 | import static java.lang.annotation.ElementType.FIELD; 28 | import static java.lang.annotation.ElementType.METHOD; 29 | import static java.lang.annotation.ElementType.PARAMETER; 30 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 31 | 32 | @Target({METHOD, FIELD, PARAMETER, ANNOTATION_TYPE}) 33 | @Retention(RUNTIME) 34 | @Constraint(validatedBy = NumericConstraintValidator.class) 35 | @Documented 36 | public @interface Numeric { 37 | 38 | String message() default "{com.github.britter.beanvalidators.strings.Numeric.message}"; 39 | 40 | Class[] groups() default {}; 41 | 42 | Class[] payload() default {}; 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/strings/internal/ASCIIConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.strings.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.BlankStringAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.strings.ASCII; 20 | import org.apache.commons.lang3.StringUtils; 21 | 22 | import javax.validation.ConstraintValidatorContext; 23 | 24 | public final class ASCIIConstraintValidator implements BlankStringAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonBlankValue(String value, ConstraintValidatorContext context) { 28 | return StringUtils.isAsciiPrintable(value); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/strings/internal/AlphaNumericConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.strings.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.BlankStringAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.strings.AlphaNumeric; 20 | import org.apache.commons.lang3.StringUtils; 21 | 22 | import javax.validation.ConstraintValidatorContext; 23 | 24 | @SuppressWarnings("PMD.BeanMembersShouldSerialize") 25 | public final class AlphaNumericConstraintValidator implements BlankStringAcceptingConstraintValidator { 26 | 27 | private boolean allowSpaces; 28 | 29 | @Override 30 | public void initialize(final AlphaNumeric constraintAnnotation) { 31 | this.allowSpaces = constraintAnnotation.allowSpaces(); 32 | } 33 | 34 | @Override 35 | public boolean isValidNonBlankValue(String value, ConstraintValidatorContext context) { 36 | return allowSpaces ? StringUtils.isAlphanumericSpace(value) : StringUtils.isAlphanumeric(value); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/strings/internal/AlphabeticConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.strings.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.BlankStringAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.strings.Alphabetic; 20 | import org.apache.commons.lang3.StringUtils; 21 | 22 | import javax.validation.ConstraintValidatorContext; 23 | 24 | @SuppressWarnings("PMD.BeanMembersShouldSerialize") 25 | public final class AlphabeticConstraintValidator implements BlankStringAcceptingConstraintValidator { 26 | 27 | private boolean allowSpaces; 28 | 29 | @Override 30 | public void initialize(final Alphabetic constraintAnnotation) { 31 | this.allowSpaces = constraintAnnotation.allowSpaces(); 32 | } 33 | 34 | @Override 35 | public boolean isValidNonBlankValue(String value, ConstraintValidatorContext context) { 36 | return allowSpaces ? StringUtils.isAlphaSpace(value) : StringUtils.isAlpha(value); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/strings/internal/BlankConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.strings.internal; 17 | 18 | import com.github.britter.beanvalidators.strings.Blank; 19 | import org.apache.commons.lang3.StringUtils; 20 | 21 | import javax.validation.ConstraintValidator; 22 | import javax.validation.ConstraintValidatorContext; 23 | 24 | public final class BlankConstraintValidator implements ConstraintValidator { 25 | 26 | @Override 27 | public void initialize(final Blank constraintAnnotation) { 28 | } 29 | 30 | @Override 31 | public boolean isValid(final String value, final ConstraintValidatorContext context) { 32 | return StringUtils.isBlank(value); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/strings/internal/ISBNConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.strings.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.BlankStringAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.strings.ISBN; 20 | import com.github.britter.beanvalidators.strings.ISBNType; 21 | import org.apache.commons.validator.routines.ISBNValidator; 22 | 23 | import javax.validation.ConstraintValidatorContext; 24 | 25 | @SuppressWarnings("PMD.BeanMembersShouldSerialize") 26 | public final class ISBNConstraintValidator implements BlankStringAcceptingConstraintValidator { 27 | 28 | private ISBNType type; 29 | 30 | @Override 31 | public void initialize(final ISBN constraintAnnotation) { 32 | this.type = constraintAnnotation.type(); 33 | } 34 | 35 | @Override 36 | public boolean isValidNonBlankValue(String value, ConstraintValidatorContext context) { 37 | switch (type) { 38 | case ISBN_10: 39 | return ISBNValidator.getInstance().isValidISBN10(value); 40 | case ISBN_13: 41 | return ISBNValidator.getInstance().isValidISBN13(value); 42 | default: 43 | return ISBNValidator.getInstance().isValid(value); 44 | } 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/strings/internal/JavaNumberConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.strings.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.BlankStringAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.strings.JavaNumber; 20 | import org.apache.commons.lang3.math.NumberUtils; 21 | 22 | import javax.validation.ConstraintValidatorContext; 23 | 24 | public final class JavaNumberConstraintValidator implements BlankStringAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonBlankValue(String value, ConstraintValidatorContext context) { 28 | return NumberUtils.isNumber(value); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/strings/internal/NumericConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.strings.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.BlankStringAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.strings.Numeric; 20 | import org.apache.commons.lang3.StringUtils; 21 | 22 | import javax.validation.ConstraintValidatorContext; 23 | 24 | public final class NumericConstraintValidator implements BlankStringAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonBlankValue(String value, ConstraintValidatorContext context) { 28 | return StringUtils.isNumeric(value); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/strings/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * Various constraints which can be applied to instance of {@link java.lang.String}. 18 | * 19 | *

20 | * All validators treat blank strings as valid. If you need a non empty/non blank String, you annotations such as 21 | * {@code org.hibernate.validator.constraints.NotEmpty} or {@code org.hibernate.validator.constraints.NotBlank} in 22 | * addition. 23 | *

24 | */ 25 | package com.github.britter.beanvalidators.strings; 26 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/time/internal/AfterNowInstantConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.time.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.time.AfterNow; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.time.Instant; 23 | 24 | public final class AfterNowInstantConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(Instant value, ConstraintValidatorContext context) { 28 | return value.isAfter(Instant.now()); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/time/internal/AfterNowLocalDateConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.time.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.time.AfterNow; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.time.LocalDate; 23 | 24 | public final class AfterNowLocalDateConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(LocalDate value, ConstraintValidatorContext context) { 28 | return value.isAfter(LocalDate.now()); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/time/internal/AfterNowLocalDateTimeConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.time.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.time.AfterNow; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.time.LocalDateTime; 23 | 24 | public final class AfterNowLocalDateTimeConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(LocalDateTime value, ConstraintValidatorContext context) { 28 | return value.isAfter(LocalDateTime.now()); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/time/internal/AfterNowLocalTimeConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.time.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.time.AfterNow; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.time.LocalTime; 23 | 24 | public final class AfterNowLocalTimeConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(LocalTime value, ConstraintValidatorContext context) { 28 | return value.isAfter(LocalTime.now()); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/time/internal/AfterNowOffsetDateTimeConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.time.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.time.AfterNow; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.time.OffsetDateTime; 23 | 24 | public final class AfterNowOffsetDateTimeConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(OffsetDateTime value, ConstraintValidatorContext context) { 28 | return value.isAfter(OffsetDateTime.now()); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/time/internal/AfterNowOffsetTimeConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.time.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.time.AfterNow; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.time.OffsetTime; 23 | 24 | public final class AfterNowOffsetTimeConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(OffsetTime value, ConstraintValidatorContext context) { 28 | return value.isAfter(OffsetTime.now()); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/time/internal/AfterNowYearConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.time.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.time.AfterNow; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.time.Year; 23 | 24 | public final class AfterNowYearConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(Year value, ConstraintValidatorContext context) { 28 | return value.isAfter(Year.now()); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/time/internal/AfterNowYearMonthConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.time.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.time.AfterNow; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.time.YearMonth; 23 | 24 | public final class AfterNowYearMonthConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(YearMonth value, ConstraintValidatorContext context) { 28 | return value.isAfter(YearMonth.now()); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/time/internal/AfterNowZonedDateTimeConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.time.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.time.AfterNow; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.time.ZonedDateTime; 23 | 24 | public final class AfterNowZonedDateTimeConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(ZonedDateTime value, ConstraintValidatorContext context) { 28 | return value.isAfter(ZonedDateTime.now()); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/time/internal/BeforeNowInstantConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.time.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.time.BeforeNow; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.time.Instant; 23 | import java.util.Optional; 24 | 25 | public final class BeforeNowInstantConstraintValidator implements NullAcceptingConstraintValidator { 26 | 27 | /** 28 | * Only for testing! 29 | */ 30 | static Optional suppliedNow = Optional.empty(); 31 | 32 | @Override 33 | public boolean isValidNonNullValue(Instant value, ConstraintValidatorContext context) { 34 | return value.isBefore(now()); 35 | } 36 | 37 | private Instant now() { 38 | return suppliedNow.orElse(Instant.now()); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/time/internal/BeforeNowLocalDateConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.time.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.time.BeforeNow; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.time.LocalDate; 23 | 24 | public final class BeforeNowLocalDateConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(LocalDate value, ConstraintValidatorContext context) { 28 | return value.isBefore(LocalDate.now()); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/time/internal/BeforeNowLocalDateTimeConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.time.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.time.BeforeNow; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.time.LocalDateTime; 23 | import java.util.Optional; 24 | 25 | public final class BeforeNowLocalDateTimeConstraintValidator implements NullAcceptingConstraintValidator { 26 | 27 | /** 28 | * Only for testing! 29 | */ 30 | static Optional suppliedNow = Optional.empty(); 31 | 32 | @Override 33 | public boolean isValidNonNullValue(LocalDateTime value, ConstraintValidatorContext context) { 34 | return value.isBefore(now()); 35 | } 36 | 37 | private LocalDateTime now() { 38 | return suppliedNow.orElse(LocalDateTime.now()); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/time/internal/BeforeNowLocalTimeConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.time.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.time.BeforeNow; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.time.LocalTime; 23 | import java.util.Optional; 24 | 25 | public final class BeforeNowLocalTimeConstraintValidator implements NullAcceptingConstraintValidator { 26 | 27 | /** 28 | * Only for testing! 29 | */ 30 | static Optional suppliedNow = Optional.empty(); 31 | 32 | @Override 33 | public boolean isValidNonNullValue(LocalTime value, ConstraintValidatorContext context) { 34 | return value.isBefore(now()); 35 | } 36 | 37 | private LocalTime now() { 38 | return suppliedNow.orElse(LocalTime.now()); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/time/internal/BeforeNowOffsetDateTimeConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.time.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.time.BeforeNow; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.time.OffsetDateTime; 23 | import java.util.Optional; 24 | 25 | public final class BeforeNowOffsetDateTimeConstraintValidator implements NullAcceptingConstraintValidator { 26 | 27 | /** 28 | * Only for testing! 29 | */ 30 | static Optional suppliedNow = Optional.empty(); 31 | 32 | @Override 33 | public boolean isValidNonNullValue(OffsetDateTime value, ConstraintValidatorContext context) { 34 | return value.isBefore(now()); 35 | } 36 | 37 | private OffsetDateTime now() { 38 | return suppliedNow.orElse(OffsetDateTime.now()); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/time/internal/BeforeNowOffsetTimeConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.time.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.time.BeforeNow; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.time.OffsetTime; 23 | import java.util.Optional; 24 | 25 | public final class BeforeNowOffsetTimeConstraintValidator implements NullAcceptingConstraintValidator { 26 | 27 | /** 28 | * Only for testing! 29 | */ 30 | static Optional suppliedNow = Optional.empty(); 31 | 32 | @Override 33 | public boolean isValidNonNullValue(OffsetTime value, ConstraintValidatorContext context) { 34 | return value.isBefore(now()); 35 | } 36 | 37 | private OffsetTime now() { 38 | return suppliedNow.orElse(OffsetTime.now()); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/time/internal/BeforeNowYearConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.time.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.time.BeforeNow; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.time.Year; 23 | 24 | public final class BeforeNowYearConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(Year value, ConstraintValidatorContext context) { 28 | return value.isBefore(Year.now()); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/time/internal/BeforeNowYearMonthConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.time.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.time.BeforeNow; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.time.YearMonth; 23 | 24 | public final class BeforeNowYearMonthConstraintValidator implements NullAcceptingConstraintValidator { 25 | 26 | @Override 27 | public boolean isValidNonNullValue(YearMonth value, ConstraintValidatorContext context) { 28 | return value.isBefore(YearMonth.now()); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/time/internal/BeforeNowZonedDateTimeConstraintValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.time.internal; 17 | 18 | import com.github.britter.beanvalidators.internal.NullAcceptingConstraintValidator; 19 | import com.github.britter.beanvalidators.time.BeforeNow; 20 | 21 | import javax.validation.ConstraintValidatorContext; 22 | import java.time.ZonedDateTime; 23 | import java.util.Optional; 24 | 25 | public final class BeforeNowZonedDateTimeConstraintValidator implements NullAcceptingConstraintValidator { 26 | 27 | /** 28 | * Only for testing! 29 | */ 30 | static Optional suppliedNow = Optional.empty(); 31 | 32 | @Override 33 | public boolean isValidNonNullValue(ZonedDateTime value, ConstraintValidatorContext context) { 34 | return value.isBefore(now()); 35 | } 36 | 37 | private ZonedDateTime now() { 38 | return suppliedNow.orElse(ZonedDateTime.now()); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/github/britter/beanvalidators/time/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * Various constraints which can be applied to instance of the classes contained in {@link java.time}. 18 | */ 19 | package com.github.britter.beanvalidators.time; 20 | -------------------------------------------------------------------------------- /src/main/resources/ValidationMessages.properties: -------------------------------------------------------------------------------- 1 | # Copyright 2015 Benedikt Ritter 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | com.github.britter.beanvalidators.Empty.message=must be empty 16 | com.github.britter.beanvalidators.Present.message=must be present 17 | 18 | com.github.britter.beanvalidators.file.Directory.message=must be a directory 19 | com.github.britter.beanvalidators.file.NotDirectory.message=must not be a directory 20 | com.github.britter.beanvalidators.file.Readable.message=must be readable 21 | com.github.britter.beanvalidators.file.NotReadable.message=must not be readable 22 | com.github.britter.beanvalidators.file.Writable.message=must be writable 23 | com.github.britter.beanvalidators.file.NotWritable.message=must not be writable 24 | com.github.britter.beanvalidators.file.Executable.message=must be executable 25 | com.github.britter.beanvalidators.file.NotExecutable.message=must not be executable 26 | com.github.britter.beanvalidators.file.Hidden.message=must be hidden 27 | com.github.britter.beanvalidators.file.NotHidden.message=must not be hidden 28 | com.github.britter.beanvalidators.file.Existing.message=must exist 29 | com.github.britter.beanvalidators.file.NotExisting.message=must not exist 30 | com.github.britter.beanvalidators.file.Absolute.message=must be absolute 31 | com.github.britter.beanvalidators.file.NotAbsolute.message=must not be absolute 32 | com.github.britter.beanvalidators.file.IsFile.message=must be a file 33 | com.github.britter.beanvalidators.file.NotFile.message=must not be a file 34 | 35 | com.github.britter.beanvalidators.net.Domain.message=must be a domain 36 | com.github.britter.beanvalidators.net.IP.message=must be an IP 37 | com.github.britter.beanvalidators.net.Port.message=must be a port number 38 | 39 | com.github.britter.beanvalidators.strings.Alphabetic.message=must be alphabetic 40 | com.github.britter.beanvalidators.strings.AlphaNumeric.message=must be alpha numeric 41 | com.github.britter.beanvalidators.strings.ASCII.message=must be ASCII printable 42 | com.github.britter.beanvalidators.strings.Blank.message=must be blank 43 | com.github.britter.beanvalidators.strings.ISBN.message=must be an ISBN 44 | com.github.britter.beanvalidators.strings.JavaNumber.message=must be a valid Java number 45 | com.github.britter.beanvalidators.strings.Numeric.message=must be numeric 46 | 47 | com.github.britter.beanvalidators.time.BeforeNow.message=must be before now 48 | com.github.britter.beanvalidators.time.AfterNow.message=must be after now 49 | -------------------------------------------------------------------------------- /src/test/java/com/github/britter/beanvalidators/PresentTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators; 17 | 18 | import org.junit.jupiter.api.BeforeEach; 19 | import org.junit.jupiter.api.Test; 20 | 21 | import java.util.Optional; 22 | 23 | public final class PresentTest { 24 | 25 | private PresentBean presentBean; 26 | private ValidationWrapper validator; 27 | 28 | @BeforeEach 29 | public void setUp() { 30 | presentBean = new PresentBean(); 31 | validator = new ValidationWrapper<>(presentBean, "must be present"); 32 | } 33 | 34 | @Test 35 | public void shouldValidateNullOptional() { 36 | presentBean.optional = null; 37 | 38 | validator.assertNoViolations("optional"); 39 | } 40 | 41 | @Test 42 | public void shouldNotValidateEmptyOptional() { 43 | presentBean.optional = Optional.empty(); 44 | 45 | validator.assertViolation("optional"); 46 | } 47 | 48 | @Test 49 | public void shouldValidatePresentOptional() { 50 | presentBean.optional = Optional.of("some"); 51 | 52 | validator.assertNoViolations("optional"); 53 | } 54 | 55 | private static class PresentBean { 56 | @Present 57 | private Optional optional; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/test/java/com/github/britter/beanvalidators/file/BaseFileTest.java: -------------------------------------------------------------------------------- 1 | package com.github.britter.beanvalidators.file; 2 | 3 | import org.apache.commons.lang3.SystemUtils; 4 | import org.junit.jupiter.api.io.TempDir; 5 | 6 | import java.io.File; 7 | import java.io.IOException; 8 | import java.io.UncheckedIOException; 9 | import java.nio.file.Files; 10 | import java.nio.file.Path; 11 | import java.nio.file.attribute.DosFileAttributeView; 12 | 13 | abstract class BaseFileTest { 14 | 15 | static final String NON_EXISTENT_PATH = "/does/not/exist"; 16 | 17 | static final File NON_EXISTENT_FILE = new File(NON_EXISTENT_PATH); 18 | 19 | @TempDir 20 | public Path tmpDir; 21 | 22 | File file() { 23 | return file("temp"); 24 | } 25 | 26 | File file(String name) { 27 | try { 28 | File file = Files.createFile(tmpDir.resolve(name)).toFile(); 29 | if (name.startsWith(".")) { 30 | makeHidden(file); 31 | } 32 | return file; 33 | } catch (IOException e) { 34 | throw new UncheckedIOException(e); 35 | } 36 | } 37 | 38 | File dir() { 39 | return dir("tempDir"); 40 | } 41 | 42 | File dir(String name) { 43 | try { 44 | File dir = Files.createDirectory(tmpDir.resolve(name)).toFile(); 45 | if (name.startsWith(".")) { 46 | makeHidden(dir); 47 | } 48 | return dir; 49 | } catch (IOException e) { 50 | throw new UncheckedIOException(e); 51 | } 52 | } 53 | 54 | private void makeHidden(File file) throws IOException { 55 | if (SystemUtils.IS_OS_WINDOWS) { 56 | DosFileAttributeView attributes = Files.getFileAttributeView(file.toPath(), DosFileAttributeView.class); 57 | attributes.setHidden(true); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/test/java/com/github/britter/beanvalidators/file/DirectoryTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file; 17 | 18 | import com.github.britter.beanvalidators.ValidationWrapper; 19 | import org.junit.jupiter.api.BeforeEach; 20 | import org.junit.jupiter.api.Test; 21 | 22 | import javax.validation.ValidationException; 23 | import java.io.File; 24 | 25 | import static org.junit.jupiter.api.Assertions.assertThrows; 26 | 27 | public final class DirectoryTest extends BaseFileTest { 28 | 29 | private FileBean fileBean; 30 | private ValidationWrapper validator; 31 | 32 | @BeforeEach 33 | public void setUp() { 34 | fileBean = new FileBean(); 35 | validator = new ValidationWrapper<>(fileBean, "must be a directory"); 36 | } 37 | 38 | @Test 39 | public void shouldValidateNull() { 40 | fileBean.dir = null; 41 | 42 | validator.assertNoViolations("dir"); 43 | } 44 | 45 | @Test 46 | public void shouldValidateDirectory() { 47 | fileBean.dir = dir(); 48 | 49 | validator.assertNoViolations("dir"); 50 | } 51 | 52 | @Test 53 | public void shouldNotValidateFile() { 54 | fileBean.dir = file(); 55 | 56 | validator.assertViolation("dir"); 57 | } 58 | 59 | @Test 60 | public void shouldValidateStringRepresentingDirectory() { 61 | fileBean.path = dir().getAbsolutePath(); 62 | 63 | validator.assertNoViolations("path"); 64 | } 65 | 66 | @Test 67 | public void shouldNotValidateStringRepresentingFile() { 68 | fileBean.path = file().getAbsolutePath(); 69 | 70 | validator.assertViolation("path"); 71 | } 72 | 73 | @Test 74 | public void shouldThrowExceptionWhenOtherTypeIsAnnotated() { 75 | assertThrows(ValidationException.class, () -> validator.validate("object")); 76 | } 77 | 78 | private static final class FileBean { 79 | @Directory 80 | private File dir; 81 | @Directory 82 | private String path; 83 | @Directory 84 | private Object object = new Object(); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/test/java/com/github/britter/beanvalidators/file/NotDirectoryTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.file; 17 | 18 | import javax.validation.ValidationException; 19 | import java.io.File; 20 | 21 | import com.github.britter.beanvalidators.ValidationWrapper; 22 | import org.junit.jupiter.api.BeforeEach; 23 | import org.junit.jupiter.api.Test; 24 | 25 | import static org.junit.jupiter.api.Assertions.assertThrows; 26 | 27 | public final class NotDirectoryTest extends BaseFileTest { 28 | 29 | private FileBean fileBean; 30 | private ValidationWrapper validator; 31 | 32 | @BeforeEach 33 | public void setUp() { 34 | fileBean = new FileBean(); 35 | validator = new ValidationWrapper<>(fileBean, "must not be a directory"); 36 | } 37 | 38 | @Test 39 | public void shouldValidateNull() { 40 | fileBean.dir = null; 41 | 42 | validator.assertNoViolations("dir"); 43 | } 44 | 45 | @Test 46 | public void shouldNotValidateDirectory() { 47 | fileBean.dir = dir(); 48 | 49 | validator.assertViolation("dir"); 50 | } 51 | 52 | @Test 53 | public void shouldValidateFile() { 54 | fileBean.dir = file(); 55 | 56 | validator.assertNoViolations("dir"); 57 | } 58 | 59 | @Test 60 | public void shouldValidateBlankString() { 61 | fileBean.path = " "; 62 | 63 | validator.assertNoViolations("path"); 64 | } 65 | 66 | @Test 67 | public void shouldValidateStringRepresentingFile() { 68 | fileBean.path = file().getAbsolutePath(); 69 | 70 | validator.assertNoViolations("path"); 71 | } 72 | 73 | @Test 74 | public void shouldNotValidateStringRepresentingDirectory() { 75 | fileBean.path = dir().getAbsolutePath(); 76 | 77 | validator.assertViolation("path"); 78 | } 79 | 80 | @Test 81 | public void shouldThrowExceptionWhenOtherTypeisAnnotated() { 82 | assertThrows(ValidationException.class, () -> validator.validate("object")); 83 | } 84 | 85 | private static final class FileBean { 86 | @NotDirectory 87 | private File dir; 88 | @NotDirectory 89 | private String path; 90 | @NotDirectory 91 | private Object object = new Object(); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/test/java/com/github/britter/beanvalidators/net/DomainTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.net; 17 | 18 | import com.github.britter.beanvalidators.ValidationWrapper; 19 | import org.junit.jupiter.api.BeforeEach; 20 | import org.junit.jupiter.api.Test; 21 | 22 | public final class DomainTest { 23 | 24 | private ValidationWrapper validator; 25 | private DomainBean domainBean; 26 | 27 | @BeforeEach 28 | public void setUp() { 29 | domainBean = new DomainBean(); 30 | validator = new ValidationWrapper<>(domainBean, "must be a domain"); 31 | } 32 | 33 | @Test 34 | public void shouldValidateNullString() { 35 | domainBean.domain = null; 36 | 37 | validator.assertNoViolations("domain"); 38 | } 39 | 40 | @Test 41 | public void shouldValidateBlankString() { 42 | domainBean.domain = " "; 43 | 44 | validator.assertNoViolations("domain"); 45 | } 46 | 47 | @Test 48 | public void shouldValidateDomain() { 49 | domainBean.domain = "www.example.com"; 50 | 51 | validator.assertNoViolations("domain"); 52 | } 53 | 54 | @Test 55 | public void shouldNotValidateRandomString() { 56 | domainBean.domain = "abcd"; 57 | 58 | validator.assertViolation("domain"); 59 | } 60 | 61 | private static final class DomainBean { 62 | @Domain 63 | private String domain; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/test/java/com/github/britter/beanvalidators/strings/ASCIITest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.strings; 17 | 18 | import com.github.britter.beanvalidators.ValidationWrapper; 19 | import org.junit.jupiter.api.BeforeEach; 20 | import org.junit.jupiter.api.Test; 21 | 22 | public final class ASCIITest { 23 | 24 | private ASCIIBean asciiBean; 25 | private ValidationWrapper validator; 26 | 27 | @BeforeEach 28 | public void setUp() { 29 | asciiBean = new ASCIIBean(); 30 | validator = new ValidationWrapper<>(asciiBean, "must be ASCII printable"); 31 | } 32 | 33 | @Test 34 | public void shouldValidateNullString() { 35 | asciiBean.ascii = null; 36 | 37 | validator.assertNoViolations("ascii"); 38 | } 39 | 40 | @Test 41 | public void shouldValidateBlankString() { 42 | asciiBean.ascii = " "; 43 | 44 | validator.assertNoViolations("ascii"); 45 | } 46 | 47 | @Test 48 | public void shouldValidateAscii() { 49 | asciiBean.ascii = "abcd"; 50 | 51 | validator.assertNoViolations("ascii"); 52 | } 53 | 54 | @Test 55 | public void shouldNotValidateNonAscii() { 56 | asciiBean.ascii = "äöü"; 57 | 58 | validator.assertViolation("ascii"); 59 | } 60 | 61 | private static final class ASCIIBean { 62 | @ASCII 63 | private String ascii; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/test/java/com/github/britter/beanvalidators/strings/BlankTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.strings; 17 | 18 | import com.github.britter.beanvalidators.ValidationWrapper; 19 | import org.junit.jupiter.api.BeforeEach; 20 | import org.junit.jupiter.api.Test; 21 | 22 | public final class BlankTest { 23 | 24 | private BlankBean blankBean; 25 | private ValidationWrapper validator; 26 | 27 | @BeforeEach 28 | public void setUp() { 29 | blankBean = new BlankBean(); 30 | validator = new ValidationWrapper<>(blankBean, "must be blank"); 31 | } 32 | 33 | @Test 34 | public void shouldValidateEmptyString() { 35 | blankBean.blank = ""; 36 | 37 | validator.assertNoViolations("blank"); 38 | } 39 | 40 | @Test 41 | public void shouldValidateNullString() { 42 | blankBean.blank = null; 43 | 44 | validator.assertNoViolations("blank"); 45 | } 46 | 47 | @Test 48 | public void shouldValidateBlankString() { 49 | blankBean.blank = " "; 50 | 51 | validator.assertNoViolations("blank"); 52 | } 53 | 54 | @Test 55 | public void shouldNotValidateNonBlankString() { 56 | blankBean.blank = " abcd "; 57 | 58 | validator.assertViolation("blank"); 59 | } 60 | 61 | private static final class BlankBean { 62 | @Blank 63 | private String blank; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/test/java/com/github/britter/beanvalidators/strings/NumericTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators.strings; 17 | 18 | import com.github.britter.beanvalidators.ValidationWrapper; 19 | import org.junit.jupiter.api.BeforeEach; 20 | import org.junit.jupiter.api.Test; 21 | 22 | public final class NumericTest { 23 | 24 | private ValidationWrapper validator; 25 | private NumericBean numericBean; 26 | 27 | @BeforeEach 28 | public void setUp() { 29 | numericBean = new NumericBean(); 30 | validator = new ValidationWrapper<>(numericBean, "must be numeric"); 31 | } 32 | 33 | @Test 34 | public void shouldValidateNumericString() { 35 | numericBean.numeric = "123456"; 36 | 37 | validator.assertNoViolations("numeric"); 38 | } 39 | 40 | @Test 41 | public void shouldValidateNullString() { 42 | numericBean.numeric = null; 43 | 44 | validator.assertNoViolations("numeric"); 45 | } 46 | 47 | @Test 48 | public void shouldValidateBlankString() { 49 | numericBean.numeric = " "; 50 | 51 | validator.assertNoViolations("numeric"); 52 | } 53 | 54 | @Test 55 | public void shouldNotValidateNonNumericString() { 56 | numericBean.numeric = "abc"; 57 | 58 | validator.assertViolation("numeric"); 59 | } 60 | 61 | private static final class NumericBean { 62 | @Numeric 63 | private String numeric; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/testFixtures/java/com/github/britter/beanvalidators/BeanValidatorsAsserts.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators; 17 | 18 | import org.assertj.core.api.Assertions; 19 | 20 | import javax.validation.ConstraintViolation; 21 | 22 | public final class BeanValidatorsAsserts extends Assertions { 23 | 24 | public static ConstraintViolationAssert assertThat(ConstraintViolation actual) { 25 | return new ConstraintViolationAssert(actual); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/testFixtures/java/com/github/britter/beanvalidators/ConstraintViolationAssert.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators; 17 | 18 | import org.assertj.core.api.AbstractAssert; 19 | 20 | import javax.validation.ConstraintViolation; 21 | import java.util.Objects; 22 | 23 | public final class ConstraintViolationAssert extends AbstractAssert, ConstraintViolation> { 24 | 25 | public ConstraintViolationAssert(ConstraintViolation actual) { 26 | super(actual, ConstraintViolationAssert.class); 27 | } 28 | 29 | public static ConstraintViolationAssert assertThat(ConstraintViolation actual) { 30 | return new ConstraintViolationAssert(actual); 31 | } 32 | 33 | public ConstraintViolationAssert hasMessage(String message) { 34 | isNotNull(); 35 | 36 | if (!Objects.equals(actual.getMessage(), message)) { 37 | failWithMessage("Expected violation's message to be <%s> but was <%s>", message, actual.getMessage()); 38 | } 39 | 40 | return this; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/testFixtures/java/com/github/britter/beanvalidators/ValidationWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Benedikt Ritter 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * https://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.britter.beanvalidators; 17 | 18 | import static com.google.common.collect.Iterables.getLast; 19 | import static com.github.britter.beanvalidators.BeanValidatorsAsserts.assertThat; 20 | 21 | import javax.validation.ConstraintViolation; 22 | import javax.validation.Validation; 23 | import javax.validation.Validator; 24 | import java.util.Set; 25 | 26 | public final class ValidationWrapper { 27 | 28 | private static final Validator VALIDATOR = Validation.buildDefaultValidatorFactory().getValidator(); 29 | private T bean; 30 | private String defaultMessage; 31 | 32 | public ValidationWrapper(T bean, String defaultMessage) { 33 | this.bean = bean; 34 | this.defaultMessage = defaultMessage; 35 | } 36 | 37 | public Set> validate(String property) { 38 | return VALIDATOR.validateProperty(bean, property); 39 | } 40 | 41 | public void assertNoViolations(String property) { 42 | Set> violations = validate(property); 43 | 44 | assertThat(violations) 45 | .as("Expected no violations, but got some") 46 | .isEmpty(); 47 | } 48 | 49 | public void assertViolation(String property) { 50 | Set> violations = validate(property); 51 | assertThat(violations) 52 | .as("Wrong violations count") 53 | .hasSize(1); 54 | 55 | ConstraintViolation violation = getLast(violations); 56 | assertThat(violation) 57 | .as("Wrong violation message") 58 | .hasMessage(defaultMessage); 59 | } 60 | } 61 | --------------------------------------------------------------------------------