Skip to content

Instantly share code, notes, and snippets.

@bartoszm
Created March 17, 2024 21:15
Show Gist options
  • Save bartoszm/5bdcebe01c59742ad40165c1a78a8966 to your computer and use it in GitHub Desktop.
Save bartoszm/5bdcebe01c59742ad40165c1a78a8966 to your computer and use it in GitHub Desktop.
Comparison of two factories using minimal example
package com.amartus.playground.jackson
import com.networknt.schema.InputFormat
import com.networknt.schema.JsonSchemaFactory
import com.networknt.schema.SchemaValidatorsConfig
import com.networknt.schema.SpecVersion
import com.networknt.schema.ValidationMessage
import com.networknt.schema.walk.JsonSchemaWalkListener
import com.networknt.schema.walk.WalkEvent
import com.networknt.schema.walk.WalkFlow
fun walk(factory : JsonSchemaFactory, cfg: SchemaValidatorsConfig) {
println("Walking with factory: ${factory}")
val schema = """
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"children": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
}
}
}
""".trimIndent()
val data = """{"name": "John", "children": [{"name": "Alice"}]}"""
println("-- with data --")
factory.getSchema(schema, cfg)
.walk(data, InputFormat.JSON, false)
println("-- with no data --")
factory.getSchema(schema, cfg)
.walk(null, false)
}
fun main() {
val listener = object: JsonSchemaWalkListener {
override fun onWalkStart(walkEvent: WalkEvent?) = WalkFlow.CONTINUE
override fun onWalkEnd(walkEvent: WalkEvent?, validationMessages: MutableSet<ValidationMessage>?) {
println(walkEvent?.instanceLocation)
}
}
val cfg = SchemaValidatorsConfig().apply {
addItemWalkListener(listener)
addPropertyWalkListener(listener)
}
walk(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909), cfg)
walk(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012), cfg)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment