Skip to content

Commit 339508d

Browse files
authored
String enum validation (#18)
* Added InStringListByEnum validator * Added tests for InStringListByEnum * Changed package version * Updated version at README * added mode to failedValidationRuleTest
1 parent 5bbc599 commit 339508d

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ repositories {
1212
}
1313

1414
// Append dependency
15-
implementation("com.icerockdev:web-utils:0.6.0")
15+
implementation("com.icerockdev:web-utils:0.6.1")
1616
````
1717

1818
## Library usage

web-utils/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ apply(plugin = "java")
1313
apply(plugin = "kotlin")
1414

1515
group = "com.icerockdev"
16-
version = "0.6.0"
16+
version = "0.6.1"
1717

1818
val sourcesJar by tasks.registering(Jar::class) {
1919
classifier = "sources"
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package com.icerockdev.validation
6+
7+
import javax.validation.Constraint
8+
import javax.validation.ConstraintValidator
9+
import javax.validation.ConstraintValidatorContext
10+
import javax.validation.ReportAsSingleViolation
11+
import kotlin.reflect.KClass
12+
13+
/**
14+
* Validation annotation to validate field than contains in array.
15+
*
16+
* Example, validate field value contains in enum:
17+
* @field:InStringListByEnum(IAvailableStringByEnum = Status::class, message = "Invalid value")
18+
*/
19+
interface IAvailableStringByEnum {
20+
fun getAvailableStringList(listName: String): List<String>
21+
}
22+
23+
@Target(AnnotationTarget.FIELD)
24+
@MustBeDocumented
25+
@Constraint(validatedBy = [InStringListByEnumValidator::class])
26+
@kotlin.annotation.Retention
27+
@ReportAsSingleViolation
28+
annotation class InStringListByEnum(
29+
val message: String = "Not exists in string array",
30+
val groups: Array<KClass<out Any>> = [],
31+
val payload: Array<KClass<out Any>> = [],
32+
val nullable: Boolean = true,
33+
val listName: String = "all",
34+
val IAvailableStringByEnum: KClass<out IAvailableStringByEnum>
35+
)
36+
37+
class InStringListByEnumValidator : ConstraintValidator<InStringListByEnum, String> {
38+
39+
private var annotationValue: List<String> = emptyList()
40+
private var annotationNullable: Boolean = true
41+
42+
override fun initialize(constraintAnnotation: InStringListByEnum) {
43+
this.annotationNullable = constraintAnnotation.nullable
44+
val clazz = constraintAnnotation.IAvailableStringByEnum.java
45+
if (!clazz.isEnum) {
46+
throw IllegalAccessException("Only enum class supported for validation")
47+
}
48+
49+
annotationValue = clazz.enumConstants.first().getAvailableStringList(constraintAnnotation.listName)
50+
}
51+
52+
override fun isValid(fieldValue: String?, constraintContext: ConstraintValidatorContext?): Boolean {
53+
if (fieldValue == null) {
54+
return this.annotationNullable
55+
}
56+
return fieldValue in this.annotationValue
57+
}
58+
}

web-utils/src/test/kotlin/com/icerockdev/api/ToolsTest.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class ToolsTest {
3030
testObj.name = null
3131
testObj.age = 21
3232
testObj.status = 10
33+
testObj.mode = "ACTIVE"
3334
testObj.email = "test@test"
3435
testObj.date = "Invalid date"
3536
testObj.passwordRepeat = "123457"
@@ -48,6 +49,7 @@ class ToolsTest {
4849
testObj.name = null
4950
testObj.age = 21
5051
testObj.status = 10
52+
testObj.mode = "ANYTHING"
5153
testObj.email = "test@test"
5254
testObj.date = "Invalid date"
5355
testObj.passwordRepeat = "123457"
@@ -60,6 +62,7 @@ class ToolsTest {
6062
val expectedErrors = arrayOf(
6163
ErrorDetail(message = "Should it be 10 or 20", code = 0),
6264
ErrorDetail(message = "Should it be 30 or 40", code = 0),
65+
ErrorDetail(message = "Should be ACTIVE or PASSIVE", code = 0),
6366
ErrorDetail(message = "Date must be in YYYY-MM-DD format", code = 0),
6467
ErrorDetail(message = "Name is required", code = 0),
6568
ErrorDetail(message = "email определен в неверном формате", code = 0),
@@ -76,7 +79,7 @@ class ToolsTest {
7679
println(e.message)
7780
}
7881

79-
assertEquals(8, errorsResponse.data.size)
82+
assertEquals(9, errorsResponse.data.size)
8083
assertEquals(422, errorsResponse.status)
8184
assertEquals("Validation Error", errorsResponse.message)
8285
assertEquals(false, errorsResponse.success)

web-utils/src/test/kotlin/com/icerockdev/api/request/TestRequest.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ enum class Status(val value: Int) : IAvailableIntByEnum {
1919
}
2020
}
2121

22+
enum class Mode : IAvailableStringByEnum {
23+
ACTIVE,
24+
PASSIVE;
25+
26+
override fun getAvailableStringList(listName: String): List<String> {
27+
return when (listName) {
28+
"all" -> arrayListOf(ACTIVE.name, PASSIVE.name)
29+
else -> emptyList()
30+
}
31+
}
32+
}
33+
2234
@FieldMatch(message = "The password fields must match", first = "password", second = "passwordRepeat")
2335
class TestRequest(
2436
@field:NotNull(message = "Name is required")
@@ -33,6 +45,8 @@ class TestRequest(
3345
var age: Int = 10,
3446
@field:InIntListByEnum(IAvailableIntByEnum = Status::class, message = "Should it be 30 or 40")
3547
var status: Int = 30,
48+
@field:InStringListByEnum(IAvailableStringByEnum = Mode::class, message = "Should be ACTIVE or PASSIVE")
49+
var mode: String = "ACTIVE",
3650
@field:StrictEmail(message = "Invalid email")
3751
var email: String = "test@test.er",
3852
@field:DateFormat(message = "Date must be in YYYY-MM-DD format", pattern = "YYYY-MM-DD")

0 commit comments

Comments
 (0)