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

How do I implement List validation in Kotlin? #30

Closed
otkmnb2783 opened this issue Dec 4, 2019 · 4 comments
Closed

How do I implement List validation in Kotlin? #30

otkmnb2783 opened this issue Dec 4, 2019 · 4 comments
Labels
question Further information is requested

Comments

@otkmnb2783
Copy link

How do I implement List validation in Kotlin?

data class Form(
  val users: List<String>
) {
  companion object {
    val validator = ValidatorBuilder.of(Form::class.java)
       .forEach(GroupCreationRequest::members) {
           ValidatorBuilder.of(String::class.java)
               .konstraint(String::toString) { // <- ★ KProperty Only
                   notEmpty()
               }
               .build()
       }
       .build()
  }
}
@making
Copy link
Owner

making commented Dec 4, 2019

@making making added the question Further information is requested label Dec 4, 2019
@otkmnb2783
Copy link
Author

Thank you for an answer.

I want to know how to validate a simple List,
not how to create nested objects.

@making
Copy link
Owner

making commented Dec 5, 2019

I think I got you meant.

use constraint instead of konstraint.
It's up to you how to call the name of the value in a list.
I would use value.

val validator = ValidatorBuilder.of<Form>()
        .forEach(Form::users) {
            constraint(String::toString, "value") { it.greaterThan(1) }
        }
        .build()

In this example, the violation names will be like users[0].value, users[1].value ...

You can also use empty string as the name.

val validator = ValidatorBuilder.of<Form>()
        .forEach(Form::users) {
            constraint(String::toString, "") { it.greaterThan(1) }
        }
        .build()

In this example, the violation names will be like users[0], users[1] ...

@otkmnb2783
Copy link
Author

Thank you for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants