Skip to content
This repository has been archived by the owner on Jun 15, 2020. It is now read-only.

Commit

Permalink
Update play-scalajs-example to use a cross compiled validation
Browse files Browse the repository at this point in the history
- Change the build to publish-local validation, which will be required to compile the example until v2.0 is released
  • Loading branch information
OlivierBlanvillain committed Jun 3, 2016
1 parent 26dedb5 commit bd842d1
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ notifications:
email:
false

script: bash misc/ci.sh
script: bash scripts/ci.sh

cache:
directories:
Expand Down
2 changes: 1 addition & 1 deletion PUBLISH.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
```sh
git checkout gh-pages
git checkout master .
sh misc/build-book.sh
sh scripts/build-book.sh
git add .
git commit -am "Update book"
git push
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ val validationVersion = "2.0"
libraryDependencies ++= Seq(
"io.github.jto" %% "validation-core" % validationVersion,
"io.github.jto" %% "validation-playjson" % validationVersion,
"io.github.jto" %% "validation-json4s" % validationVersion,
"io.github.jto" %% "validation-jsonast" % validationVersion,
"io.github.jto" %% "validation-form" % validationVersion,
"io.github.jto" %% "validation-delimited" % validationVersion,
"io.github.jto" %% "validation-xml" % validationVersion
// "io.github.jto" %%% "validation-jsjson" % validationVersion
)
```

Expand Down
34 changes: 17 additions & 17 deletions play-scalajs-example/build.sbt
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
val scalaV = "2.11.8"

val validationVersion = "2.0"

lazy val jvm = project
.in(file("jvm"))
.settings(
scalaVersion := scalaV,
scalaJSProjects := Seq(js),
pipelineStages := Seq(scalaJSProd),
libraryDependencies += "com.vmunier" %% "play-scalajs-scripts" % "0.5.0")
libraryDependencies ++= Seq(
"com.vmunier" %% "play-scalajs-scripts" % "0.5.0",
"io.github.jto" %% "validation-core" % validationVersion,
"io.github.jto" %% "validation-playjson" % validationVersion,
"io.github.jto" %% "validation-jsonast" % validationVersion))
.enablePlugins(PlayScala)
.aggregate(js)
.dependsOn(sharedJVM)
// To be used instead of `ProjectRef`s if compiled in isolation:
// .settings(libraryDependencies ++= Seq(
// "io.github.jto" %% "validation-core" % "2.0",
// "io.github.jto" %% "validation-playjson" % "2.0"))
.dependsOn(ProjectRef(file(".."), "validation-core"))
.dependsOn(ProjectRef(file(".."), "validation-playjson"))
.dependsOn(ProjectRef(file(".."), "validation-jsonast"))

lazy val js = project
.in(file("js"))
.settings(
scalaVersion := scalaV,
persistLauncher := true)
persistLauncher := true,
libraryDependencies ++= Seq(
"io.github.jto" %%% "validation-core" % validationVersion,
"io.github.jto" %%% "validation-jsjson" % validationVersion,
"io.github.jto" %%% "validation-jsonast" % validationVersion))
.enablePlugins(ScalaJSPlugin, ScalaJSPlay)
.dependsOn(sharedJS)
// To be used instead of `ProjectRef`s if compiled in isolation:
// .settings(libraryDependencies ++= Seq(
// "io.github.jto" %%% "validation-core" % "2.0",
// "io.github.jto" %%% "validation-jsjson" % "2.0"))
.dependsOn(ProjectRef(file(".."), "validation-core"))
.dependsOn(ProjectRef(file(".."), "validation-jsjson"))
.dependsOn(ProjectRef(file(".."), "validation-jsonast"))

lazy val shared = crossProject.crossType(CrossType.Pure)
.in(file("shared"))
.settings(scalaVersion := scalaV)
.settings(
scalaVersion := scalaV,
libraryDependencies ++= Seq(
"io.github.jto" %%% "validation-core" % validationVersion,
"io.github.jto" %%% "validation-jsonast" % validationVersion))
.jsConfigure(_.enablePlugins(ScalaJSPlay))

