-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from bargergo/string-length-validators
Add validators for strings
- Loading branch information
Showing
27 changed files
with
315 additions
and
27 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/com/papsign/ktor/openapigen/annotations/type/common/ConstraintViolation.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,6 @@ | ||
package com.papsign.ktor.openapigen.annotations.type.common | ||
|
||
import java.lang.Exception | ||
|
||
abstract class ConstraintViolation(defaultMessage: String, message: String = "", cause: Throwable? = null) | ||
: Exception(if (message.isEmpty()) defaultMessage else message, cause) |
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
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
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
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
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
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/com/papsign/ktor/openapigen/annotations/type/string/NotAStringViolation.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,5 @@ | ||
package com.papsign.ktor.openapigen.annotations.type.string | ||
|
||
import com.papsign.ktor.openapigen.annotations.type.common.ConstraintViolation | ||
|
||
class NotAStringViolation(val value: Any?): ConstraintViolation("Constraint violation: $value is not a string") |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/com/papsign/ktor/openapigen/annotations/type/string/length/Length.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,9 @@ | ||
package com.papsign.ktor.openapigen.annotations.type.string.length | ||
|
||
import com.papsign.ktor.openapigen.schema.processor.SchemaProcessorAnnotation | ||
import com.papsign.ktor.openapigen.validation.ValidatorAnnotation | ||
|
||
@Target(AnnotationTarget.TYPE, AnnotationTarget.PROPERTY) | ||
@SchemaProcessorAnnotation(LengthProcessor::class) | ||
@ValidatorAnnotation(LengthProcessor::class) | ||
annotation class Length(val min: Int, val max: Int, val errorMessage: String = "") |
72 changes: 72 additions & 0 deletions
72
...n/com/papsign/ktor/openapigen/annotations/type/string/length/LengthConstraintProcessor.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,72 @@ | ||
package com.papsign.ktor.openapigen.annotations.type.string.length | ||
|
||
import com.papsign.ktor.openapigen.annotations.type.common.ConstraintViolation | ||
import com.papsign.ktor.openapigen.annotations.type.string.NotAStringViolation | ||
import com.papsign.ktor.openapigen.classLogger | ||
import com.papsign.ktor.openapigen.getKType | ||
import com.papsign.ktor.openapigen.model.schema.SchemaModel | ||
import com.papsign.ktor.openapigen.schema.processor.SchemaProcessor | ||
import com.papsign.ktor.openapigen.validation.Validator | ||
import com.papsign.ktor.openapigen.validation.ValidatorBuilder | ||
import kotlin.reflect.KType | ||
import kotlin.reflect.full.withNullability | ||
|
||
abstract class LengthConstraintProcessor<A: Annotation>(): SchemaProcessor<A>, ValidatorBuilder<A> { | ||
|
||
private val log = classLogger() | ||
|
||
val types = listOf(getKType<String>().withNullability(true), getKType<String>().withNullability(false)) | ||
|
||
abstract fun process(model: SchemaModel.SchemaModelLitteral<*>, annotation: A): SchemaModel.SchemaModelLitteral<*> | ||
|
||
abstract fun getConstraint(annotation: A): LengthConstraint | ||
|
||
private class LengthConstraintValidator(private val constraint: LengthConstraint): Validator { | ||
override fun <T> validate(subject: T?): T? { | ||
if (subject is String?) { | ||
val value = subject?.length ?: 0 | ||
if (constraint.min != null) { | ||
if (value < constraint.min) throw LengthConstraintViolation(value, constraint) | ||
} | ||
if (constraint.max != null) { | ||
if (value > constraint.max) throw LengthConstraintViolation(value, constraint) | ||
} | ||
} else { | ||
throw NotAStringViolation(subject) | ||
} | ||
return subject | ||
} | ||
} | ||
|
||
override fun build(type: KType, annotation: A): Validator { | ||
return if (types.contains(type)) { | ||
LengthConstraintValidator(getConstraint(annotation)) | ||
} else { | ||
error("${annotation::class} can only be used on types: $types") | ||
} | ||
} | ||
|
||
override fun process(model: SchemaModel<*>, type: KType, annotation: A): SchemaModel<*> { | ||
return if (model is SchemaModel.SchemaModelLitteral<*> && types.contains(type)) { | ||
process(model, annotation) | ||
} else { | ||
log.warn("${annotation::class} can only be used on types: $types") | ||
model | ||
} | ||
} | ||
} | ||
|
||
data class LengthConstraint(val min: Int? = null, val max: Int? = null, val errorMessage: String) | ||
|
||
class LengthConstraintViolation(val actual: Number?, val constraint: LengthConstraint): ConstraintViolation("Constraint violation: the length of the string should be ${ | ||
{ | ||
val min = "${constraint.min}" | ||
val max = "${constraint.max}" | ||
when { | ||
constraint.min != null && constraint.max != null -> "between $min and $max" | ||
constraint.min != null -> "at least $min" | ||
constraint.max != null -> "at most $max" | ||
else -> "anything" | ||
} | ||
}() | ||
}, but it is $actual", constraint.errorMessage) |
17 changes: 17 additions & 0 deletions
17
...main/kotlin/com/papsign/ktor/openapigen/annotations/type/string/length/LengthProcessor.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,17 @@ | ||
package com.papsign.ktor.openapigen.annotations.type.string.length | ||
|
||
import com.papsign.ktor.openapigen.model.schema.SchemaModel | ||
|
||
object LengthProcessor : LengthConstraintProcessor<Length>() { | ||
override fun process(model: SchemaModel.SchemaModelLitteral<*>, annotation: Length): SchemaModel.SchemaModelLitteral<*> { | ||
@Suppress("UNCHECKED_CAST") | ||
return (model as SchemaModel.SchemaModelLitteral<Any?>).apply { | ||
maxLength = annotation.max | ||
minLength = annotation.min | ||
} | ||
} | ||
|
||
override fun getConstraint(annotation: Length): LengthConstraint { | ||
return LengthConstraint(min = annotation.min, max = annotation.max, errorMessage = annotation.errorMessage) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/com/papsign/ktor/openapigen/annotations/type/string/length/MaxLength.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,9 @@ | ||
package com.papsign.ktor.openapigen.annotations.type.string.length | ||
|
||
import com.papsign.ktor.openapigen.schema.processor.SchemaProcessorAnnotation | ||
import com.papsign.ktor.openapigen.validation.ValidatorAnnotation | ||
|
||
@Target(AnnotationTarget.TYPE, AnnotationTarget.PROPERTY) | ||
@SchemaProcessorAnnotation(MaxLengthProcessor::class) | ||
@ValidatorAnnotation(MaxLengthProcessor::class) | ||
annotation class MaxLength(val value: Int, val errorMessage: String = "") |
16 changes: 16 additions & 0 deletions
16
...n/kotlin/com/papsign/ktor/openapigen/annotations/type/string/length/MaxLengthProcessor.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,16 @@ | ||
package com.papsign.ktor.openapigen.annotations.type.string.length | ||
|
||
import com.papsign.ktor.openapigen.model.schema.SchemaModel | ||
|
||
object MaxLengthProcessor : LengthConstraintProcessor<MaxLength>() { | ||
override fun process(model: SchemaModel.SchemaModelLitteral<*>, annotation: MaxLength): SchemaModel.SchemaModelLitteral<*> { | ||
@Suppress("UNCHECKED_CAST") | ||
return (model as SchemaModel.SchemaModelLitteral<Any?>).apply { | ||
maxLength = annotation.value | ||
} | ||
} | ||
|
||
override fun getConstraint(annotation: MaxLength): LengthConstraint { | ||
return LengthConstraint(max = annotation.value, errorMessage = annotation.errorMessage) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/com/papsign/ktor/openapigen/annotations/type/string/length/MinLength.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,9 @@ | ||
package com.papsign.ktor.openapigen.annotations.type.string.length | ||
|
||
import com.papsign.ktor.openapigen.schema.processor.SchemaProcessorAnnotation | ||
import com.papsign.ktor.openapigen.validation.ValidatorAnnotation | ||
|
||
@Target(AnnotationTarget.TYPE, AnnotationTarget.PROPERTY) | ||
@SchemaProcessorAnnotation(MinLengthProcessor::class) | ||
@ValidatorAnnotation(MinLengthProcessor::class) | ||
annotation class MinLength(val value: Int, val errorMessage: String = "") |
16 changes: 16 additions & 0 deletions
16
...n/kotlin/com/papsign/ktor/openapigen/annotations/type/string/length/MinLengthProcessor.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,16 @@ | ||
package com.papsign.ktor.openapigen.annotations.type.string.length | ||
|
||
import com.papsign.ktor.openapigen.model.schema.SchemaModel | ||
|
||
object MinLengthProcessor : LengthConstraintProcessor<MinLength>() { | ||
override fun process(model: SchemaModel.SchemaModelLitteral<*>, annotation: MinLength): SchemaModel.SchemaModelLitteral<*> { | ||
@Suppress("UNCHECKED_CAST") | ||
return (model as SchemaModel.SchemaModelLitteral<Any?>).apply { | ||
minLength = annotation.value | ||
} | ||
} | ||
|
||
override fun getConstraint(annotation: MinLength): LengthConstraint { | ||
return LengthConstraint(min = annotation.value, errorMessage = annotation.errorMessage) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...n/kotlin/com/papsign/ktor/openapigen/annotations/type/string/pattern/RegularExpression.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,9 @@ | ||
package com.papsign.ktor.openapigen.annotations.type.string.pattern | ||
|
||
import com.papsign.ktor.openapigen.schema.processor.SchemaProcessorAnnotation | ||
import com.papsign.ktor.openapigen.validation.ValidatorAnnotation | ||
|
||
@Target(AnnotationTarget.TYPE, AnnotationTarget.PROPERTY) | ||
@SchemaProcessorAnnotation(RegularExpressionProcessor::class) | ||
@ValidatorAnnotation(RegularExpressionProcessor::class) | ||
annotation class RegularExpression(@org.intellij.lang.annotations.Language("RegExp") val pattern: String, val errorMessage: String = "") |
58 changes: 58 additions & 0 deletions
58
...n/ktor/openapigen/annotations/type/string/pattern/RegularExpressionConstraintProcessor.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,58 @@ | ||
package com.papsign.ktor.openapigen.annotations.type.string.pattern | ||
|
||
import com.papsign.ktor.openapigen.annotations.type.common.ConstraintViolation | ||
import com.papsign.ktor.openapigen.annotations.type.string.NotAStringViolation | ||
import com.papsign.ktor.openapigen.classLogger | ||
import com.papsign.ktor.openapigen.getKType | ||
import com.papsign.ktor.openapigen.model.schema.SchemaModel | ||
import com.papsign.ktor.openapigen.schema.processor.SchemaProcessor | ||
import com.papsign.ktor.openapigen.validation.Validator | ||
import com.papsign.ktor.openapigen.validation.ValidatorBuilder | ||
import kotlin.reflect.KType | ||
import kotlin.reflect.full.withNullability | ||
|
||
abstract class RegularExpressionConstraintProcessor<A: Annotation>(): SchemaProcessor<A>, ValidatorBuilder<A> { | ||
|
||
private val log = classLogger() | ||
|
||
val types = listOf(getKType<String>().withNullability(true), getKType<String>().withNullability(false)) | ||
|
||
abstract fun process(model: SchemaModel.SchemaModelLitteral<*>, annotation: A): SchemaModel.SchemaModelLitteral<*> | ||
|
||
abstract fun getConstraint(annotation: A): RegularExpressionConstraint | ||
|
||
private class RegularExpressionConstraintValidator(private val constraint: RegularExpressionConstraint): Validator { | ||
override fun <T> validate(subject: T?): T? { | ||
if (subject is String?) { | ||
if (subject == null || !constraint.pattern.toRegex().containsMatchIn(subject)) { | ||
throw RegularExpressionConstraintViolation(subject, constraint) | ||
} | ||
} else { | ||
throw NotAStringViolation(subject) | ||
} | ||
return subject | ||
} | ||
} | ||
|
||
override fun build(type: KType, annotation: A): Validator { | ||
return if (types.contains(type)) { | ||
RegularExpressionConstraintValidator(getConstraint(annotation)) | ||
} else { | ||
error("${annotation::class} can only be used on types: $types") | ||
} | ||
} | ||
|
||
override fun process(model: SchemaModel<*>, type: KType, annotation: A): SchemaModel<*> { | ||
return if (model is SchemaModel.SchemaModelLitteral<*> && types.contains(type)) { | ||
process(model, annotation) | ||
} else { | ||
log.warn("${annotation::class} can only be used on types: $types") | ||
model | ||
} | ||
} | ||
} | ||
|
||
data class RegularExpressionConstraint(val pattern: String, val errorMessage: String) | ||
|
||
class RegularExpressionConstraintViolation(val actual: String?, val constraint: RegularExpressionConstraint): ConstraintViolation("Constraint violation: the string " + | ||
"'$actual' does not match the regular expression ${constraint.pattern}", constraint.errorMessage) |
16 changes: 16 additions & 0 deletions
16
...com/papsign/ktor/openapigen/annotations/type/string/pattern/RegularExpressionProcessor.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,16 @@ | ||
package com.papsign.ktor.openapigen.annotations.type.string.pattern | ||
|
||
import com.papsign.ktor.openapigen.model.schema.SchemaModel | ||
|
||
object RegularExpressionProcessor : RegularExpressionConstraintProcessor<RegularExpression>() { | ||
override fun process(model: SchemaModel.SchemaModelLitteral<*>, annotation: RegularExpression): SchemaModel.SchemaModelLitteral<*> { | ||
@Suppress("UNCHECKED_CAST") | ||
return (model as SchemaModel.SchemaModelLitteral<Any?>).apply { | ||
pattern = annotation.pattern | ||
} | ||
} | ||
|
||
override fun getConstraint(annotation: RegularExpression): RegularExpressionConstraint { | ||
return RegularExpressionConstraint(annotation.pattern, annotation.errorMessage) | ||
} | ||
} |
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.