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

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

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

JsonSchema.Builder.obj

Using AI Code Generation

copy

Full Screen

1{2"address": {3},4{5},6{7}8}9{10"address": {11},12{13},14{15}16}17{18"address": {19},20{21},22{23}24}25{26"address": {27},28{29},30{31}32}33{

Full Screen

Full Screen

JsonSchema.Builder.obj

Using AI Code Generation

copy

Full Screen

1val schema = JsonSchema.obj(2"firstName" to JsonSchema.string(),3"lastName" to JsonSchema.string(),4"age" to JsonSchema.int(),5"address" to JsonSchema.obj(6"street" to JsonSchema.string(),7"city" to JsonSchema.string(),8"state" to JsonSchema.string(),9"zip" to JsonSchema.string()10val schema = JsonSchema.arr(11JsonSchema.obj(12"firstName" to JsonSchema.string(),13"lastName" to JsonSchema.string(),14"age" to JsonSchema.int(),15"address" to JsonSchema.obj(16"street" to JsonSchema.string(),17"city" to JsonSchema.string(),18"state" to JsonSchema.string(),19"zip" to JsonSchema.string()20val schema = JsonSchema.any()21val schema = JsonSchema.string()22val schema = JsonSchema.int()23val schema = JsonSchema.number()24val schema = JsonSchema.boolean()25val schema = JsonSchema.null()26val schema = JsonSchema.obj(27"firstName" to JsonSchema.string(),28"lastName" to JsonSchema.string(),29"age" to JsonSchema.int(),30"address" to JsonSchema.obj(31"street" to JsonSchema.string(),32"city" to JsonSchema.string(),

Full Screen

Full Screen

JsonSchema.Builder.obj

Using AI Code Generation

copy

Full Screen

1val schema = JsonSchema.obj {2"firstName" to JsonSchema.string()3"lastName" to JsonSchema.string()4"age" to JsonSchema.int()5"address" to JsonSchema.obj {6"streetAddress" to JsonSchema.string()7"city" to JsonSchema.string()8"state" to JsonSchema.string()9"postalCode" to JsonSchema.string()10}11"phoneNumber" to JsonSchema.array(JsonSchema.string())12}13val personJson = """{14"address": {15},16}"""17personJson.shouldMatchJsonSchema(schema)18val schema = JsonSchema.array(JsonSchema.obj {19"firstName" to JsonSchema.string()20"lastName" to JsonSchema.string()21"age" to JsonSchema.int()22"address" to JsonSchema.obj {23"streetAddress" to JsonSchema.string()24"city" to JsonSchema.string()25"state" to JsonSchema.string()26"postalCode" to JsonSchema.string()27}28"phoneNumber" to JsonSchema.array(JsonSchema.string())29})30val personJson = """[{31"address": {32},33}]"""34personJson.shouldMatchJsonSchema(schema)35val schema = JsonSchema.string()36personJson.shouldMatchJsonSchema(schema)37val schema = JsonSchema.int()38personJson.shouldMatchJsonSchema(schema)

Full Screen

Full Screen

JsonSchema.Builder.obj

Using AI Code Generation

copy

Full Screen

1import io.kotest.assertions.json.schema.*2val schema = JsonSchema.obj {3}4val schema = JsonSchema.obj {5}6val schema = JsonSchema.obj {7}8val schema = JsonSchema.obj {9}10val schema = JsonSchema.obj {11}12val schema = JsonSchema.obj {13}14val schema = JsonSchema.obj {15}16val schema = JsonSchema.obj {17}18val schema = JsonSchema.obj {19}

Full Screen

Full Screen

JsonSchema.Builder.obj

Using AI Code Generation

copy

Full Screen

1val schema = JsonSchema.obj<TestClass> {2 "name" - string()3}4val schema = JsonSchema.obj<TestClass> {5 "testName" - string()6}7val schema = JsonSchema.obj<TestClass> {8 "name" - int()9}10val schema = JsonSchema.obj<TestClass> {11 "testName" - int()12}13val schema = JsonSchema.obj<TestClass> {14 "testName" - int().min(1)15}16val schema = JsonSchema.obj<TestClass> {17 "testName" - int().min(1)18}19val schema = JsonSchema.obj<TestClass> {20 "testName" - int().min(1)21}22val schema = JsonSchema.obj<TestClass> {23 "testName" - int().min(1)24}25val schema = JsonSchema.obj<TestClass> {26 "testName" - int().min(1)27}28val schema = JsonSchema.obj<TestClass> {29 "testName" - int().min(1)30}

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