Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate a collection of primitive values #67

Open
mattupstate opened this issue Jun 9, 2021 · 2 comments
Open

Validate a collection of primitive values #67

mattupstate opened this issue Jun 9, 2021 · 2 comments

Comments

@mattupstate
Copy link

mattupstate commented Jun 9, 2021

Is your feature request related to a problem? Please describe.

Giving Valiktor a try and it's not clear to me how to validate a collection of primitive values. For example, is it not possible to validate each value in a Set<String> with isNotBlank()?

Describe the solution you'd like

Perhaps something like:

data class Request(val emailAddresses: List<String>? = null)

validate(Request(listOf("hello@world.com", "goodbye")) {
   validate(Request::emailAddresses)
    .isNotNull()
    .hasSize(min = 1)
    .eachIsNotBlank()
    .eachIsEmail()
}

Describe alternatives you've considered

Otherwise it seems I have to make the type for the collection a value or data class with one field:

value class StringValue(val value: String)

data class Request(val emailAddresses: List<StringValue>? = null)

validate(Request(listOf("hello@world.com", "goodbye")) {
   validate(Request::emailAddresses)
    .isNotNull()
    .hasSize(min = 1)
    .validateForEach {
      validate(StringValue::value)
        .isNotBlank()
        .isEmail()
    }
}

Additional context

While I don't personally mind value classes like this, it's not exactly leading to the API design I'd like for JSON/HTTP interfaces.

@makcon
Copy link

makcon commented Jun 23, 2021

I've stuck with the same problem, would be good to have it. Thanks

@PierrickPuimeanChieze
Copy link

Solution could be to build your own DefaultConstraintsValidation. Something like that.

data class Request(val emailAddresses: List<String>? = null) {
    fun executeValidation() {
        validate(this) {
            validate(Request::emailAddresses).addConstraintViolations(
                it.emailAddresses
                    ?.mapIndexedNotNull { index, value ->
                        if (value.isEmail()) {
                            null
                        } else {
                            DefaultConstraintViolation(
                                property = "emailAddresses[$index]",
                                constraint = Email,
                                value = value
                            )
                        }
                    } ?: emptyList()
            )
        }
    }
}

fun String?.isEmail() = this == null || matches(
    Regex(
        "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"
    )
)

not the cleanest way to do it, I agree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants