akka-http-json-validation


License

License

MIT
Categories

Categories

JSON Data Akka Container Microservices Reactive libraries
GroupId

GroupId

com.github.uryyyyyyy
ArtifactId

ArtifactId

akka-http-json-validation_2.12
Last Version

Last Version

0.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

akka-http-json-validation
akka-http-json-validation
Project URL

Project URL

https://github.com/uryyyyyyy/akka-http-json-validation
Project Organization

Project Organization

com.github.uryyyyyyy
Source Code Management

Source Code Management

https://github.com/uryyyyyyy/akka-http-json-validation

Download akka-http-json-validation_2.12

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.uryyyyyyy/akka-http-json-validation_2.12/ -->
<dependency>
    <groupId>com.github.uryyyyyyy</groupId>
    <artifactId>akka-http-json-validation_2.12</artifactId>
    <version>0.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.uryyyyyyy/akka-http-json-validation_2.12/
implementation 'com.github.uryyyyyyy:akka-http-json-validation_2.12:0.2.0'
// https://jarcasting.com/artifacts/com.github.uryyyyyyy/akka-http-json-validation_2.12/
implementation ("com.github.uryyyyyyy:akka-http-json-validation_2.12:0.2.0")
'com.github.uryyyyyyy:akka-http-json-validation_2.12:jar:0.2.0'
<dependency org="com.github.uryyyyyyy" name="akka-http-json-validation_2.12" rev="0.2.0">
  <artifact name="akka-http-json-validation_2.12" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.uryyyyyyy', module='akka-http-json-validation_2.12', version='0.2.0')
)
libraryDependencies += "com.github.uryyyyyyy" % "akka-http-json-validation_2.12" % "0.2.0"
[com.github.uryyyyyyy/akka-http-json-validation_2.12 "0.2.0"]

Dependencies

compile (3)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.12.7
com.typesafe.akka : akka-http_2.12 jar 10.1.5
com.typesafe.akka : akka-http-spray-json_2.12 jar 10.1.5

provided (1)

Group / Artifact Type Version
com.typesafe.akka : akka-stream_2.12 jar 2.5.18

test (2)

Group / Artifact Type Version
com.typesafe.akka : akka-http-testkit_2.12 jar 10.1.5
org.scalatest : scalatest_2.12 jar 3.0.5

Project Modules

There are no modules declared in this project.

akka-http-json-validation

inspired by http://fruzenshtein.com/akka-http-another-one-validation-directive/

  • 2.11 Maven Central
  • 2.12 Maven Central

Usage

see example

setup

first, create class and validation.

case class Tag(
  id: Int,
  text: String
)

object TagValidator extends Validator[Tag] {

  private def idRule: Validation =
    genValidation(
      "id",
      tag => tag.id <= 0,
      "id must be positive"
    )

  private def textRule: Validation =
    genValidation(
      "text",
      tag => tag.text.isEmpty,
      "text must not be empty"
    )

  val validations = Seq(idRule, textRule)
}

then, create validation Directives

import com.github.uryyyyyyy.akkahttp.validation.ValidationDirectiveBase

trait CustomValidationDirectives extends ValidationDirectiveBase {
  implicit val tagV = TagValidator
}

then, extends its Directives & use directive method

object Main extends CustomValidationDirectives with CustomJsonFormat {

  val route = post {
    path("tag") {
      validateModel(asV[Tag]) { validatedTag =>
        complete(validatedTag)
      }
    }
  }
}

verify

(you can run example)

normal object

# valid pattern
$ curl --silent -X POST -H "Content-Type: application/json" http://localhost:9000/tag -d '{"id": 1, "text": "tag1"}' | jq

# invalid pattern
$ curl --silent -X POST -H "Content-Type: application/json" http://localhost:9000/tag -d '{"id": -1, "text": ""}' | jq
{
  "error": "validation error.",
  "details": {
    "id": "id must be positive",
    "text": "text must not be empty"
  }
}

array object

# valid pattern
$ curl --silent -X POST -H "Content-Type: application/json" http://localhost:9000/tags -d '[{"id": 1, "text": "tag1"}]' | jq

# invalid pattern 1
$ curl --silent -X POST -H "Content-Type: application/json" http://localhost:9000/tags -d '[{"id": 1, "text": "tag1"}, {"id": -1, "text": ""}]' | jq
{
  "error": "validation error.",
  "details": {
    "1": {
      "id": "id must be positive",
      "text": "text must not be empty"
    }
  }
}

# invalid pattern 2
$ curl --silent -X POST -H "Content-Type: application/json" http://localhost:9000/tags -d '[]' | jq
{
  "error": "validation error.",
  "details": [
    "list must not be empty",
    "must not be empty2. (if you want, you can set more validation)"
  ]
}

# invalid pattern 3
$ curl --silent -X POST -H "Content-Type: application/json" http://localhost:9000/tags -d '[{"id": 1, "text": "tag1"}, {"id": 2, "text": "tag2"}, {"id": 3, "text": "tag3"}]' | jq
{
  "error": "validation error.",
  "details": [
    "list must be less than 3"
  ]
}

nested object

# valid pattern
$ curl --silent -X POST -H "Content-Type: application/json" http://localhost:9000/nestedTag -d '{"tag": {"id": 1, "text": "tag1"}, "tags": [{"id": 2, "text": "tag2"}]}' | jq

# invalid pattern
$ curl --silent -X POST -H "Content-Type: application/json" http://localhost:9000/nestedTag -d '{"tag": {"id": -1, "text": "tag1"}, "tags": []}' | jq
{
  "error": "validation error.",
  "details": {
    "tag": {
      "id": "id must be positive"
    },
    "tags": [
      "list must not be empty",
      "must not be empty2. (if you want, you can set more validation)"
    ]
  }
}

Versions

Version
0.2.0
0.1.0
0.0.1