Skip to content

Commit b1c8951

Browse files
authored
build(deps): bump networknt json-schema-validator from v1 to v2.0.1 (#1486)
* build(deps): bump networknt json-schema-validator from v1 to v2.0.1 * style: format and optimize class properties based on review comments
1 parent 73d5ff3 commit b1c8951

2 files changed

Lines changed: 33 additions & 47 deletions

File tree

agent_sdks/kotlin/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ repositories {
3939

4040
dependencies {
4141
api("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
42-
implementation("com.networknt:json-schema-validator:1.5.1")
42+
implementation("com.networknt:json-schema-validator:2.0.1")
4343
implementation("com.fasterxml.jackson.core:jackson-databind:2.17.2")
4444

4545
// Core Dependencies

agent_sdks/kotlin/src/main/kotlin/com/google/a2ui/schema/Validator.kt

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
package com.google.a2ui.schema
1818

19-
import com.fasterxml.jackson.databind.ObjectMapper
20-
import com.networknt.schema.JsonSchema
21-
import com.networknt.schema.JsonSchemaFactory
22-
import com.networknt.schema.SchemaValidatorsConfig
23-
import com.networknt.schema.SpecVersion
19+
import com.networknt.schema.InputFormat
20+
import com.networknt.schema.Schema
21+
import com.networknt.schema.SchemaRegistry
22+
import com.networknt.schema.SchemaRegistryConfig
23+
import com.networknt.schema.dialect.Dialects
2424
import kotlinx.serialization.json.Json
2525
import kotlinx.serialization.json.JsonArray
2626
import kotlinx.serialization.json.JsonElement
@@ -44,21 +44,18 @@ constructor(
4444
private val catalog: A2uiCatalog,
4545
private val schemaMappings: Map<String, String> = emptyMap(),
4646
) {
47-
private val validator: JsonSchema = buildValidator()
48-
private val mapper = ObjectMapper()
49-
private val shared0_9Factory: JsonSchemaFactory by lazy {
50-
JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012))
51-
.schemaMappers { schemaMappers ->
52-
schemaMappings.forEach { (prefix, target) -> schemaMappers.mapPrefix(prefix, target) }
47+
private val shared0_9Registry: SchemaRegistry by lazy {
48+
SchemaRegistry.withDialect(Dialects.getDraft202012()) { builder ->
49+
builder.schemaIdResolvers { schemaIdResolvers ->
50+
schemaMappings.forEach { (prefix, target) -> schemaIdResolvers.mapPrefix(prefix, target) }
5351
}
54-
.build()
55-
}
56-
private val sharedConfig: SchemaValidatorsConfig by lazy {
57-
SchemaValidatorsConfig.builder().build()
52+
}
5853
}
59-
private val subValidators = mutableMapOf<String, JsonSchema>()
54+
private val sharedConfig: SchemaRegistryConfig by lazy { SchemaRegistryConfig.builder().build() }
55+
private val subValidators = mutableMapOf<String, Schema>()
56+
private val validator: Schema = buildValidator()
6057

61-
private fun buildValidator(): JsonSchema =
58+
private fun buildValidator(): Schema =
6259
if (catalog.version == A2uiVersion.VERSION_0_8) build0_8Validator() else build0_9Validator()
6360

6461
private fun injectAdditionalProperties(
@@ -121,7 +118,7 @@ constructor(
121118
return bundled as JsonObject
122119
}
123120

124-
private fun build0_8Validator(): JsonSchema {
121+
private fun build0_8Validator(): Schema {
125122
val bundledSchema = bundle0_8Schemas()
126123
val fullSchema = SchemaResourceLoader.wrapAsJsonArray(bundledSchema).toMutableMap()
127124
fullSchema[KEY_DOLLAR_SCHEMA] = JsonPrimitive(SCHEMA_DRAFT_2020_12)
@@ -132,38 +129,29 @@ constructor(
132129
val baseDirUri = baseUri.substringBeforeLast("/")
133130
val commonTypesUri = "$baseDirUri/$FILE_COMMON_TYPES"
134131

135-
val config = SchemaValidatorsConfig.builder().build()
136-
137132
val jsonFmt = Json { prettyPrint = false }
138133

139-
val factory =
140-
JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012))
141-
.schemaMappers { schemaMappers ->
142-
schemaMappings.forEach { (prefix, target) -> schemaMappers.mapPrefix(prefix, target) }
143-
schemaMappers.mapPrefix(FILE_COMMON_TYPES, commonTypesUri)
134+
val registry =
135+
SchemaRegistry.withDialect(Dialects.getDraft202012()) { builder ->
136+
builder.schemaIdResolvers { schemaIdResolvers ->
137+
schemaMappings.forEach { (prefix, target) -> schemaIdResolvers.mapPrefix(prefix, target) }
138+
schemaIdResolvers.mapPrefix(FILE_COMMON_TYPES, commonTypesUri)
144139
}
145-
.build()
140+
builder.schemaRegistryConfig(sharedConfig)
141+
}
146142

147143
val schemaString = jsonFmt.encodeToString(JsonElement.serializer(), JsonObject(fullSchema))
148-
return factory.getSchema(schemaString, config)
144+
return registry.getSchema(schemaString, InputFormat.JSON)
149145
}
150146

151-
private fun build0_9Validator(): JsonSchema {
147+
private fun build0_9Validator(): Schema {
152148
val fullSchema =
153149
SchemaResourceLoader.wrapAsJsonArray(catalog.serverToClientSchema).toMutableMap()
154150
fullSchema[KEY_DOLLAR_SCHEMA] = JsonPrimitive(SCHEMA_DRAFT_2020_12)
155151

156-
val config = SchemaValidatorsConfig.builder().build()
157-
val factory =
158-
JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012))
159-
.schemaMappers { schemaMappers ->
160-
schemaMappings.forEach { (prefix, target) -> schemaMappers.mapPrefix(prefix, target) }
161-
}
162-
.build()
163-
164152
val jsonFmt = Json { prettyPrint = false }
165153
val schemaString = jsonFmt.encodeToString(JsonElement.serializer(), JsonObject(fullSchema))
166-
return factory.getSchema(schemaString, config)
154+
return shared0_9Registry.getSchema(schemaString, InputFormat.JSON)
167155
}
168156

169157
/**
@@ -185,9 +173,8 @@ constructor(
185173
// Basic schema validation
186174
val jsonFmt = Json { prettyPrint = false }
187175
val messagesString = jsonFmt.encodeToString(JsonElement.serializer(), messages)
188-
val jsonNode = mapper.readTree(messagesString)
189176

190-
val errors = validator.validate(jsonNode)
177+
val errors = validator.validate(messagesString, InputFormat.JSON)
191178
if (errors.isNotEmpty()) {
192179
val msg = buildString {
193180
append("Validation failed:")
@@ -290,7 +277,7 @@ constructor(
290277
}
291278
}
292279

293-
private fun getSubValidator(defName: String): JsonSchema {
280+
private fun getSubValidator(defName: String): Schema {
294281
return subValidators.getOrPut(defName) {
295282
val defs =
296283
catalog.serverToClientSchema["\$defs"] as? JsonObject
@@ -310,22 +297,21 @@ constructor(
310297

311298
val jsonFmt = Json { prettyPrint = false }
312299
val schemaString = jsonFmt.encodeToString(JsonElement.serializer(), tempSchema)
313-
shared0_9Factory.getSchema(schemaString, sharedConfig)
300+
shared0_9Registry.getSchema(schemaString, InputFormat.JSON)
314301
}
315302
}
316303

317304
private fun getFormattedErrors(
318-
validator: JsonSchema,
305+
validator: Schema,
319306
instance: JsonElement,
320307
basePath: String,
321308
): List<String> {
322309
val jsonFmt = Json { prettyPrint = false }
323310
val instanceStr = jsonFmt.encodeToString(JsonElement.serializer(), instance)
324-
val jsonNode = mapper.readTree(instanceStr)
325-
val errors = validator.validate(jsonNode)
311+
val errors = validator.validate(instanceStr, InputFormat.JSON)
326312

327313
return errors.map { err ->
328-
val msg = err.message ?: ""
314+
val msg = err.toString()
329315
val unexpectedRegex =
330316
Regex(
331317
"property '(.*?)' is not defined in the schema and the schema does not allow additional properties"
@@ -419,7 +405,7 @@ constructor(
419405
)
420406
val jsonFmt = Json { prettyPrint = false }
421407
val schemaString = jsonFmt.encodeToString(JsonElement.serializer(), tempSchema)
422-
shared0_9Factory.getSchema(schemaString, sharedConfig)
408+
shared0_9Registry.getSchema(schemaString, InputFormat.JSON)
423409
}
424410

425411
return getFormattedErrors(validator, comp, path)

0 commit comments

Comments
 (0)