play28-refined


License

License

GroupId

GroupId

be.venneborg
ArtifactId

ArtifactId

play28-refined_2.12
Last Version

Last Version

0.6.0
Release Date

Release Date

Type

Type

jar
Description

Description

play28-refined
play28-refined
Project URL

Project URL

https://github.com/kwark/play-refined
Project Organization

Project Organization

be.venneborg
Source Code Management

Source Code Management

https://github.com/kwark/play-refined

Download play28-refined_2.12

How to add to project

<!-- https://jarcasting.com/artifacts/be.venneborg/play28-refined_2.12/ -->
<dependency>
    <groupId>be.venneborg</groupId>
    <artifactId>play28-refined_2.12</artifactId>
    <version>0.6.0</version>
</dependency>
// https://jarcasting.com/artifacts/be.venneborg/play28-refined_2.12/
implementation 'be.venneborg:play28-refined_2.12:0.6.0'
// https://jarcasting.com/artifacts/be.venneborg/play28-refined_2.12/
implementation ("be.venneborg:play28-refined_2.12:0.6.0")
'be.venneborg:play28-refined_2.12:jar:0.6.0'
<dependency org="be.venneborg" name="play28-refined_2.12" rev="0.6.0">
  <artifact name="play28-refined_2.12" type="jar" />
</dependency>
@Grapes(
@Grab(group='be.venneborg', module='play28-refined_2.12', version='0.6.0')
)
libraryDependencies += "be.venneborg" % "play28-refined_2.12" % "0.6.0"
[be.venneborg/play28-refined_2.12 "0.6.0"]

Dependencies

compile (3)

Group / Artifact Type Version
eu.timepit : refined_2.12 jar 0.9.14
com.typesafe.play : play_2.12 jar 2.8.2
com.typesafe.play : play-json_2.12 jar 2.9.0

test (4)

Group / Artifact Type Version
com.typesafe.play : play-netty-server_2.12 jar 2.8.2
org.scalatestplus.play : scalatestplus-play_2.12 jar 4.0.3
com.typesafe.play : play-ws-standalone-json_2.12 jar 2.1.2
org.scalacheck : scalacheck_2.12 jar 1.14.3

Project Modules

There are no modules declared in this project.

Play-refined

Maven version

Play-refined is a small scala library that enables boilerplate-free integration of refinement types using the Refined library with Lightbend's Play framework

It allows you to easily use refined types with Play.

Scala 2.11, 2.12, 2.13 and Play 2.5, 2.6, 2.7 & 2.8 are supported. You'll also need to use Java8 or later.

The library provides the following functionality:

  • boilerplate-free JSON serialization/deserialization of refined types
  • boilerplate-free form binding/unbinding of refined types
  • Path/Query binding for refined types
  • Translation of Refined error messages to standard Play error codes

Usage

Versions: The table below lists the versions and their main dependencies.

Artifact to use Version Scala 2.11 Scala 2.12 Scala 2.13 Play Refined
play25-refined 0.5.0 2.5.x 0.9.8
play26-refined 0.5.0 2.6.x 0.9.8
play26-refined 0.6.0 2.6.x 0.9.14
play27-refined 0.5.0 2.7.x 0.9.8
play27-refined 0.6.0 2.7.x 0.9.14
play28-refined 0.6.0 2.8.x 0.9.14

Starting from version 0.6.0 support for scala 2.11 was dropped.

Depending on the artifact and version you need to add the correct dependency to your SBT dependencies:

libraryDependencies += "be.venneborg" %% "play27-refined" % <version>

Json Formatters

Suppose we have a case class which uses Refined types:

import eu.timepit.refined.types.string.NonEmptyString
import eu.timepit.refined.types.numeric.PosInt

case class FooBar(foo: NonEmptyString, bar: PosInt)

To automatically serialize/deserialize this case class containing refined types, you just need an additional import and derive a JSON Formatter

import be.venneborg.refined.play.RefinedJsonFormats._
import play.api.libs.json.Json

implicit val fooBarFormat = Json.format[FooBar]

Form binding

You can just as easily bind/unbind to/from a Form

import play.api.data.{Form, Forms}
import play.api.data.Forms.mapping

import be.venneborg.refined.play.RefinedForms._

val fooBarForm: Form[FooBar] = Form(
  mapping(
    "foo"  -> Forms.of[NonEmptyString],
    "bar"  -> Forms.of[PosInt]
  )(FooBar.apply)(FooBar.unapply)
) 

fooBarForm.bind(Map("foo" -> "myfoo", "bar" -> 5)).value

Path/Query binding

If you want to use a refined type as a query/path parameter in your routes file, you have a bit more work to do.

First you need to adapt your build.sbt file and extend the routesImport:

routesGenerator := InjectedRoutesGenerator

routesImport ++= Seq(
  "be.venneborg.refined.play.RefinedPathBinders._",
  "be.venneborg.refined.play.RefinedQueryBinders._",
  "eu.timepit.refined.types.numeric.PosInt" //This depends on the refined types you want to use
)

And now you can simple use the PosInt refined type either as a path or query param in your routes file:

GET /foobars/:bar     controllers.MyController.foobar(bar: PosInt)

Of course you'll also need a MyController with the appropriate Action defined:

class MyController extends .... {

    def foobar(bar: PosInt) = Action { Ok(v.value) }

}

Example project

This repository also contains an example project, which demonstrates all of the above. Please refer to it for more details.

Versions

Version
0.6.0