Skip to content

Commit

Permalink
Adjust oneofstrings validator to improve formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mattupstate committed Apr 12, 2024
1 parent 4f514e9 commit 302f7fb
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 8 deletions.
1 change: 1 addition & 0 deletions acme-lib/acme-lib-validation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ dependencies {
implementation(libs.com.googlecode.libphonenumber)
api(libs.jakarta.validation.jakarta.validation.api)
testImplementation(libs.io.kotest.kotest.runner.junit5)
testImplementation(libs.org.hibernate.validator.hibernate.validator)
testRuntimeOnly(libs.org.glassfish.jakarta.el)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import kotlin.reflect.KClass
@MustBeDocumented
@Constraint(validatedBy = [OneOfStringsValidator::class])
annotation class OneOfStrings(
val values: Array<String> = [],
val values: String = "",
val delimiter: String = ", ",
val message: String = "{com.acme.validation.OneOfStrings.message}",
val groups: Array<KClass<*>> = [],
val payload: Array<KClass<out Payload>> = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ class OneOfStringsValidator : ConstraintValidator<OneOfStrings, String> {
}

override fun isValid(value: String?, context: ConstraintValidatorContext?): Boolean =
annotation!!.values.contains(value)
annotation!!.values.split(annotation!!.delimiter).contains(value)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.acme.validation

import io.kotest.core.spec.style.ShouldSpec
import io.kotest.matchers.collections.shouldHaveSize
import jakarta.validation.Validation
import jakarta.validation.ValidatorFactory

class OneOfStringsTest: ShouldSpec({
data class FakeBean(
@field:OneOfStrings(values = "Doctor, Nurse, Pediatrician")
val title: String?
)

should("result in errors for invalid value") {
val validator = Validation.buildDefaultValidatorFactory().validator
val results = validator.validate(FakeBean("Doc"))
results shouldHaveSize 1
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
com.acme.validation.PhoneNumber.message=invalid phone number
com.acme.validation.OneOfStrings.message=must be one of: {values}
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ class SchedulingClientCollectionApiSpec : ShouldSpec({
"path": "/emailAddresses"
},
{
"message": "must be one of [MALE, FEMALE, TRANSGENDER, NON_BINARY, OTHER, UNKNOWN]",
"message": "must be one of MALE, FEMALE, TRANSGENDER, NON_BINARY, OTHER, UNKNOWN",
"path": "/gender"
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ data class CreateAppointmentCommandRequest(
val practiceId: String? = null,

@field:NotNull
@field:OneOfStrings(["SCHEDULED", "UNATTENDED", "ATTENDED", "CANCELED"])
@field:OneOfStrings(values = "SCHEDULED, UNATTENDED, ATTENDED, CANCELED")
val state: String?,

@field:NotNull
Expand All @@ -77,7 +77,7 @@ data class CreateClientCommandRequest(
val name: HumanName? = null,

@field:NotNull
@field:OneOfStrings(["MALE", "FEMALE", "TRANSGENDER", "NON_BINARY", "OTHER", "UNKNOWN"])
@field:OneOfStrings(values = "MALE, FEMALE, TRANSGENDER, NON_BINARY, OTHER, UNKNOWN")
val gender: String? = null,

@field:NotNull
Expand Down Expand Up @@ -111,7 +111,7 @@ data class CreatePractitionerCommandRequest(
val name: HumanName? = null,

@field:NotNull
@field:OneOfStrings(["MALE", "FEMALE", "TRANSGENDER", "NON_BINARY", "OTHER", "UNKNOWN"])
@field:OneOfStrings(values = "MALE, FEMALE, TRANSGENDER, NON_BINARY, OTHER, UNKNOWN")
val gender: String? = null,

@field:NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CreateClientCommandRequestTest : ShouldSpec({
"/gender" to setOf(
"" +
"must not be null",
"must be one of [MALE, FEMALE, TRANSGENDER, NON_BINARY, OTHER, UNKNOWN]"
"must be one of MALE, FEMALE, TRANSGENDER, NON_BINARY, OTHER, UNKNOWN"
),
"/phoneNumbers" to setOf("must not be null"),
"/emailAddresses" to setOf("must not be null"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CreatePractitionerCommandRequestTest : ShouldSpec({
"/gender" to setOf("must not be null"),
"/gender" to setOf(
"must not be null",
"must be one of [MALE, FEMALE, TRANSGENDER, NON_BINARY, OTHER, UNKNOWN]"
"must be one of MALE, FEMALE, TRANSGENDER, NON_BINARY, OTHER, UNKNOWN"
),
"/phoneNumbers" to setOf("must not be null"),
"/emailAddresses" to setOf("must not be null"),
Expand Down

0 comments on commit 302f7fb

Please sign in to comment.