Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ATL-1342] feat(pollux): Added JSON Schema #44

Merged
merged 1 commit into from
Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions pollux/vc-jwt/project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@ object Dependencies {
val jwtCirceVersion = "9.1.1"
}

private lazy val coreJwtCirce = "io.circe" %% "circe-core" % Versions.circeVersion
private lazy val genericJwtCirce = "io.circe" %% "circe-generic" % Versions.circeVersion
private lazy val parserJwtCirce = "io.circe" %% "circe-parser" % Versions.circeVersion
private lazy val coreJwtCirce = ("io.circe" %% "circe-core" % Versions.circeVersion).cross(CrossVersion.for3Use2_13)
private lazy val genericJwtCirce =
("io.circe" %% "circe-generic" % Versions.circeVersion).cross(CrossVersion.for3Use2_13)
private lazy val parserJwtCirce =
("io.circe" %% "circe-parser" % Versions.circeVersion).cross(CrossVersion.for3Use2_13)

private lazy val jwtCirce = "com.github.jwt-scala" %% "jwt-circe" % Versions.jwtCirceVersion
private lazy val circeJsonSchema =
("net.reactivecore" %% "circe-json-schema" % "0.3.0").cross(CrossVersion.for3Use2_13)

private lazy val jwtCirce =
("com.github.jwt-scala" %% "jwt-circe" % Versions.jwtCirceVersion).cross(CrossVersion.for3Use2_13)

private lazy val test = "org.scalameta" %% "munit" % "0.7.29" % Test

// Dependency Modules
private lazy val circeDependencies: Seq[ModuleID] = Seq(coreJwtCirce, genericJwtCirce, parserJwtCirce)
private lazy val baseDependencies: Seq[ModuleID] = circeDependencies :+ jwtCirce :+ test
private lazy val baseDependencies: Seq[ModuleID] = circeDependencies :+ jwtCirce :+ circeJsonSchema :+ test

// Project Dependencies
lazy val polluxVcJwtDependencies: Seq[ModuleID] = baseDependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import pdi.jwt.{JwtAlgorithm, JwtCirce}
import java.security.*
import java.security.spec.*
import java.time.Instant
import net.reactivecore.cjs.Loader

import net.reactivecore.cjs.{DocumentValidator, Loader, Result}
import net.reactivecore.cjs.resolver.Downloader
import cats.implicits._
import io.circe.Json

@main def jwtDemo(): Unit =
val keyGen = KeyPairGenerator.getInstance("EC")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package io.iohk.atala.pollux.vc.jwt

import cats.implicits.*
import io.circe.*
import net.reactivecore.cjs.resolver.Downloader
import net.reactivecore.cjs.{DocumentValidator, Loader, Result}
import pdi.jwt.{JwtAlgorithm, JwtCirce, JwtClaim}

import java.security.*
import java.security.spec.*
import java.time.Instant

@main def schemaDemo(): Unit =
val schemaCode =
"""
|{
| "type": "object",
| "properties": {
| "userName": {
| "$ref": "#/$defs/user"
| },
| "age": {
| "$ref": "#/$defs/age"
| },
| "email": {
| "$ref": "#/$defs/email"
| }
| },
| "required": ["userName", "age", "email"],
| "$defs": {
| "user": {
| "type": "string",
| "minLength": 3
| },
| "age": {
| "type": "number"
| },
| "email": {
| "type": "string",
| "format": "email"
| }
| }
|}
|""".stripMargin

val validator = Loader.empty.fromJson(schemaCode)

def test(s: Json): Unit = {
val result = validator.right.get.validate(s)
println(s"Result of ${s}: ${result}")
}

test(Json.fromString("wrongType"))
test(
Json.obj(
"userName" -> Json.fromString("Bob"),
"age" -> Json.fromInt(42)
)
)

// Missing UserName
test(
Json.obj(
"age" -> Json.fromInt(42),
"email" -> Json.fromString("[email protected]")
)
)

// Age has Wrong type
test(
Json.obj(
"userName" -> Json.fromString("Bob"),
"age" -> Json.fromBoolean(false),
"email" -> Json.fromString("[email protected]")
)
)

// Success
test(
Json.obj(
"userName" -> Json.fromString("Bob"),
"age" -> Json.fromInt(42),
"email" -> Json.fromString("email")
)
)