skinny-framework / skinny-framework

:monorail: "Scala on Rails" - A full-stack web app framework for rapid development in Scala
https://skinny-framework.github.io/
MIT License
737 stars 73 forks source link

Validation & Case Classes #253

Open lenzenc opened 9 years ago

lenzenc commented 9 years ago

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 commented 9 years ago

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.