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

Commit 14fb41c

Browse files
authored
Merge pull request #62 from OlivierBlanvillain/json-ast
Replace json4s-ast with custom AST
2 parents 96d46ab + e46f04b commit 14fb41c

File tree

25 files changed

+375
-200
lines changed

25 files changed

+375
-200
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ notifications:
77
email:
88
false
99

10-
script: bash misc/ci.sh
10+
script: bash scripts/ci.sh
1111

1212
cache:
1313
directories:

PUBLISH.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
```sh
1212
git checkout gh-pages
1313
git checkout master .
14-
sh misc/build-book.sh
14+
sh scripts/build-book.sh
1515
git add .
1616
git commit -am "Update book"
1717
git push

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ val validationVersion = "2.0"
6060
libraryDependencies ++= Seq(
6161
"io.github.jto" %% "validation-core" % validationVersion,
6262
"io.github.jto" %% "validation-playjson" % validationVersion,
63-
"io.github.jto" %% "validation-json4s" % validationVersion,
63+
"io.github.jto" %% "validation-jsonast" % validationVersion,
6464
"io.github.jto" %% "validation-form" % validationVersion,
6565
"io.github.jto" %% "validation-delimited" % validationVersion,
6666
"io.github.jto" %% "validation-xml" % validationVersion
67+
// "io.github.jto" %%% "validation-jsjson" % validationVersion
6768
)
6869
```
6970

