com.mercateo.jsonschema.schema.mapper.PrimitiveJsonPropertyBuilder.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of json-schema Show documentation
Show all versions of json-schema Show documentation
json-schema generator for the JVM.
package com.mercateo.jsonschema.schema.mapper
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.node.JsonNodeFactory
import com.fasterxml.jackson.databind.node.ObjectNode
import com.mercateo.jsonschema.property.Property
import com.mercateo.jsonschema.schema.ObjectContext
internal class PrimitiveJsonPropertyBuilder(private val nodeFactory: JsonNodeFactory) {
private val genericJsonPropertyMapper: GenericJsonPropertyMapper
init {
this.genericJsonPropertyMapper = GenericJsonPropertyMapper(nodeFactory)
}
fun forProperty(jsonProperty: ObjectContext): Builder {
return Builder(jsonProperty)
}
internal inner class Builder(private val jsonProperty: ObjectContext) {
private val propertyNode: ObjectNode
init {
propertyNode = ObjectNode(nodeFactory)
}
fun withType(type: String): Builder {
propertyNode.put("type", type)
return this
}
fun withDefaultAndAllowedValues(nodeCreator: (T) -> JsonNode): Builder {
genericJsonPropertyMapper.addDefaultAndAllowedValues(propertyNode, this.jsonProperty, nodeCreator)
return this
}
fun withDefaultValue(value: JsonNode): Builder {
propertyNode.set("default", value)
return this
}
fun build(): ObjectNode {
return propertyNode
}
}
}