mirror of
https://github.com/Dannecron/spring-boot-demo.git
synced 2025-12-26 00:32:34 +03:00
refactor validator, add tests
This commit is contained in:
@@ -10,6 +10,7 @@ import com.example.demo.services.database.product.ProductService
|
||||
import com.example.demo.services.database.product.ProductServiceImpl
|
||||
import com.example.demo.services.kafka.Producer
|
||||
import com.example.demo.services.validation.SchemaValidator
|
||||
import com.example.demo.services.validation.SchemaValidatorImp
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.fasterxml.jackson.databind.SerializationFeature
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
|
||||
@@ -53,7 +54,7 @@ class AppConfig(
|
||||
fun cityService(@Autowired cityRepository: CityRepository): CityService = CityServiceImpl(cityRepository)
|
||||
|
||||
@Bean
|
||||
fun schemaValidator(): SchemaValidator = SchemaValidator(kafkaProperties.validation.schema)
|
||||
fun schemaValidator(): SchemaValidator = SchemaValidatorImp(kafkaProperties.validation.schema)
|
||||
|
||||
@Bean
|
||||
fun otlpHttpSpanExporter(@Value("\${tracing.url}") url: String): OtlpHttpSpanExporter {
|
||||
|
||||
@@ -2,43 +2,9 @@ package com.example.demo.services.validation
|
||||
|
||||
import com.example.demo.services.validation.exceptions.ElementNotValidException
|
||||
import com.example.demo.services.validation.exceptions.SchemaNotFoundException
|
||||
import io.github.optimumcode.json.schema.JsonSchema
|
||||
import io.github.optimumcode.json.schema.ValidationError
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import org.springframework.util.ResourceUtils
|
||||
|
||||
class SchemaValidator(
|
||||
private val schemaMap: Map<String, String>,
|
||||
) {
|
||||
private val loadedSchema: MutableMap<String, String> = mutableMapOf()
|
||||
|
||||
fun validate(schemaName: String, value: JsonElement) {
|
||||
|
||||
val schema = JsonSchema.fromDefinition(
|
||||
getSchema(schemaName),
|
||||
)
|
||||
|
||||
val errors = mutableListOf<ValidationError>()
|
||||
|
||||
val valid = schema.validate(value, errors::add)
|
||||
if (!valid) {
|
||||
throw ElementNotValidException(errors)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getSchema(schemaName: String): String {
|
||||
val loaded = loadedSchema[schemaName]
|
||||
if (loaded != null) {
|
||||
return loaded
|
||||
}
|
||||
|
||||
val schemaFile = schemaMap[schemaName]
|
||||
?: throw SchemaNotFoundException()
|
||||
|
||||
val schema = ResourceUtils.getFile("classpath:json-schemas/$schemaFile")
|
||||
.readText(Charsets.UTF_8)
|
||||
loadedSchema[schemaName] = schema
|
||||
|
||||
return schema
|
||||
}
|
||||
interface SchemaValidator {
|
||||
@Throws(ElementNotValidException::class, SchemaNotFoundException::class)
|
||||
fun validate(schemaName: String, value: JsonElement)
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.example.demo.services.validation
|
||||
|
||||
import com.example.demo.services.validation.exceptions.ElementNotValidException
|
||||
import com.example.demo.services.validation.exceptions.SchemaNotFoundException
|
||||
import io.github.optimumcode.json.schema.JsonSchema
|
||||
import io.github.optimumcode.json.schema.ValidationError
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import org.springframework.util.ResourceUtils
|
||||
|
||||
class SchemaValidatorImp(
|
||||
private val schemaMap: Map<String, String>,
|
||||
): SchemaValidator {
|
||||
private val loadedSchema: MutableMap<String, String> = mutableMapOf()
|
||||
|
||||
override fun validate(schemaName: String, value: JsonElement) {
|
||||
|
||||
val schema = JsonSchema.fromDefinition(
|
||||
getSchema(schemaName),
|
||||
)
|
||||
|
||||
val errors = mutableListOf<ValidationError>()
|
||||
|
||||
val valid = schema.validate(value, errors::add)
|
||||
if (!valid) {
|
||||
throw ElementNotValidException(errors)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getSchema(schemaName: String): String {
|
||||
val loaded = loadedSchema[schemaName]
|
||||
if (loaded != null) {
|
||||
return loaded
|
||||
}
|
||||
|
||||
val schemaFile = schemaMap[schemaName]
|
||||
?: throw SchemaNotFoundException()
|
||||
|
||||
val schema = ResourceUtils.getFile("classpath:json-schemas/$schemaFile")
|
||||
.readText(Charsets.UTF_8)
|
||||
loadedSchema[schemaName] = schema
|
||||
|
||||
return schema
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
"schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "event sync product",
|
||||
"type": "object",
|
||||
"required": ["id", "guid"],
|
||||
"required": ["id", "guid", "name", "price", "createdAt"],
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "number"
|
||||
@@ -19,8 +19,8 @@
|
||||
{ "type": "null" }
|
||||
]
|
||||
},
|
||||
"number": {
|
||||
"type": "string"
|
||||
"price": {
|
||||
"type": "number"
|
||||
},
|
||||
"createdAt": {
|
||||
"type": "string"
|
||||
|
||||
Reference in New Issue
Block a user