How to use JsonSchema.Builder.array method of io.kotest.assertions.json.schema.JsonSchema class

Best Kotest code snippet using io.kotest.assertions.json.schema.JsonSchema.JsonSchema.Builder.array

builder.kt

Source:builder.kt Github

copy

Full Screen

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 )...

Full Screen

Full Screen

json.kt

Source:json.kt Github

copy

Full Screen

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}...

Full Screen

Full Screen

JsonSchema.Builder.array

Using AI Code Generation

copy

Full Screen

1import io.kotest.assertions.json.schema.JsonSchema2val schema = JsonSchema.Builder.array()3import io.kotest.assertions.json.schema.JsonSchema4val schema = JsonSchema.Builder.boolean()5import io.kotest.assertions.json.schema.JsonSchema6val schema = JsonSchema.Builder.integer()7import io.kotest.assertions.json.schema.JsonSchema8val schema = JsonSchema.Builder.number()9import io.kotest.assertions.json.schema.JsonSchema10val schema = JsonSchema.Builder.`null`()11import io.kotest.assertions.json.schema.JsonSchema12val schema = JsonSchema.Builder.`object`()13import io.kotest.assertions.json.schema.JsonSchema14val schema = JsonSchema.Builder.string()15import io.kotest.assertions.json.schema.JsonSchema16val schema = JsonSchema.Builder.type()17import io.kotest.assertions.json.schema.JsonSchema18val schema = JsonSchema.Builder.union()19import io.kotest.assertions.json.schema.JsonSchema20val schema = JsonSchema.Builder.uri()21import io.kotest.assertions.json.schema.JsonSchema22val schema = JsonSchema.Builder.with()23import io.kotest.assertions.json.schema.JsonSchema24val schema = JsonSchema.Builder.withDefault()

Full Screen

Full Screen

JsonSchema.Builder.array

Using AI Code Generation

copy

Full Screen

1val schema = JsonSchema.builder().array("array").items(JsonSchema.builder().string("string")).build()2val schema = JsonSchema.builder().array("array").items(JsonSchema.builder().string("string")).build()3val schema = JsonSchema.builder().array("array").items(JsonSchema.builder().string("string")).build()4val schema = JsonSchema.builder().array("array").items(JsonSchema.builder().string("string")).build()5val schema = JsonSchema.builder().array("array").items(JsonSchema.builder().string("string")).build()6val schema = JsonSchema.builder().array("array").items(JsonSchema.builder().string("string")).build()7val schema = JsonSchema.builder().array("array").items(JsonSchema.builder().string("string")).build()8val schema = JsonSchema.builder().array("array").items(JsonSchema.builder().string("string")).build()9val schema = JsonSchema.builder().array("array").items(JsonSchema.builder().string("string")).build()10val schema = JsonSchema.builder().array("array").items(JsonSchema.builder().string("string")).build()11val schema = JsonSchema.builder().array("array").items(JsonSchema.builder().string("string")).build()12val schema = JsonSchema.builder().array("array").items(JsonSchema

Full Screen

Full Screen

JsonSchema.Builder.array

Using AI Code Generation

copy

Full Screen

1val schema = JsonSchema.array()2schema should matchJsonSchema(3"""{4}"""5val schema = JsonSchema.array()6schema should matchJsonSchema(7"""{8}"""9val schema = JsonSchema.array()10schema should matchJsonSchema(11"""{12}"""13val schema = JsonSchema.array()14schema should matchJsonSchema(15"""{16}"""17val schema = JsonSchema.array()18schema should matchJsonSchema(19"""{20}"""21val schema = JsonSchema.array()22schema should matchJsonSchema(23"""{24}"""25val schema = JsonSchema.array()26schema should matchJsonSchema(27"""{28}"""29val schema = JsonSchema.array()30schema should matchJsonSchema(31"""{32}"""33val schema = JsonSchema.array()34schema should matchJsonSchema(35"""{36}"""37val schema = JsonSchema.array()38schema should matchJsonSchema(39"""{40}"""41val schema = JsonSchema.array()42schema should matchJsonSchema(43"""{44}"""

Full Screen

Full Screen

JsonSchema.Builder.array

Using AI Code Generation

copy

Full Screen

1val schema = JsonSchema.array()2val schema = JsonSchema.`object`()3val schema = JsonSchema.string()4val schema = JsonSchema.number()5val schema = JsonSchema.boolean()6val schema = JsonSchema.null()7val schema = JsonSchema.value()8val schema = JsonSchema.ref()9val schema = JsonSchema.any()10val schema = JsonSchema.not()11val schema = JsonSchema.allOf()12val schema = JsonSchema.oneOf()13val schema = JsonSchema.anyOf()

Full Screen

Full Screen

JsonSchema.Builder.array

Using AI Code Generation

copy

Full Screen

1val arraySchema = JsonSchema . Builder . array ( JsonSchema . Builder . string (). build ()). build ()2val arraySchema = JsonSchema . Builder . array ( JsonSchema . Builder . string (). build (), JsonSchema . Builder . number (). build ()). build ()3val arraySchema = JsonSchema . Builder . array ( JsonSchema . Builder . string (). build (), JsonSchema . Builder . number (). build (), JsonSchema . Builder . boolean (). build ()). build ()4val arraySchema = JsonSchema . Builder . array ( JsonSchema . Builder . string (). build (), JsonSchema . Builder . number (). build (), JsonSchema . Builder . boolean (). build (), JsonSchema . Builder . array (). build ()). build ()5val arraySchema = JsonSchema . Builder . array ( JsonSchema . Builder . string (). build (), JsonSchema . Builder . number (). build (), JsonSchema . Builder . boolean (). build (), JsonSchema . Builder . array (). build (), JsonSchema . Builder . objectSchema (). build ()). build ()6val arraySchema = JsonSchema . Builder . array ( JsonSchema . Builder . string (). build (), JsonSchema . Builder . number (). build (), JsonSchema . Builder . boolean (). build (), JsonSchema . Builder . array (). build (), JsonSchema . Builder . objectSchema (). build (), JsonSchema . Builder . nullSchema (). build ()). build ()

Full Screen

Full Screen

JsonSchema.Builder.array

Using AI Code Generation

copy

Full Screen

1import io.kotest.assertions.json.schema.JsonSchema2import io.kotest.assertions.json.schema.array3import io.kotest.assertions.json.schema.any4import io.kotest.assertions.json.schema.obj5val schema = JsonSchema.obj {6 "a" to JsonSchema.any()7 "b" to JsonSchema.array(JsonSchema.any())8}9import io.kotest.assertions.json.schema.JsonSchema10import io.kotest.assertions.json.schema.obj11val schema = JsonSchema.obj {12 "a" to JsonSchema.any()13 "b" to JsonSchema.obj {14 "c" to JsonSchema.any()15 }16}17import io.kotest.assertions.json.schema.JsonSchema18import io.kotest.assertions.json.schema.any19import io.kotest.assertions.json.schema.nullable20val schema = JsonSchema.any().nullable()21import io.kotest.assertions.json.schema.JsonSchema22import io.kotest.assertions.json.schema.any23import io.kot

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful