refactor: use builtin UniqueElements validator

This commit is contained in:
Gauthier Roebroeck 2020-08-21 11:35:02 +08:00
parent 539ea6a7d8
commit d8db46c589
7 changed files with 18 additions and 45 deletions

View file

@ -1,23 +0,0 @@
package org.gotson.komga.infrastructure.validation
import javax.validation.Constraint
import javax.validation.ConstraintValidator
import javax.validation.ConstraintValidatorContext
import kotlin.reflect.KClass
@Constraint(validatedBy = [UniqueValidator::class])
@Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.FIELD, AnnotationTarget.PROPERTY_GETTER)
@Retention(AnnotationRetention.RUNTIME)
annotation class Unique(
val message: String = "Must contain unique values",
val groups: Array<KClass<out Any>> = [],
val payload: Array<KClass<out Any>> = []
)
class UniqueValidator : ConstraintValidator<Unique, List<*>> {
override fun isValid(value: List<*>?, context: ConstraintValidatorContext?): Boolean {
if (value == null) return true
return value.distinct().size == value.size
}
}

View file

@ -1,11 +1,11 @@
package org.gotson.komga.interfaces.rest.dto
import org.gotson.komga.infrastructure.validation.Unique
import org.hibernate.validator.constraints.UniqueElements
import javax.validation.constraints.NotBlank
import javax.validation.constraints.NotEmpty
data class CollectionCreationDto(
@get:NotBlank val name: String,
val ordered: Boolean,
@get:NotEmpty @get:Unique val seriesIds: List<String>
@get:NotEmpty @get:UniqueElements val seriesIds: List<String>
)

View file

@ -2,10 +2,10 @@ package org.gotson.komga.interfaces.rest.dto
import org.gotson.komga.infrastructure.validation.NullOrNotBlank
import org.gotson.komga.infrastructure.validation.NullOrNotEmpty
import org.gotson.komga.infrastructure.validation.Unique
import org.hibernate.validator.constraints.UniqueElements
data class CollectionUpdateDto(
@get:NullOrNotBlank val name: String?,
val ordered: Boolean?,
@get:NullOrNotEmpty @get:Unique val seriesIds: List<String>?
@get:NullOrNotEmpty @get:UniqueElements val seriesIds: List<String>?
)

View file

@ -1,10 +1,10 @@
package org.gotson.komga.interfaces.rest.dto
import org.gotson.komga.infrastructure.validation.Unique
import org.hibernate.validator.constraints.UniqueElements
import javax.validation.constraints.NotBlank
import javax.validation.constraints.NotEmpty
data class ReadListCreationDto(
@get:NotBlank val name: String,
@get:NotEmpty @get:Unique val bookIds: List<String>
@get:NotEmpty @get:UniqueElements val bookIds: List<String>
)

View file

@ -2,9 +2,9 @@ package org.gotson.komga.interfaces.rest.dto
import org.gotson.komga.infrastructure.validation.NullOrNotBlank
import org.gotson.komga.infrastructure.validation.NullOrNotEmpty
import org.gotson.komga.infrastructure.validation.Unique
import org.hibernate.validator.constraints.UniqueElements
data class ReadListUpdateDto(
@get:NullOrNotBlank val name: String?,
@get:NullOrNotEmpty @get:Unique val bookIds: List<String>?
@get:NullOrNotEmpty @get:UniqueElements val bookIds: List<String>?
)

View file

