Created
March 17, 2024 21:15
-
-
Save bartoszm/5bdcebe01c59742ad40165c1a78a8966 to your computer and use it in GitHub Desktop.
Comparison of two factories using minimal example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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