Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Strongly Typed vs. JavaX Validation: optimization
- Loading branch information
Showing
30 changed files
with
700 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 3 additions & 20 deletions
23
...ngly-typed-vs-javax-validation/src/main/kotlin/example/javaxvalidation/validation/City.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,16 @@ | ||
package example.javaxvalidation.validation | ||
|
||
import javax.validation.Constraint | ||
import javax.validation.ConstraintValidator | ||
import javax.validation.ConstraintValidatorContext | ||
import javax.validation.constraints.NotBlank | ||
import javax.validation.constraints.Size | ||
import kotlin.reflect.KClass | ||
|
||
@NotBlank // constraint #1 | ||
@Size(max = 50) // constraint #2 | ||
@Constraint(validatedBy = [City.Validator::class]) | ||
@DoesNotContainAnyPossibleInjectionCharacters // constraint #3 | ||
@Constraint(validatedBy = []) | ||
annotation class City( | ||
val message: String = "Invalid City", | ||
val groups: Array<KClass<out Any>> = [], | ||
val payload: Array<KClass<out Any>> = [] | ||
) { | ||
|
||
class Validator : ConstraintValidator<City, CharSequence?> { | ||
private val illegalCharacters = possibleInjectionCharacters | ||
private val illegalCharactersMessage = | ||
"contains one of these illegal characters: ${illegalCharacters.joinToString(separator = " ")}" | ||
|
||
override fun isValid(value: CharSequence?, context: ConstraintValidatorContext): Boolean { | ||
if (value containsAny illegalCharacters) { // constraint #3 | ||
context.disableDefaultConstraintViolation() | ||
context.buildConstraintViolationWithTemplate(illegalCharactersMessage).addConstraintViolation() | ||
return false | ||
} | ||
return true | ||
} | ||
} | ||
} | ||
) |
43 changes: 43 additions & 0 deletions
43
...kotlin/example/javaxvalidation/validation/DoesNotContainAnyPossibleInjectionCharacters.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package example.javaxvalidation.validation | ||
|
||
import javax.validation.Constraint | ||
import javax.validation.ConstraintValidator | ||
import javax.validation.ConstraintValidatorContext | ||
import kotlin.reflect.KClass | ||
|
||
internal val possibleInjectionCharacters = setOf('$', '<', '>', '"', ';', '\'') // not complete ... | ||
|
||
@Constraint(validatedBy = [DoesNotContainAnyPossibleInjectionCharacters.Validator::class]) | ||
annotation class DoesNotContainAnyPossibleInjectionCharacters( | ||
val whitelist: CharArray = [], | ||
val message: String = "Invalid First Name", | ||
val groups: Array<KClass<out Any>> = [], | ||
val payload: Array<KClass<out Any>> = [] | ||
) { | ||
|
||
class Validator : ConstraintValidator<DoesNotContainAnyPossibleInjectionCharacters, CharSequence?> { | ||
|
||
private lateinit var illegalCharacters: Set<Char> | ||
|
||
override fun initialize(constraintAnnotation: DoesNotContainAnyPossibleInjectionCharacters) { | ||
illegalCharacters = possibleInjectionCharacters - constraintAnnotation.whitelist.toSet() | ||
} | ||
|
||
override fun isValid(value: CharSequence?, context: ConstraintValidatorContext): Boolean { | ||
if (value containsAny illegalCharacters) { | ||
context.disableDefaultConstraintViolation() | ||
context.buildConstraintViolationWithTemplate(message()).addConstraintViolation() | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
private infix fun CharSequence?.containsAny(characters: Iterable<Char>): Boolean = | ||
this?.any { char -> characters.contains(char) } ?: false | ||
|
||
private fun message() = | ||
"contains one of these illegal characters: ${illegalCharacters.joinToString(separator = " ")}" | ||
|
||
} | ||
|
||
} |
23 changes: 3 additions & 20 deletions
23
...typed-vs-javax-validation/src/main/kotlin/example/javaxvalidation/validation/FirstName.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,16 @@ | ||
package example.javaxvalidation.validation | ||
|
||
import javax.validation.Constraint | ||
import javax.validation.ConstraintValidator | ||
import javax.validation.ConstraintValidatorContext | ||
import javax.validation.constraints.NotBlank | ||
import javax.validation.constraints.Size | ||
import kotlin.reflect.KClass | ||
|
||
@NotBlank // constraint #1 | ||
@Size(max = 50) // constraint #2 | ||
@Constraint(validatedBy = [FirstName.Validator::class]) | ||
@DoesNotContainAnyPossibleInjectionCharacters(whitelist = ['\'']) // constraint #3 | ||
@Constraint(validatedBy = []) | ||
annotation class FirstName( | ||
val message: String = "Invalid First Name", | ||
val groups: Array<KClass<out Any>> = [], | ||
val payload: Array<KClass<out Any>> = [] | ||
) { | ||
|
||
class Validator : ConstraintValidator<FirstName, CharSequence?> { | ||
private val illegalCharacters = possibleInjectionCharacters - '\'' | ||
private val illegalCharactersMessage = | ||
"contains one of these illegal characters: ${illegalCharacters.joinToString(separator = " ")}" | ||
|
||
override fun isValid(value: CharSequence?, context: ConstraintValidatorContext): Boolean { | ||
if (value containsAny illegalCharacters) { // constraint #3 | ||
context.disableDefaultConstraintViolation() | ||
context.buildConstraintViolationWithTemplate(illegalCharactersMessage).addConstraintViolation() | ||
return false | ||
} | ||
return true | ||
} | ||
} | ||
} | ||
) |
23 changes: 3 additions & 20 deletions
23
...-typed-vs-javax-validation/src/main/kotlin/example/javaxvalidation/validation/LastName.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,16 @@ | ||
package example.javaxvalidation.validation | ||
|
||
import javax.validation.Constraint | ||
import javax.validation.ConstraintValidator | ||
import javax.validation.ConstraintValidatorContext | ||
import javax.validation.constraints.NotBlank | ||
import javax.validation.constraints.Size | ||
import kotlin.reflect.KClass | ||
|
||
@NotBlank // constraint #1 | ||
@Size(max = 50) // constraint #2 | ||
@Constraint(validatedBy = [LastName.Validator::class]) | ||
@DoesNotContainAnyPossibleInjectionCharacters(whitelist = ['\'']) // constraint #3 | ||
@Constraint(validatedBy = []) | ||
annotation class LastName( | ||
val message: String = "Invalid Last Name", | ||
val groups: Array<KClass<out Any>> = [], | ||
val payload: Array<KClass<out Any>> = [] | ||
) { | ||
|
||
class Validator : ConstraintValidator<LastName, CharSequence?> { | ||
private val illegalCharacters = possibleInjectionCharacters - '\'' | ||
private val illegalCharactersMessage = | ||
"contains one of these illegal characters: ${illegalCharacters.joinToString(separator = " ")}" | ||
|
||
override fun isValid(value: CharSequence?, context: ConstraintValidatorContext): Boolean { | ||
if (value containsAny illegalCharacters) { // constraint #3 | ||
context.disableDefaultConstraintViolation() | ||
context.buildConstraintViolationWithTemplate(illegalCharactersMessage).addConstraintViolation() | ||
return false | ||
} | ||
return true | ||
} | ||
} | ||
} | ||
) |
23 changes: 3 additions & 20 deletions
23
...ly-typed-vs-javax-validation/src/main/kotlin/example/javaxvalidation/validation/Street.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,16 @@ | ||
package example.javaxvalidation.validation | ||
|
||
import javax.validation.Constraint | ||
import javax.validation.ConstraintValidator | ||
import javax.validation.ConstraintValidatorContext | ||
import javax.validation.constraints.NotBlank | ||
import javax.validation.constraints.Size | ||
import kotlin.reflect.KClass | ||
|
||
@NotBlank // constraint #1 | ||
@Size(max = 100) // constraint #2 | ||
@Constraint(validatedBy = [Street.Validator::class]) | ||
@DoesNotContainAnyPossibleInjectionCharacters // constraint #3 | ||
@Constraint(validatedBy = []) | ||
annotation class Street( | ||
val message: String = "Invalid Street", | ||
val groups: Array<KClass<out Any>> = [], | ||
val payload: Array<KClass<out Any>> = [] | ||
) { | ||
|
||
class Validator : ConstraintValidator<Street, CharSequence?> { | ||
private val illegalCharacters = possibleInjectionCharacters | ||
private val illegalCharactersMessage = | ||
"contains one of these illegal characters: ${illegalCharacters.joinToString(separator = " ")}" | ||
|
||
override fun isValid(value: CharSequence?, context: ConstraintValidatorContext): Boolean { | ||
if (value containsAny illegalCharacters) { // constraint #3 | ||
context.disableDefaultConstraintViolation() | ||
context.buildConstraintViolationWithTemplate(illegalCharactersMessage).addConstraintViolation() | ||
return false | ||
} | ||
return true | ||
} | ||
} | ||
} | ||
) |
23 changes: 3 additions & 20 deletions
23
...y-typed-vs-javax-validation/src/main/kotlin/example/javaxvalidation/validation/ZipCode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,16 @@ | ||
package example.javaxvalidation.validation | ||
|
||
import javax.validation.Constraint | ||
import javax.validation.ConstraintValidator | ||
import javax.validation.ConstraintValidatorContext | ||
import javax.validation.constraints.NotBlank | ||
import javax.validation.constraints.Size | ||
import kotlin.reflect.KClass | ||
|
||
@NotBlank // constraint #1 | ||
@Size(max = 15) // constraint #2 | ||
@Constraint(validatedBy = [ZipCode.Validator::class]) | ||
@DoesNotContainAnyPossibleInjectionCharacters // constraint #3 | ||
@Constraint(validatedBy = []) | ||
annotation class ZipCode( | ||
val message: String = "Invalid Zip Code", | ||
val groups: Array<KClass<out Any>> = [], | ||
val payload: Array<KClass<out Any>> = [] | ||
) { | ||
|
||
class Validator : ConstraintValidator<ZipCode, CharSequence?> { | ||
private val illegalCharacters = possibleInjectionCharacters | ||
private val illegalCharactersMessage = | ||
"contains one of these illegal characters: ${illegalCharacters.joinToString(separator = " ")}" | ||
|
||
override fun isValid(value: CharSequence?, context: ConstraintValidatorContext): Boolean { | ||
if (value containsAny illegalCharacters) { // constraint #3 | ||
context.disableDefaultConstraintViolation() | ||
context.buildConstraintViolationWithTemplate(illegalCharactersMessage).addConstraintViolation() | ||
return false | ||
} | ||
return true | ||
} | ||
} | ||
} | ||
) |
3 changes: 0 additions & 3 deletions
3
...typed-vs-javax-validation/src/main/kotlin/example/javaxvalidation/validation/constants.kt
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
...typed-vs-javax-validation/src/main/kotlin/example/javaxvalidation/validation/functions.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.