@ -170,7 +170,7 @@ class ReadListControllerTest(
@WithMockCustomUser
fun `given non-admin user when creating read list then return forbidden`() {
val jsonString = """
{"name":"readlist","bookIds":[3]}
{"name":"readlist","bookIds":["3"]}
""".trimIndent()
mockMvc.post("/api/v1/readlists") {
@ -204,7 +204,7 @@ class ReadListControllerTest(
makeReadLists()
val jsonString = """
{"name":"Lib1","bookIds":[${booksLibrary1.first().id}]}
{"name":"Lib1","bookIds":["${booksLibrary1.first().id}"]}
""".trimIndent()
mockMvc.post("/api/v1/readlists") {
@ -218,10 +218,8 @@ class ReadListControllerTest(
@Test
@WithMockCustomUser(roles = [ROLE_ADMIN])
fun `given read list with duplicate bookIds when creating read list then return bad request`() {
makeReadLists()
val jsonString = """
{"name":"Lib1","bookIds":[${booksLibrary1.first().id},${booksLibrary1.first().id}]}
{"name":"Lib1","bookIds":["${booksLibrary1.first().id}","${booksLibrary1.first().id}"]}
""".trimIndent()
mockMvc.post("/api/v1/readlists") {
@ -239,7 +237,7 @@ class ReadListControllerTest(
@WithMockCustomUser
fun `given non-admin user when updating read list then return forbidden`() {
val jsonString = """
{"name":"readlist","bookIds":[3]}
{"name":"readlist","bookIds":["3"]}
""".trimIndent()
mockMvc.patch("/api/v1/readlists/5") {
@ -295,7 +293,7 @@ class ReadListControllerTest(
fun `given existing read list when updating read list with duplicate bookIds then return bad request`() {
makeReadLists()
val jsonString = """{"bookIds":[${booksLibrary1.first().id},${booksLibrary1.first().id}]}"""
val jsonString = """{"bookIds":["${booksLibrary1.first().id}","${booksLibrary1.first().id}"]}"""
mockMvc.patch("/api/v1/readlists/${rlLib1.id}") {
contentType = MediaType.APPLICATION_JSON

View file

@ -165,7 +165,7 @@ class SeriesCollectionControllerTest(
@WithMockCustomUser
fun `given non-admin user when creating collection then return forbidden`() {
val jsonString = """
{"name":"collection","ordered":false,"seriesIds":[3]}
{"name":"collection","ordered":false,"seriesIds":["3"]}
""".trimIndent()
mockMvc.post("/api/v1/collections") {
@ -200,7 +200,7 @@ class SeriesCollectionControllerTest(
makeCollections()
val jsonString = """
{"name":"Lib1","ordered":false,"seriesIds":[${seriesLibrary1.first().id}]}
{"name":"Lib1","ordered":false,"seriesIds":["${seriesLibrary1.first().id}"]}
""".trimIndent()
mockMvc.post("/api/v1/collections") {
@ -214,10 +214,8 @@ class SeriesCollectionControllerTest(
@Test
@WithMockCustomUser(roles = [ROLE_ADMIN])
fun `given collection with duplicate seriesIds when creating collection then return bad request`() {
makeCollections()
val jsonString = """
{"name":"Lib1","ordered":false,"seriesIds":[${seriesLibrary1.first().id},${seriesLibrary1.first().id}]}
{"name":"Lib1","ordered":false,"seriesIds":["${seriesLibrary1.first().id}","${seriesLibrary1.first().id}"]}
""".trimIndent()
mockMvc.post("/api/v1/collections") {
@ -235,7 +233,7 @@ class SeriesCollectionControllerTest(
@WithMockCustomUser
fun `given non-admin user when updating collection then return forbidden`() {
val jsonString = """
{"name":"collection","ordered":false,"seriesIds":[3]}
{"name":"collection","ordered":false,"seriesIds":["3"]}
""".trimIndent()
mockMvc.patch("/api/v1/collections/5") {
@ -292,7 +290,7 @@ class SeriesCollectionControllerTest(
fun `given existing collection when updating collection with duplicate seriesIds then return bad request`() {
makeCollections()
val jsonString = """{"seriesIds":[${seriesLibrary1.first().id},${seriesLibrary1.first().id}]}"""
val jsonString = """{"seriesIds":["${seriesLibrary1.first().id}","${seriesLibrary1.first().id}"]}"""
mockMvc.patch("/api/v1/collections/${colLib1.id}") {
contentType = MediaType.APPLICATION_JSON