-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #305 from ysusuk/master
#88: Add JsonPath predicate
- Loading branch information
Showing
4 changed files
with
63 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
modules/jsonpath/jvm/src/main/scala/eu/timepit/refined/jsonpath/string.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
modules/jsonpath/jvm/src/test/scala/eu/timepit/refined/jsonpath/StringValidateSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 '..'" | ||
} | ||
|
||
} |