question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Validation functionality (discussion, input wanted)

See original GitHub issue

I’m considering including some functionality for validating user input. Typical things that need to be validated are:

ctx.pathParam("user-id")
ctx.queryParam("isbn")
ctx.formParam("username")

You could potentially include the validate function on the Context:

val isbn = ctx.validate(Param.QUERY, "isbn").matches(isbnRegex).get() ?: throw BadRequestResponse()
val username = ctx.validate(Param.FORM, "username").minLength(5).get() ?: throw BadRequestResponse()

But since there are a lot of possible entry points, a general validation function (util) would probably be better?

val isbn = validate(ctx.queryParam("isbn")).matches(isbnRegex).get() ?: throw BadRequestResponse()
val username = validate(ctx.formParam("username")).minLength(5).get() ?: throw BadRequestResponse()

This issue is intended for discussion. Should the functionality be included? If yes, how should it work?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:37 (30 by maintainers)

github_iconTop GitHub Comments

2reactions
tipsycommented, Sep 16, 2018

Out now as part of 2.2.0 !

2reactions
tipsycommented, Sep 13, 2018

Let me know if you have any comments on it or if you want me to implement a viable version of it.

I like @bchristenson’s suggestion for putting the validator on the Context, with prefix/suffix for param type. Mainly because I’m really excited about the idea of auto-generating nice error messages.

In my last suggestion I removed getOrThrow in favor of just get, because I needed the cast to be part of the get, like getAs<Int>() (getAsOrThrow<Int>(Exception.class) doesn’t work). If we used your approach with asDouble() (etc), we could go back to having getOrThrow():

val age = ctx.validatedQueryParam("age").asInt().getOrThrow()
val username = ctx.validatedFormParam("username").check({ it.length > 10 }).getOrThrow()
val userId = ctx.validatePathParam("user-id").matches("[0-9]").asLong().getOrThrow(MyException.class)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Client-side form validation - Learn web development | MDN
Specifying the email type, for example, validates the inputs value against a well-formed email address pattern or a pattern matching a comma- ...
Read more >
Designing Better Inline Validation UX - Smashing Magazine
Every input fields needs a conversation. For some fields, Ableton validates on blur, and for others validates only on submit.
Read more >
When and how to choose HTML for form validation
Client-side form validation such as HTML5 gives users near-immediate feedback about whether or not their input data is valid.
Read more >
What is Data Validation? How It Works and Why It's Important
Data validation is an essential part of any data handling task whether you're in the field collecting information, analyzing data, or preparing to...
Read more >
User Input Validation | Controls - Pearson IT Certification
The validation controls are usually associated with input server controls on which the validation needs to be performed. For validation to work properly,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found