1

I am trying to adopt existing code of parameter validation from Javalin 3 to Javalin 4. It uses Javalin's Validator class. Here's the code I've got (rewritten):

  val messageId = ctx.pathParamAsClass<String>("messageId")
    .check(check = { it.trim().matches(Uuid.REGEX) }, errorMessage = "message id must be a UUID")
    .get()
    .trim()

And I am getting compile error for the check() call:

e: 
/home/ivan/.../SourceFile.kt: (53, 6): None of the following functions can be called with the arguments 
supplied: 
public final fun check(check: Check<String> /* = (String) -> Boolean */, error: ValidationError<String>): 
Validator<String> defined in io.javalin.core.validation.Validator
public final fun check(check: Check<String> /* = (String) -> Boolean */, error: String): Validator<String> defined in 
io.javalin.core.validation.Validator

I can't understand why there is an error. I assume I should have matched second overload of check(). How to write it correctly?

Note: I have read Javalin 3 to 4 migration guide, which gives example like this:

ctx.queryParamAsClass<Int>("age")
    .check({ it >= 18 }, ValidationError("AGE_TOO_LOW", args = mapOf("minAge" to 18)))
    .get()

which I seem to follow, except I give it error message as string, but there's matching overload. So what is wrong here?

ivan.ukr
  • 2,853
  • 1
  • 23
  • 41

1 Answers1

0

The cause was that second parameter of check() is called error, not errorMessage, i.e. correct code is like this:

...
  .check(check = { it.trim().matches(Uuid.REGEX) }, error = "...")
...
ivan.ukr
  • 2,853
  • 1
  • 23
  • 41