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

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

builder.kt

Source:builder.kt Github

copy

Full Screen

...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@ExperimentalKotest...

Full Screen

Full Screen

ObjectSchemaTest.kt

Source:ObjectSchemaTest.kt Github

copy

Full Screen

...7 {8 fun json(@Language("JSON") raw: String) = raw9 val personSchemaAllowingExtraProperties = jsonSchema {10 obj {11 withProperty("name", required = true) { string() }12 withProperty("initials", required = false) { string() }13 withProperty("age", required = true) { number() }14 }15 }16 val personSchema = parseSchema(17 json(18 """19 {20 "type": "object",21 "properties": {22 "name": { "type": "string" },23 "initials": { "type": "string" },24 "age": { "type": "number" }25 },26 "requiredProperties": [ "name", "age" ],27 "additionalProperties": false28 }29 """30 )31 )32 test("matching object passes") {33 """{ "name": "John", "age": 27.2 }""" shouldMatchSchema personSchema34 }35 test("mismatching property type causes failure") {36 shouldFail {37 json("""{ "name": "John", "age": "twentyseven" }""") shouldMatchSchema personSchema38 }.message shouldBe """39 $.age => Expected number, but was string40 """.trimIndent()41 }42 test("primitive instead of object causes failure") {43 shouldFail {44 "\"hello\"" shouldMatchSchema personSchema45 }.message shouldBe """46 $ => Expected object, but was string47 """.trimIndent()48 }49 test("Extra property causes failure") {50 shouldFail {51 json("""{ "name": "John", "age": 27.2, "profession": "T800" }""") shouldMatchSchema personSchema52 }.message shouldBe """53 $.profession => Key undefined in schema, and schema is set to disallow extra keys54 """.trimIndent()55 }56 test("Missing required property causes failure") {57 shouldFail {58 json("""{ "name": "John" }""") shouldMatchSchema personSchema59 }.message shouldBe """60 $.age => Expected number, but was undefined61 """.trimIndent()62 }63 test("Extra property causes failure for scheam disallowing it") {64 shouldFail {65 json("""{ "name": "John", "favorite_pet": "Cat", "age": 2 }""") shouldMatchSchema personSchema66 }.message shouldBe """67 $.favorite_pet => Key undefined in schema, and schema is set to disallow extra keys68 """.trimIndent()69 }70 test("Extra property is OK when schema allows it") {71 json("""{ "name": "John", "favorite_pet": "Cat", "age": 2 }""") shouldMatchSchema personSchemaAllowingExtraProperties72 }73 test("Problems compound") {74 shouldFail {75 json("""{ "name": 5, "age": "twentyseven" }""") shouldMatchSchema personSchema76 }.message shouldBe """77 $.name => Expected string, but was number78 $.age => Expected number, but was string79 """.trimIndent()80 }81 context("nested objects") {82 val companySchema = jsonSchema {83 obj {84 withProperty("owner") { personSchema.root }85 withProperty("employees") {86 array {87 personSchema.root // TODO: Should be possible to compose schemas without explicitly unpacking boxing element88 }89 }90 }91 }92 test("matching object passes") {93 json("""{ "owner": { "name": "Emil", "age": 34.1 }, "employees": [] }""") shouldMatchSchema94 companySchema95 }96 test("Mismatch gives good message") {97 shouldFail {98 json("""{ "owner": { "name": 5, "age": 34.1 }, "employees": [] }""") shouldMatchSchema99 companySchema...

Full Screen

Full Screen

SchemaWithMatcherTest.kt

Source:SchemaWithMatcherTest.kt Github

copy

Full Screen

...25 context("smoke") {26 val schema = jsonSchema {27 array {28 obj {29 withProperty("name") { string { haveMinLength(3) and haveMaxLength(20) } }30 withProperty("age") { number { beGreaterThanOrEqualTo(0.0) and beLessThanOrEqualTo(120.0) } }31 }32 }33 }34 test("Violating schema") {35 shouldFail {36 // language=JSON37 """38 [39 {40 "name": "bo",41 "age": 9242 },43 {44 "name": "sophie",...

Full Screen

Full Screen

ArraySchemaTest.kt

Source:ArraySchemaTest.kt Github

copy

Full Screen

...8 fun json(@Language("JSON") raw: String) = raw9 val numberArray = jsonSchema { array { number() } }10 val person = jsonSchema {11 obj {12 withProperty("name", required = true) { string() }13 withProperty("age", required = true) { number() }14 }15 }16 val personArray = jsonSchema { array { person() } }17 test("Array with correct elements match") {18 """[1, 2]""" shouldMatchSchema numberArray19 }20 test("Problems compound") {21 shouldFail { """["one", "two"]""" shouldMatchSchema numberArray }.message shouldBe """22 $[0] => Expected number, but was string23 $[1] => Expected number, but was string24 """.trimIndent()25 }26 test("empty array is ok") {27 "[]" shouldMatchSchema personArray...

Full Screen

Full Screen

withProperty

Using AI Code Generation

copy

Full Screen

1val schema = JsonSchema.fromResource("/schema.json")2val json = JsonParser.parseString("""{"name": "John"}""")3json should matchJsonSchema(schema)4json should matchJsonSchema(schema) withProperty "name" withValue "John"5json should matchJsonSchema(schema) withProperty "name" withValue "John" withValue "Doe"6val json = JsonParser.parseString("""{"name": "John"}""")7json should matchJsonSchema(schema)8json should matchJsonSchema(schema) withProperty "name" withValue "John"9json should matchJsonSchema(schema) withProperty "name" withValue "John" withValue "Doe"10val json = JsonParser.parseString("""{"name": "John"}""")11json should matchJsonSchema(schema)12json should matchJsonSchema(schema) withProperty "name" withValue "John"13json should matchJsonSchema(schema) withProperty "name" withValue "John" withValue "Doe"14val json = JsonParser.parseString("""{"name": "John"}""")15json should matchJsonSchema(schema)16json should matchJsonSchema(schema) withProperty "name" withValue "John"17json should matchJsonSchema(schema) withProperty "name" withValue "John" withValue "Doe"18val json = JsonParser.parseString("""{"name": "John"}""")19json should matchJsonSchema(schema)20json should matchJsonSchema(schema) withProperty "name" withValue "John"21json should matchJsonSchema(schema) withProperty "name" withValue "John" withValue "Doe"22val json = JsonParser.parseString("""{"name": "John"}""")

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