Bond - Type-level validation for Scala

101 views
Skip to first unread message

Flavio W. Brasil

unread,
Mar 2, 2015, 8:14:06 AM3/2/15
to scala-a...@googlegroups.com, scala...@googlegroups.com

I’ve just pushed to maven central the first release of Bond. It is a library to express validation constraints using the type system. Some usage examples:

import net.fwbrasil.bond._

case class Employee (
    name: String with NonEmpty,
    email: String with Email,
    age: Int with GreaterThan[T.`14`.T]
)

case class Company (
    employees: List[Employee] with MinSize[T.`2`.T]
)

val employeeAge: Int with GreaterThan[T.`14`.T] = ...

val lifted: Int with GreaterThan[T.`12`.T] =
    GreaterThan(12).lift(employee.age)

GreaterThan(18).lift(employee.age)

[error] The lifting of 'Int with net.fwbrasil.bond.GreaterThan[Int(14)]'' to 'net.fwbrasil.bond.GreaterThan[Int(18)]' is not valid (Bond)
[error]         GreaterThan(18).lift(employee.age)
[error]                             ^


Regards,

-- 
Flavio W. Brasil

Som Snytt

unread,
Mar 2, 2015, 12:07:18 PM3/2/15
to scala-user
Did anyone else just go, "James Bond."


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ben Hutchison

unread,
Mar 3, 2015, 10:33:04 PM3/3/15
to Flavio W. Brasil, scala-a...@googlegroups.com, scala-user
Hi Flavio,

That looks really cool! I'm going to have a play with it when I find a suitable use case.

Some questions:

* The T.`12`.T syntax reads a bit odd. Is there any hope for a more natural way to write these constraint types?

* Will there by a Range or Interval type with bounds at both ends?


-Ben

--

Flavio W. Brasil

unread,
Mar 4, 2015, 7:18:12 PM3/4/15
to Ben Hutchison, scala-user
Hi Ben,

On Wednesday, March 4, 2015 at 4:32 AM, Ben Hutchison wrote:
Hi Flavio,

That looks really cool! I'm going to have a play with it when I find a suitable use case.

Some questions:

* The T.`12`.T syntax reads a bit odd. Is there any hope for a more natural way to write these constraint types?
There is a SIP to provide a better mechanism, but I’m afraid no one is working on it right now:


If I understood correctly, this would be the syntax: `GreaterThan[10]`. 

* Will there by a Range or Interval type with bounds at both ends?
It isn’t supported, but it is possible to implement. Could you open a github issue if you’d like to have this feature?

Best,
 
-- 
Flavio W. Brasil 
Reply all
Reply to author
Forward
0 new messages