Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ConstrainedFields(private val classHoldingConstraints: Class<*>) {
* @param path json path of the field
*/
fun withPath(path: String): FieldDescriptor =
withMappedPath(path, path)
withMappedPath(path, beanPropertyNameFromPath(path))

/**
*
Expand All @@ -38,7 +38,10 @@ class ConstrainedFields(private val classHoldingConstraints: Class<*>) {
.value(this.validatorConstraintResolver.resolveForProperty(beanPropertyName, classHoldingConstraints))
)

private fun beanPropertyNameFromPath(jsonPath: String) = jsonPath.substringAfterLast(DOT_NOTATION_DELIMITER)

companion object {
private const val CONSTRAINTS_KEY = "validationConstraints"
private const val DOT_NOTATION_DELIMITER = "."
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,43 @@ import javax.validation.constraints.NotEmpty

internal class ConstrainedFieldsTest {

val fields = ConstrainedFields(SomeWithConstraints::class.java)

@Test
@Suppress("UNCHECKED_CAST")
fun `should resolve constraints`() {
val fields = ConstrainedFields(SomeWithConstraints::class.java)
val descriptor = fields.withPath("nonEmpty")

then(descriptor.attributes).containsKey("validationConstraints")
then((descriptor.attributes["validationConstraints"] as List<Constraint>).map { it.name })
.containsExactly(NotEmpty::class.java.name)
}

@Test
@Suppress("UNCHECKED_CAST")
fun `should resolve one level nested constraints`() {
val fields = ConstrainedFields(SomeWithConstraints::class.java)
val descriptor = fields.withPath("nested.nonEmpty")

then(descriptor.attributes).containsKey("validationConstraints")
then((descriptor.attributes["validationConstraints"] as List<Constraint>).map { it.name })
.containsExactly(NotEmpty::class.java.name)
}

@Test
@Suppress("UNCHECKED_CAST")
fun `should resolve two level nested constraints`() {
val fields = ConstrainedFields(SomeWithConstraints::class.java)
val descriptor = fields.withPath("nested.nested.nonEmpty")

then(descriptor.attributes).containsKey("validationConstraints")
then((descriptor.attributes["validationConstraints"] as List<Constraint>).map { it.name })
.containsExactly(NotEmpty::class.java.name)
}

private data class SomeWithConstraints(
@field:NotEmpty
val nonEmpty: String
val nonEmpty: String,

val nested: SomeWithConstraints?
)
}