Skip to content

Add Map-specific accesser like eachValue() or get() #37

@rektplorer64

Description

@rektplorer64

Relevant types

  • Map<K, V>

Implementation

A list of the constraint parameters and its expected behavior.

Example 1

val map = mapOf(
    "key1" to 10,
    "key2" to 20,
    "key3" to 30,
    "key4" to 40,
    "key5" to 50,
    "key6" to 60,
)

val mapValidator = Validator<Map<String, Int>> {
    // 1. eachValue - allows iteration of values within the Map (I think ordering is irrelevant.)
    eachValue { // this: Validatable<Int>
        isGreaterThanOrEqualTo(30)
    }

    // 2. get - allows quick access to a certain key within the map (Notice the nullable Int receiver type)
    get("key6") { // this: Validatable<Int?>
        isNotNull()
        isGreaterThanOrEqualTo(80)
    }
   
    // Alternative operator fun for the get method
    this["key7"] { // this: Validatable<Int?>
        isNotNull()
    }

    // 3. iterator - allows user to iterate all Entries within the Map
    iterator().forEach { validatable: Validatable<Map.Entry<String, Int>> ->
        with(validatable) {
            constrainIfNotNull { (key, value) ->
                '2' !in key
            }.otherwise { "Must not have 2 in key" }
        }
    }
}

Output

key1 -> Must be greater than or equal to 30
key2 -> Must be greater than or equal to 30
key6 -> Must be greater than or equal to 80
key7 -> Must not be null
key2 -> Must not have 2 in key

Example 2 (Nested Map)

This one used kotlinx.serialization's JsonElement for convenience.

val map = buildJsonObject {
    put("key1", "value1")
    putJsonObject("key2") {
        put("key2_1", "value2_1")
        put("key2_2", "value2_2")
    }
    putJsonObject("key3") {
        put("key3_1", "value3_1")
        putJsonArray("key3_2") {
            add("value3_2__1")
            add("value3_2__2")
            add("value3_2__3")
        }
    }
}


val mapValidator = Validator<JsonObject> {
    get("key1") {
        isEqualTo(JsonPrimitive("value1"))
    }

    get("key2") {
        isNotNull()
        constrainIfNotNull { it is JsonPrimitive }.otherwise { "key2 must be a primitive" }
    }

    this["key3"] {
        isNotNull()
        constrainIfNotNull { it is JsonObject }.otherwise { "key3 must be an object" }

        map { it?.jsonObject }.invoke {
            key("key3_1") {
                isNotNull()
                isEqualTo(JsonPrimitive("value3_1"))
            }

            key("key3_2") {
                map { it?.jsonArray }.invoke {
                    hasSizeEqualTo(10)
                    this[4] {
                        isEqualTo(JsonPrimitive("value3_2__5"))
                    }
                }
            }
        }
    }
}

Output

key2 -> key2 must be a primitive
key3.key3_2 -> The number of items must be equal to 10
key3.key3_2.4 -> Must be equal to ""value3_2__5""

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions