Skip to content

Commit

Permalink
Merge pull request #305 from ysusuk/master
Browse files Browse the repository at this point in the history
#88: Add JsonPath predicate
  • Loading branch information
fthomas committed Jul 7, 2017
2 parents 14888f1 + 478d0f3 commit 1b659a1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
19 changes: 16 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ val scalaXmlVersion = "1.0.6"
val scalazVersion = "7.2.13"
val scodecVersion = "1.10.3"
val pureconfigVersion = "0.7.2"
val jsonpathVersion = "2.3.0"

// needed for tests with Scala 2.10
val macroParadise = compilerPlugin(
"org.scalamacros" % "paradise" % macroParadiseVersion % Test cross CrossVersion.patch)

val allSubprojects =
Seq("cats", "core", "eval", "scalacheck", "scalaz", "scodec", "pureconfig")
Seq("cats", "core", "eval", "scalacheck", "scalaz", "scodec", "pureconfig", "jsonpath")
val allSubprojectsJVM = allSubprojects.map(_ + "JVM")
val allSubprojectsJS = {
val jvmOnlySubprojects = Seq("pureconfig")
val jvmOnlySubprojects = Seq("pureconfig", "jsonpath")
(allSubprojects diff jvmOnlySubprojects).map(_ + "JS")
}

Expand All @@ -48,7 +49,8 @@ lazy val root = project
scalazJS,
scodecJVM,
scodecJS,
pureconfigJVM)
pureconfigJVM,
jsonpathJVM)
.settings(commonSettings)
.settings(noPublishSettings)
.settings(releaseSettings)
Expand Down Expand Up @@ -179,6 +181,17 @@ lazy val pureconfig = crossProject(JSPlatform, JVMPlatform)

lazy val pureconfigJVM = pureconfig.jvm

lazy val jsonpath = crossProject(JSPlatform, JVMPlatform)
.configureCross(moduleCrossConfig("jsonpath"))
.dependsOn(core % "compile->compile;test->test")
.settings(
libraryDependencies ++= Seq(
"com.jayway.jsonpath" % "json-path" % jsonpathVersion,
macroParadise
)
)

lazy val jsonpathJVM = jsonpath.jvm
/// settings

lazy val commonSettings = Def.settings(
Expand Down
1 change: 1 addition & 0 deletions latestVersion.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ latestVersionInSeries in ThisBuild := Some("0.8.2")
unreleasedModules in ThisBuild := Set(
// Example:
// "refined-eval"
"refined-jsonpath"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package eu.timepit.refined
package jsonpath

import eu.timepit.refined.api.Validate

object string extends StringValidate {

/** Predicate that checks if a `String` is a valid JSON path. */
final case class JSONPath()
}

private[refined] trait StringValidate {
import string._

implicit def jsonPathValidate: Validate.Plain[String, JSONPath] = {
import com.jayway.jsonpath.internal.path.PathCompiler.compile

def compileWithoutPredicates(str: String) = compile(str)

Validate
.fromPartial(compileWithoutPredicates, "JSONPath", JSONPath())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package eu.timepit.refined
package jsonpath

import eu.timepit.refined.TestUtils._
import eu.timepit.refined.jsonpath.string._
import org.scalacheck.Prop._
import org.scalacheck.Properties

class StringValidateSpec extends Properties("JSONPathStringValidate") {

property("JSONPath is valid") = secure {
isValid[JSONPath]("$") && isValid[JSONPath]("@")
}

property("Illegal character at position 1 expected '.' or '[") = secure {
showResult[JSONPath]("$X") ?= "JSONPath predicate failed: Illegal character at position 1 expected '.' or '["
}

property("Path must not end with a '.' or '..'") = secure {
showResult[JSONPath]("$.") ?= "JSONPath predicate failed: Path must not end with a '.' or '..'"
}

}

0 comments on commit 1b659a1

Please sign in to comment.