build.sbt

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,16 @@ val license = ("Apache License", url("http://www.apache.org/licenses/LICENSE-2.0
66
val catsVersion = "0.6.0"
77
val jodaConvertVersion = "1.8.1"
88
val jodaTimeVersion = "2.9.4"
9-
val json4sAstVersion = "4.0.0-M1"
109
val kindProjectorVersion = "0.7.1"
1110
val parserCombinatorsVersion = "1.0.2"
1211
val playVersion = "2.5.3"
1312
val scalacVersion = "2.11.8"
1413
val scalatestVersion = "3.0.0-M16-SNAP6"
1514
val scalaXmlVersion = "1.0.5"
1615

17-
val json4sAST = libraryDependencies += "org.json4s" %%% "json4s-ast" % json4sAstVersion
18-
1916
lazy val root = aggregate("validation", validationJVM, validationJS, docs).in(file("."))
20-
lazy val validationJVM = aggregate("validationJVM", coreJVM, formJVM, delimitedJVM, json4sJVM, `validation-playjson`, `validation-xml`, `date-tests`)
21-
lazy val validationJS = aggregate("validationJS", coreJS, formJS, delimitedJS, json4sJS, `validation-jsjson`)
17+
lazy val validationJVM = aggregate("validationJVM", coreJVM, formJVM, delimitedJVM, jsonAstJVM, `validation-playjson`, `validation-xml`, `date-tests`)
18+
lazy val validationJS = aggregate("validationJS", coreJS, formJS, delimitedJS, jsonAstJS, `validation-jsjson`)
2219

2320
lazy val `validation-core` = crossProject
2421
.crossType(CrossType.Pure)
@@ -48,14 +45,15 @@ lazy val delimitedJVM = `validation-delimited`.jvm
4845
lazy val delimitedJS = `validation-delimited`.js
4946
lazy val delimited = aggregate("validation-delimited", delimitedJVM, delimitedJS)
5047

51-
lazy val `validation-json4s` = crossProject
52-
.crossType(CrossType.Pure)
48+
lazy val `validation-jsonast` = crossProject
49+
.crossType(CrossType.Full)
5350
.settings(validationSettings: _*)
54-
.settings(json4sAST)
5551
.dependsOn(`validation-core`)
56-
lazy val json4sJVM = `validation-json4s`.jvm
57-
lazy val json4sJS = `validation-json4s`.js
58-
lazy val json4s = aggregate("validation-json4s", json4sJVM, json4sJS)
52+
.jvmSettings(libraryDependencies +=
53+
"com.typesafe.play" %% "play-json" % playVersion)
54+
lazy val jsonAstJVM = `validation-jsonast`.jvm
55+
lazy val jsonAstJS = `validation-jsonast`.js
56+
lazy val jsonAst = aggregate("validation-jsonast", jsonAstJVM, jsonAstJS)
5957

6058
lazy val `validation-playjson` = project
6159
.settings(validationSettings: _*)
@@ -80,13 +78,12 @@ lazy val docs = project
8078
.settings(crossTarget := file(".") / "docs" / "target")
8179
.settings(tutSettings: _*)
8280
.settings(scalacOptions -= "-Ywarn-unused-import")
83-
.dependsOn(coreJVM, formJVM, delimitedJVM, json4sJVM, `validation-playjson`, `validation-xml`)
81+
.dependsOn(coreJVM, formJVM, delimitedJVM, jsonAstJVM, `validation-playjson`, `validation-xml`)
8482

8583
lazy val `date-tests` = project
8684
.settings(validationSettings: _*)
8785
.settings(dontPublish: _*)
88-
.settings(json4sAST)
89-
.dependsOn(coreJVM, formJVM, json4sJVM, `validation-playjson`, `validation-xml`)
86+
.dependsOn(coreJVM, formJVM, jsonAstJVM, `validation-playjson`, `validation-xml`)
9087

9188
def aggregate(name: String, projects: ProjectReference*): Project =
9289
Project(name, file("." + name))
@@ -104,7 +101,7 @@ lazy val settings = Seq(
104101
testOptions in Test += Tests.Argument(TestFrameworks.ScalaTest, "-oDF"),
105102
scalaJSStage in Global := FastOptStage,
106103
parallelExecution := false
107-
) ++ reformatOnCompileSettings
104+
)
108105

109106
val commonScalacOptions = Seq(
110107
"-deprecation",

date-tests/src/test/scala/DateSpec.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,8 @@ class DateSpec extends WordSpec with Matchers {
480480
}
481481
}
482482

483-
"json4s" should {
484-
import jto.validation.json4s._
485-
import org.json4s.ast.safe._
483+
"jsonAst" should {
484+
import jto.validation.jsonast._
486485
import Rules._, Writes._
487486

488487
"Format" when {
@@ -540,7 +539,7 @@ class DateSpec extends WordSpec with Matchers {
540539
"time" in {
541540
Formatting[JValue, JObject] { __ =>
542541
(__ \ "n").format(jodaTimeR, jodaTimeW)
543-
}.validate(JObject(Map("n" -> JNumber(dd.getTime)))) shouldBe
542+
}.validate(JObject(Map("n" -> JNumber(dd.getTime.toString)))) shouldBe
544543
(Valid(jd))
545544

546545
Formatting[JValue, JObject] { __ =>

docs/src/main/tut/ReleaseNotes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
- Impove error reporting ([#40](https://github.com/jto/validation/pull/40) [1](https://github.com/jto/validation/commit/357b87778f19fbbc06a49da08cb2dccf9e0a40e3), [2](https://github.com/jto/validation/commit/c1bdac7fcff098b1d85c6881c731d5fd4ee2ac2e))
88

9-
- Rework project structure: `json``playjson`, `json4s``json-ast`, `jsjson` ([#50](https://github.com/jto/validation/pull/50) [0](https://github.com/jto/validation/commit/f95ac30b1d1346a27e26c08841ee06c00340891f) [1](https://github.com/jto/validation/commit/5b36f606334a5fe26715cf0d7c47ebf861acb811), [2](https://github.com/jto/validation/commit/3f31f4917d01b8f6fdefe4adaca70ddc823722db))
9+
- Rework project structure: `json``playjson`, `json4s``jsonast`, `jsjson` ([#50](https://github.com/jto/validation/pull/50) [0](https://github.com/jto/validation/commit/f95ac30b1d1346a27e26c08841ee06c00340891f) [1](https://github.com/jto/validation/commit/5b36f606334a5fe26715cf0d7c47ebf861acb811), [2](https://github.com/jto/validation/commit/3f31f4917d01b8f6fdefe4adaca70ddc823722db))
1010

1111
- Add Scala.js support ([#42](https://github.com/jto/validation/pull/42) [1](https://github.com/jto/validation/commit/db359abfbe90d2b3b853beabcbabe88ecd1cfddb), [2](https://github.com/jto/validation/commit/568aa1fa1df06d775abb583cac8da679c1301227), [3](https://github.com/jto/validation/commit/d67d6dee7d99d27d6cf751cc69b83235b57a8246), [4](https://github.com/jto/validation/commit/67499a823ff463860b72d6697cf45b5764c475b2), [5](https://github.com/jto/validation/commit/d720ba265541a90f225c388043b5430a68e9fff3))

docs/src/main/tut/V2MigrationGuide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ becomes
2121
#### Package name
2222

2323
- Since the library does not depend on Play anymore and is not planned to be integrated into Play, the package names have changed. Basically `play.api.mapping` now becomes `jto.validation`. A simple search and replace in your project should work.
24-
- The validation api support both json4s and play-json. Therefore, the package name for play json changes. `play.api.mapping.json` becomes `play.api.mapping.playjson`
24+
- The validation api support several json representations. Therefore, the package name for play json changes. `play.api.mapping.json` becomes `play.api.mapping.playjson`
2525

2626
#### Rule renaming
2727

play-scalajs-example/build.sbt

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
11
val scalaV = "2.11.8"
22

3+
val validationVersion = "2.0"
4+
35
lazy val jvm = project
46
.in(file("jvm"))
57
.settings(
68
scalaVersion := scalaV,
79
scalaJSProjects := Seq(js),
810
pipelineStages := Seq(scalaJSProd),
9-
libraryDependencies += "com.vmunier" %% "play-scalajs-scripts" % "0.5.0")
11+
libraryDependencies ++= Seq(
12+
"com.vmunier" %% "play-scalajs-scripts" % "0.5.0",
13+
"io.github.jto" %% "validation-core" % validationVersion,
14+
"io.github.jto" %% "validation-playjson" % validationVersion,
15+
"io.github.jto" %% "validation-jsonast" % validationVersion))
1016
.enablePlugins(PlayScala)
1117
.aggregate(js)
1218
.dependsOn(sharedJVM)
13-
// To be used instead of `ProjectRef`s if compiled in isolation:
14-
// .settings(libraryDependencies ++= Seq(
15-
// "io.github.jto" %% "validation-core" % "2.0",
16-
// "io.github.jto" %% "validation-playjson" % "2.0"))
17-
.dependsOn(ProjectRef(file(".."), "validation-core"))
18-
.dependsOn(ProjectRef(file(".."), "validation-playjson"))
1919

2020
lazy val js = project
2121
.in(file("js"))
2222
.settings(
2323
scalaVersion := scalaV,
24-
persistLauncher := true)
24+
persistLauncher := true,
25+
libraryDependencies ++= Seq(
26+
"io.github.jto" %%% "validation-core" % validationVersion,
27+
"io.github.jto" %%% "validation-jsjson" % validationVersion,
28+
"io.github.jto" %%% "validation-jsonast" % validationVersion))
2529
.enablePlugins(ScalaJSPlugin, ScalaJSPlay)
2630
.dependsOn(sharedJS)
27-
// To be used instead of `ProjectRef`s if compiled in isolation:
28-
// .settings(libraryDependencies ++= Seq(
29-
// "io.github.jto" %%% "validation-core" % "2.0",
30-
// "io.github.jto" %%% "validation-jsjson" % "2.0"))
31-
.dependsOn(ProjectRef(file(".."), "validation-core"))
32-
.dependsOn(ProjectRef(file(".."), "validation-jsjson"))
3331

3432
lazy val shared = crossProject.crossType(CrossType.Pure)
3533
.in(file("shared"))
36-
.settings(scalaVersion := scalaV)
34+
.settings(
35+
scalaVersion := scalaV,
36+
libraryDependencies ++= Seq(
37+
"io.github.jto" %%% "validation-core" % validationVersion,
38+
"io.github.jto" %%% "validation-jsonast" % validationVersion))
3739
.jsConfigure(_.enablePlugins(ScalaJSPlay))
3840

3941
lazy val sharedJVM = shared.jvm
Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package client
22

3-
import jto.validation.{ Format, Valid, Invalid, VA, Formatting }
4-
import jto.validation.jsjson.{ Rules, Writes }
3+
import jto.validation._
4+
import jto.validation.jsonast.Ast
5+
import jto.validation.jsjson._
56
import scala.scalajs.js
67
import js.annotation.JSExport
78
import model.User
@@ -10,25 +11,14 @@ import scala.Function.{unlift, const}
1011
@JSExport
1112
object Validate {
1213
@JSExport
13-
def user(json: js.Dynamic): js.Dictionary[Any] = {
14-
import Rules._, Writes._
14+
def user(json: js.Dynamic): js.Dynamic = {
15+
import Writes._
1516

16-
val format =
17-
Formatting[js.Dynamic, js.Dynamic] { __ =>
18-
(
19-
(__ \ "name").format(notEmpty) ~
20-
(__ \ "age").format(min(0) |+| max(130)) ~
21-
(__ \ "email").format(optionR(email), optionW(stringW)) ~
22-
(__ \ "isAlive").format[Boolean]
23-
)(User.apply, unlift(User.unapply))
24-
}
25-
26-
val validated: VA[User] = format.validate(json)
27-
28-
js.Dictionary(
29-
"isValid" -> validated.isValid,
30-
"output" -> validated.fold(const(null), format.writes),
31-
"errors" -> validated.fold(e => failureW.writes(Invalid(e)), const(null))
17+
implicit val format: Format[js.Dynamic, js.Dynamic, User] = Format(
18+
Ast.from andThen User.format,
19+
Write.toWrite(User.format) andThen Ast.to
3220
)
21+
22+
To[VA[User], js.Dynamic](format.validate(json))
3323
}
3424
}
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
package controllers
22

3-
import jto.validation.playjson.Writes
4-
import jto.validation.Write
3+
import jto.validation._
4+
import jto.validation.jsonast._
55
import play.api.Environment
66
import play.api.libs.json._
77
import play.api.mvc._
88

99
import model.User
1010

1111
class Application()(implicit environment: Environment) extends Controller {
12-
1312
def index = Action {
14-
import Writes._
15-
val write: Write[User, JsObject] = Write.gen[User, JsObject]
13+
val write: Write[User, JsValue] = Write.toWrite(User.format) andThen Ast.to
1614
val user: User = User("supercat", 20, Some("[email protected]"), true)
1715
val json: String = Json.prettyPrint(write.writes(user))
1816
Ok(views.html.index(json))
1917
}
20-
2118
}

0 commit comments

Comments
 (0)