Skip to content

How to validate a Map? #38

Answered by nesk
rektplorer64 asked this question in Q&A
Discussion options

You must be logged in to vote

Yes, this is supported:

val mapValidator = Validator<Map<String, Int>> {
    // Validate a specific key
    this["key7"] { // this: Validatable<Int?>
        isNotNull()
    }

    // Iterate over keys
    keys.each { // this: Validatable<String>
        isGreaterThanOrEqualTo(30)
    }

    // Iterate over values
    values.each { // // this: Validatable<Int>
        isGreaterThanOrEqualTo(30)
    }

    // Iterate over entries
    entries.each {// this: Validatable<Map.Entry<String, Int>>
        constrainIfNotNull { (key, value) -> '2' !in key }
            .otherwise { "Must not have 2 in key" }
    }
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by nesk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested
2 participants
Converted from issue

This discussion was converted from issue #37 on August 03, 2024 10:54.