Best Kotest code snippet using io.kotest.assertions.json.schema.JsonSchema.JsonSchema.Builder.string
builder.kt
Source:builder.kt
1package io.kotest.assertions.json.schema2import io.kotest.common.ExperimentalKotest3import io.kotest.matchers.Matcher4import kotlinx.serialization.Contextual5import kotlinx.serialization.ExperimentalSerializationApi6import kotlinx.serialization.SerialName7import kotlinx.serialization.Serializable8@DslMarker9annotation class JsonSchemaMarker10@JsonSchemaMarker11@ExperimentalKotest12@Serializable(with = SchemaDeserializer::class)13sealed interface JsonSchemaElement {14 fun typeName(): String15}16internal interface ValueNode<T> {17 val matcher: Matcher<T>?18 get() = null19}20@ExperimentalKotest21data class JsonSchema(22 val root: JsonSchemaElement23) {24 /**25 * Provides a way to access pre-defined schemas in a conventional way (e.g. `type()`). Example:26 * ```kotlin27 * val addressSchema = jsonSchema {28 * obj {29 * withProperty("street") { string() }30 * withProperty("zipCode") { integer { beEven() } }31 * }32 * }33 *34 * val personSchema = jsonSchema {35 * obj {36 * withProperty("name") { string() }37 * withProperty("address") { addressSchema() }38 * }39 * }40 * ```41 */42 @ExperimentalKotest43 operator fun invoke() = root44 object Builder45 internal interface JsonNumber46 @Serializable47 data class JsonArray(val elementType: JsonSchemaElement) : JsonSchemaElement {48 override fun typeName() = "array"49 }50 class JsonObjectBuilder {51 var additionalProperties: Boolean = true52 var minProperties: Int = 053 var maxProperties: Int? = null54 var properties: MutableMap<String, JsonSchemaElement> = mutableMapOf()55 /**56 * https://json-schema.org/understanding-json-schema/reference/object.html#required-properties57 */58 var requiredProperties: MutableList<String> = mutableListOf()59 /**60 * By default properties are optional.61 * Using [required], you can specify that it must be included.62 */63 fun withProperty(64 name: String,65 required: Boolean = false,66 elementBuilder: JsonSchema.Builder.() -> JsonSchemaElement67 ) {68 properties[name] = JsonSchema.Builder.elementBuilder()69 if (required) requiredProperties.add(name)70 }71 fun build() = JsonObject(72 additionalProperties = additionalProperties,73 minProperties = minProperties,74 maxProperties = maxProperties,75 properties = properties,76 requiredProperties = requiredProperties.toTypedArray()77 )78 }79 @Serializable80 data class JsonObject(81 /**82 * Controls whether this node allows additional properties to be defined or not.83 * By default, additional properties are _allowed_84 *85 * https://json-schema.org/understanding-json-schema/reference/object.html#additional-properties86 */87 val additionalProperties: Boolean = true,88 val minProperties: Int = 0,89 val maxProperties: Int? = null,90 val properties: Map<String, JsonSchemaElement>,91 /**92 * https://json-schema.org/understanding-json-schema/reference/object.html#required-properties93 */94 val requiredProperties: Array<String> = emptyArray(),95 ) : JsonSchemaElement {96 operator fun get(name: String) = properties.get(name)97 override fun typeName() = "object"98 }99 data class JsonString(override val matcher: Matcher<String>? = null) : JsonSchemaElement, ValueNode<String> {100 override fun typeName() = "string"101 }102 data class JsonInteger(103 override val matcher: Matcher<Long>? = null104 ) : JsonSchemaElement, JsonNumber, ValueNode<Long> {105 override fun typeName() = "integer"106 }107 data class JsonDecimal(108 override val matcher: Matcher<Double>? = null109 ) : JsonSchemaElement, JsonNumber, ValueNode<Double> {110 override fun typeName() = "number"111 }112 @Serializable113 object JsonBoolean : JsonSchemaElement, ValueNode<Boolean> {114 override fun typeName() = "boolean"115 }116 @Serializable117 object Null : JsonSchemaElement {118 override fun typeName() = "null"119 }120}121/**122 * Creates a [JsonSchema.JsonString] node, which is a leaf node that will hold a [String] value.123 * Optionally, you can provide a [matcherBuilder] that constructs a [Matcher] which the node will be tested with.124 */125@ExperimentalKotest126fun JsonSchema.Builder.string(matcherBuilder: () -> Matcher<String>? = { null }) =127 JsonSchema.JsonString(matcherBuilder())128/**129 * Creates a [JsonSchema.JsonInteger] node, which is a leaf node that will hold a [Long] value.130 * Optionally, you can provide a [matcherBuilder] that constructs a [Matcher] which the node will be tested with.131 */132@ExperimentalKotest133fun JsonSchema.Builder.integer(matcherBuilder: () -> Matcher<Long>? = { null }) =134 JsonSchema.JsonInteger(matcherBuilder())135/**136 * Creates a [JsonSchema.JsonDecimal] node, which is a leaf node that will hold a [Double] value.137 * Optionally, you can provide a [matcherBuilder] that constructs a [Matcher] which the node will be tested with.138 */139@ExperimentalKotest140fun JsonSchema.Builder.number(matcherBuilder: () -> Matcher<Double>? = { null }) =141 JsonSchema.JsonDecimal(matcherBuilder())142/**143 * Alias for [number]144 *145 * Creates a [JsonSchema.JsonDecimal] node, which is a leaf node that will hold a [Double] value.146 * Optionally, you can provide a [matcherBuilder] that constructs a [Matcher] which the node will be tested with.147 */148@ExperimentalKotest149fun JsonSchema.Builder.decimal(matcherBuilder: () -> Matcher<Double>? = { null }) =150 JsonSchema.JsonDecimal(matcherBuilder())151/**152 * Creates a [JsonSchema.JsonBoolean] node, which is a leaf node that will hold a [Boolean] value.153 * It supports no further configuration. The actual value must always be either true or false.154 */155@ExperimentalKotest156fun JsonSchema.Builder.boolean() =157 JsonSchema.JsonBoolean158/**159 * Creates a [JsonSchema.Null] node, which is a leaf node that must always be null, if present.160 */161@ExperimentalKotest162fun JsonSchema.Builder.`null`() =163 JsonSchema.Null164/**165 * Creates a [JsonSchema.JsonObject] node. Expand on the object configuration using the [dsl] which lets you specify166 * properties using [withProperty]167 *168 * Example:169 * ```kotlin170 * val personSchema = jsonSchema {171 * obj {172 * withProperty("name") { string() }173 * withProperty("age") { number() }174 * }175 * }176 * ```177 */178@ExperimentalKotest179fun JsonSchema.Builder.obj(dsl: JsonSchema.JsonObjectBuilder.() -> Unit = {}) =180 JsonSchema.JsonObjectBuilder().apply(dsl).build()181/**182 * Defines a [JsonSchema.JsonArray] node where the elements are of the type provided by [typeBuilder].183 */184@ExperimentalKotest185fun JsonSchema.Builder.array(typeBuilder: () -> JsonSchemaElement) =186 JsonSchema.JsonArray(typeBuilder())187@ExperimentalKotest188fun jsonSchema(189 rootBuilder: JsonSchema.Builder.() -> JsonSchemaElement190): JsonSchema =191 JsonSchema(192 JsonSchema.Builder.rootBuilder()193 )...
json.kt
Source:json.kt
1package com.acme.web.test2import com.github.fge.jackson.JsonLoader3import com.github.fge.jsonschema.SchemaVersion4import com.github.fge.jsonschema.cfg.ValidationConfiguration5import com.github.fge.jsonschema.core.report.ListProcessingReport6import com.github.fge.jsonschema.main.JsonSchemaFactory7import io.kotest.assertions.assertionCounter8import io.kotest.assertions.failure9import io.kotest.assertions.json.shouldEqualJson10import io.kotest.common.runBlocking11import io.ktor.client.call.body12import io.ktor.client.statement.HttpResponse13import kotlinx.serialization.json.Json14import kotlinx.serialization.json.JsonElement15import kotlinx.serialization.json.JsonObject16import kotlinx.serialization.json.jsonArray17import kotlinx.serialization.json.jsonObject18import kotlinx.serialization.json.jsonPrimitive19@Suppress("ObjectPropertyName")20val JsonElement._links get() = this.jsonObject["_links"]!!.jsonObject21val JsonObject.self get() = this["self"]!!.jsonObject22val JsonObject.href get() = this["href"]!!.jsonPrimitive.content23val JsonObject.items get() = this["items"]!!.jsonArray.map { it.jsonObject }24private val schemaFactory = JsonSchemaFactory.newBuilder().setValidationConfiguration(25 ValidationConfiguration.newBuilder().setDefaultVersion(26 SchemaVersion.DRAFTV427 ).freeze()28).freeze()29suspend fun HttpResponse.json() = Json.parseToJsonElement(body())30suspend fun HttpResponse.firstLinkedItemHref() = json()._links.items.first().href31infix fun HttpResponse.shouldEqualJson(json: String) =32 runBlocking {33 body<String>() shouldEqualJson json34 }35infix fun HttpResponse.shouldMatchJsonSchema(schema: String) =36 runBlocking {37 body<String>() shouldMatchJsonSchema schema38 }39infix fun String.shouldMatchJsonSchema(schema: String) {40 assertionCounter.inc()41 val schemaJson = JsonLoader.fromString(schema.trimIndent())42 val contentJson = JsonLoader.fromString(this)43 val report = schemaFactory.getJsonSchema(schemaJson).validate(contentJson) as ListProcessingReport44 if (!report.isSuccess) {45 failure(report.asJson().toPrettyString())46 }47}...
JsonSchema.Builder.string
Using AI Code Generation
1{2 "properties" : {3 "name" : {4 }5 },6}7{8}
JsonSchema.Builder.string
Using AI Code Generation
1val schema = JsonSchema.Builder.string()2schema.minLength(3)3schema.maxLength(10)4schema.pattern("[a-z]+")5schema.nullable()6schema.default("abc")7schema.description("This is a string")8schema.title("String")9schema.examples("abc", "xyz")10schema.examples("pqr")
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!