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

Validation & Case Classes #253

Open
lenzenc opened this issue Apr 9, 2015 · 1 comment
Open

Validation & Case Classes #253

lenzenc opened this issue Apr 9, 2015 · 1 comment

Comments

@lenzenc
Copy link

lenzenc commented Apr 9, 2015

Are there any examples or extensions available to use case classes with the validation framework?

I guess it would be easy enough to just write a bit of code to turn a case class to a map and use the MapValidator, but it would be nice to have a similar validation feel like in RoR. Something like...

case class Person(name: String)
val validator = Validator(person, person is required, person, .....more validations)

Thoughts?

@seratch
Copy link
Member

seratch commented Apr 9, 2015

I'm not sure what you need. ActiveRecord-ish DSL? If so, we need to build new APIs that make full use of Java reflection API calls and implicit conversions... I'm not sure that's really nice.

Currently, skinny-validator can work with case classes. I hope you'll like it.

http://skinny-framework.org/documentation/validator.html

scala> import skinny.validator._
import skinny.validator._

scala> case class Person(name: String)
defined class Person

scala> val person = new Person("Alice")
person: Person = Person(Alice)

scala> val validator = Validator(param("name" -> person.name) is required)
validator: skinny.validator.Validator = skinny.validator.Validator@57f1ccde

scala> validator.errors
res1: skinny.validator.Errors = Errors(Map())

scala> val validator = Validator(param("name" -> person.name) is required & maxLength(3))
validator: skinny.validator.Validator = skinny.validator.Validator@a725f13

scala> validator.errors
res2: skinny.validator.Errors = Errors(Map(name -> List(Error(name = maxLength, messageParams = List(3)))))

If you hope to simplify param("name" -> person.name) is required more, I can understand. But, just an idea, enabling person.name is required will require quite heavy implicit conversions there. I'm afraid it won't make developers happy due to its negative side effect.

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

No branches or pull requests

2 participants