jsr 303 and scala

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

jsr 303 and scala

Tim Pigden

Hi

 

Has anyone given any thought to how JSR 303 might interact with Scala or done any experiments, with for example hibernate validator 4 beta? I’ve got a big domain model I’m developing in scala and validator annotations would be pretty useful.

 

Tim Pigden

Reply | Threaded
Open this post in threaded view
|

RE: jsr 303 and scala

Tim Pigden

Well mixed success.

 

class TestAnnotatedValidators {

 

  class Something {

    var positive: Int = _

 

  @Min(0)

    def getPositive = positive

 

  }

 

  @Test

  def testAnnotation() {

    var myThing = new Something

    myThing.positive = -1

    val validator = Validation.buildDefaultValidatorFactory.getValidator

    val validationResult = validator.validate(myThing)

    println("validation result " + validationResult)

  }

}

 

From: Tim Pigden [mailto:[hidden email]]
Sent: 10 September 2009 14:04
To: [hidden email]
Subject: [scala-user] jsr 303 and scala

 

Hi

 

Has anyone given any thought to how JSR 303 might interact with Scala or done any experiments, with for example hibernate validator 4 beta? I’ve got a big domain model I’m developing in scala and validator annotations would be pretty useful.

 

Tim Pigden

Reply | Threaded
Open this post in threaded view
|

RE: jsr 303 and scala

Tim Pigden
In reply to this post by Tim Pigden

Sorry hit the wrong button:

 

 

 

Well mixed success.

This works:

 

class TestAnnotatedValidators {

 

  class Something {

    var positive: Int = _

 

  @Min(0)

    def getPositive = positive

 

  }

 

  @Test

  def testAnnotation() {

    var myThing = new Something

    myThing.positive = -1

    val validator = Validation.buildDefaultValidatorFactory.getValidator

    val validationResult = validator.validate(myThing)

    println("validation result " + validationResult)

  }

}

 

But this doesn’t

 

  class Something {

    @Min(0)

      var positive: Int = _

 

 

  }

 

testAnnotation(uk.co.optrak.scdomain.test.orders.TestAnnotatedValidators)  Time elapsed: 0.015 sec  <<< ERROR!

javax.validation.ValidationException: Annotated methods must follow the JavaBeans naming convention. positive_$eq() does not.

 

Seemingly because the annotation on the declaration of positive seems to be attached to a method rather than to a field – which I presume is something to do with the way scala compiler treats these things.

 

Any comments?

Reply | Threaded
Open this post in threaded view
|

Re: jsr 303 and scala

Kris Nuttycombe-3
See https://lampsvn.epfl.ch/trac/scala/ticket/1846
https://lampsvn.epfl.ch/trac/scala/ticket/2245
https://lampsvn.epfl.ch/trac/scala/ticket/2252 for other instances of
this problem. Essentially, we don't yet have enough expressivity with
respect to how annotations are applied to vars, vals, and the setters
and getters that the compiler generates, and it causes problems with
all sorts of annotation-based frameworks.

Kris

On Thu, Sep 10, 2009 at 9:41 AM, Tim Pigden <[hidden email]> wrote:

> Sorry hit the wrong button:
>
>
>
>
>
>
>
> Well mixed success.
>
> This works:
>
>
>
> class TestAnnotatedValidators {
>
>
>
>   class Something {
>
>     var positive: Int = _
>
>
>
>   @Min(0)
>
>     def getPositive = positive
>
>
>
>   }
>
>
>
>   @Test
>
>   def testAnnotation() {
>
>     var myThing = new Something
>
>     myThing.positive = -1
>
>     val validator = Validation.buildDefaultValidatorFactory.getValidator
>
>     val validationResult = validator.validate(myThing)
>
>     println("validation result " + validationResult)
>
>   }
>
> }
>
>
>
> But this doesn’t
>
>
>
>   class Something {
>
>     @Min(0)
>
>       var positive: Int = _
>
>
>
>
>
>   }
>
>
>
> testAnnotation(uk.co.optrak.scdomain.test.orders.TestAnnotatedValidators)
> Time elapsed: 0.015 sec  <<< ERROR!
>
> javax.validation.ValidationException: Annotated methods must follow the
> JavaBeans naming convention. positive_$eq() does not.
>
>
>
> Seemingly because the annotation on the declaration of positive seems to be
> attached to a method rather than to a field – which I presume is something
> to do with the way scala compiler treats these things.
>
>
>
> Any comments?