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

Created dynamic parameter validator #2377

Merged
merged 1 commit into from May 4, 2014
Merged

Created dynamic parameter validator #2377

merged 1 commit into from May 4, 2014

Conversation

jeroenr
Copy link

@jeroenr jeroenr commented Feb 13, 2014

Allows easy validation of multiple parameters against multiple constraints. To check, for instance, if a few numeric parameters are within range [0, 100]:

def myAction(param1: Option[Int], param2: Option[Int]) = Action {
   implicit request =>
     ParameterValidator(Seq(Contraints.min(0), Constraints.max(100)), param1, param2) match {
        case Valid => Ok
        case Invalid(errors) => BadRequest(errors.mkstring(", "))
    }

Unit tests are included

@huntc
Copy link
Contributor

huntc commented Feb 16, 2014

Can you please flatten the two commits. Thanks

@jroper
Copy link
Member

jroper commented Feb 16, 2014

Need to think about this, here we're talking about introducing validation on raw values, not on some structure (eg a form). It may be better to introduce this into the parameter binding API rather than here.

…meters against multiple constraints. Included unit tests

Added another unit test to prove we support different data types and aggregate multiple errors
@jeroenr
Copy link
Author

jeroenr commented Feb 17, 2014

@huntc done :) @jroper as far as I could tell none of the constructs in the play/api/data/validation/Validation.scala file assumes we are in a form context. The form related validation is in the play/api/data/Form.scala file, so Validation.scala seemed to be the right place

@jroper
Copy link
Member

jroper commented Feb 17, 2014

@jeroenr You're right, but I think actually what's happening here is we're introducing a new structure, a list of options, and validating that. Just like the form validation code is outside of this, and the JSON validation code is outside of this, perhaps this also should be somewhere else. Or perhaps not. I'd just like to consider the alternatives.

@jeroenr
Copy link
Author

jeroenr commented Feb 18, 2014

@jroper Ok that makes sense. I've been playing around combining the validation logic with custom QueryStringBindable's. It fits well, so I can definitely see your point now about moving the validator to the parameter binding API.

@jroper
Copy link
Member

jroper commented May 4, 2014

Let's just call this a handy little validation and put it here for now.

jroper added a commit that referenced this pull request May 4, 2014
Created dynamic parameter validator
@jroper jroper merged commit 7ebd6c4 into playframework:master May 4, 2014
@jeroenr
Copy link
Author

jeroenr commented May 5, 2014

Awesome 👍

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

Successfully merging this pull request may close these issues.

None yet

3 participants