lazy val sharedJVM = shared.jvm
Expand Down
20 changes: 8 additions & 12 deletions play-scalajs-example/js/src/main/scala/Validate.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package client

import jto.validation.{To, Format, Valid, Invalid, VA, Formatting}
import jto.validation.jsjson.{Rules, Writes}
import jto.validation._
import jto.validation.jsonast.Ast
import jto.validation.jsjson._
import scala.scalajs.js
import js.annotation.JSExport
import model.User
Expand All @@ -11,17 +12,12 @@ import scala.Function.{unlift, const}
object Validate {
@JSExport
def user(json: js.Dynamic): js.Dynamic = {
import Rules._, Writes._
import Writes._

implicit val format =
Formatting[js.Dynamic, js.Dynamic] { __ =>
(
(__ \ "name").format(notEmpty) ~
(__ \ "age").format(min(0) |+| max(130)) ~
(__ \ "email").format(optionR(email), optionW(stringW)) ~
(__ \ "isAlive").format[Boolean]
)(User.apply, unlift(User.unapply))
}
implicit val format: Format[js.Dynamic, js.Dynamic, User] = Format(
Rule(j => User.format.validate(Ast.from(j))),
Write(u => Ast.to(User.format.writes(u)))
)

To[VA[User], js.Dynamic](format.validate(json))
}
Expand Down
9 changes: 3 additions & 6 deletions play-scalajs-example/jvm/app/controllers/Application.scala
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package controllers

import jto.validation.playjson.Writes
import jto.validation.Write
import jto.validation._
import jto.validation.jsonast._
import play.api.Environment
import play.api.libs.json._
import play.api.mvc._

import model.User

class Application()(implicit environment: Environment) extends Controller {

def index = Action {
import Writes._
val write: Write[User, JsObject] = Write.gen[User, JsObject]
val write: Write[User, JsValue] = Write(u => Ast.to(User.format.writes(u)))
val user: User = User("supercat", 20, Some("e@mail.com"), true)
val json: String = Json.prettyPrint(write.writes(user))
Ok(views.html.index(json))
}

}
17 changes: 17 additions & 0 deletions play-scalajs-example/shared/src/main/scala/User.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
package model

import jto.validation._
import jto.validation.jsonast._
import scala.Function.unlift

case class User(
name: String,
age: Int,
email: Option[String],
isAlive: Boolean
)

object User {
import Rules._, Writes._
implicit val format: Format[JValue, JObject, User] =
Formatting[JValue, JObject] { __ =>
(
(__ \ "name").format(notEmpty) ~
(__ \ "age").format(min(0) |+| max(130)) ~
(__ \ "email").format(optionR(email), optionW(stringW)) ~
(__ \ "isAlive").format[Boolean]
)(User.apply, unlift(User.unapply))
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion misc/ci.sh → scripts/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test_cmd="$sbt_cmd clean test"

coverage="$sbt_cmd clean coverage validationJVM/test coverageReport && sbt coverageAggregate && sbt coveralls"

compile_example="(cd play-scalajs-example; $sbt_cmd compile)"
compile_example="$sbt_cmd publish-local && (cd play-scalajs-example && $sbt_cmd compile)"

compile_doc="bash misc/build-book.sh"

Expand Down
2 changes: 1 addition & 1 deletion validation-jsonast/js/src/main/scala/Ast.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package jto.validation
package jsonAst
package jsonast

import scala.scalajs.js
import scala.scalajs.js.JSConverters._
Expand Down
2 changes: 1 addition & 1 deletion validation-jsonast/jvm/src/main/scala/Ast.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package jto.validation
package jsonAst
package jsonast

import play.api.libs.json._

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package jto.validation
package jsonAst
package jsonast

import scala.collection.Map

Expand Down
2 changes: 1 addition & 1 deletion validation-jsonast/shared/src/main/scala/Rules.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package jto.validation
package jsonAst
package jsonast

object Rules extends DefaultRules[JValue] {
private def jsonAs[T](
Expand Down
2 changes: 1 addition & 1 deletion validation-jsonast/shared/src/main/scala/Writes.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package jto.validation
package jsonAst
package jsonast

import cats.Monoid

Expand Down

0 comments on commit bd842d1

Please sign in to comment.