-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ATL-5575 Generalize and Streamline Json Schema SerDes logic (#653)
Signed-off-by: Bassam Riman <[email protected]>
- Loading branch information
1 parent
70b2f16
commit eb4f8f4
Showing
16 changed files
with
178 additions
and
90 deletions.
There are no files selected for viewing
11 changes: 5 additions & 6 deletions
11
...lib/core/src/main/scala/io/iohk/atala/pollux/core/model/error/CredentialSchemaError.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 |
---|---|---|
@@ -1,18 +1,17 @@ | ||
package io.iohk.atala.pollux.core.model.error | ||
|
||
import io.iohk.atala.pollux.core.model.schema.validator.JsonSchemaError | ||
|
||
sealed trait CredentialSchemaError { | ||
def userMessage: String | ||
} | ||
|
||
object CredentialSchemaError { | ||
case class SchemaError(schemaError: JsonSchemaError) extends CredentialSchemaError { | ||
def userMessage: String = schemaError.error | ||
} | ||
case class URISyntaxError(userMessage: String) extends CredentialSchemaError | ||
case class CredentialSchemaParsingError(userMessage: String) extends CredentialSchemaError | ||
case class UnsupportedCredentialSchemaType(userMessage: String) extends CredentialSchemaError | ||
case class JsonSchemaParsingError(userMessage: String) extends CredentialSchemaError | ||
case class UnsupportedJsonSchemaSpecVersion(userMessage: String) extends CredentialSchemaError | ||
case class ClaimsParsingError(userMessage: String) extends CredentialSchemaError | ||
case class ClaimsValidationError(errors: Seq[String]) extends CredentialSchemaError { | ||
def userMessage: String = errors.mkString(";") | ||
} | ||
case class UnexpectedError(userMessage: String) extends CredentialSchemaError | ||
} |
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
20 changes: 11 additions & 9 deletions
20
.../core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/AnoncredSchemaType.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
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
5 changes: 3 additions & 2 deletions
5
...ore/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/CredentialSchemaType.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 |
---|---|---|
@@ -1,10 +1,11 @@ | ||
package io.iohk.atala.pollux.core.model.schema.`type` | ||
|
||
import io.iohk.atala.pollux.core.model.error.CredentialSchemaError | ||
import io.iohk.atala.pollux.core.model.schema.Schema | ||
import io.iohk.atala.pollux.core.model.schema.validator.JsonSchemaError | ||
import zio.IO | ||
|
||
trait CredentialSchemaType { | ||
val `type`: String | ||
def validate(schema: Schema): IO[CredentialSchemaError, Unit] | ||
|
||
def validate(schema: Schema): IO[JsonSchemaError, Unit] | ||
} |
11 changes: 0 additions & 11 deletions
11
...la/io/iohk/atala/pollux/core/model/schema/type/anoncred/AnoncredSchemaSchemaVersion.scala
This file was deleted.
Oops, something went wrong.
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
13 changes: 0 additions & 13 deletions
13
...in/scala/io/iohk/atala/pollux/core/model/schema/validator/CredentialSchemaValidator.scala
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
...ore/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/JsonSchemaError.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,17 @@ | ||
package io.iohk.atala.pollux.core.model.schema.validator | ||
|
||
sealed trait JsonSchemaError { | ||
def error: String | ||
} | ||
|
||
object JsonSchemaError { | ||
case class JsonSchemaParsingError(error: String) extends JsonSchemaError | ||
|
||
case class JsonValidationErrors(errors: Seq[String]) extends JsonSchemaError { | ||
def error: String = errors.mkString(";") | ||
} | ||
|
||
case class UnsupportedJsonSchemaSpecVersion(error: String) extends JsonSchemaError | ||
|
||
case class UnexpectedError(error: String) extends JsonSchemaError | ||
} |
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
12 changes: 12 additions & 0 deletions
12
...src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/JsonSchemaValidator.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,12 @@ | ||
package io.iohk.atala.pollux.core.model.schema.validator | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import zio.* | ||
|
||
trait JsonSchemaValidator { | ||
def validate(claims: String): IO[JsonSchemaError, Unit] | ||
|
||
def validate(claimsJsonNode: JsonNode): IO[JsonSchemaError, Unit] = { | ||
validate(claimsJsonNode.toString) | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
...b/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/SchemaSerDes.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,53 @@ | ||
package io.iohk.atala.pollux.core.model.schema.validator | ||
|
||
import com.networknt.schema.JsonSchema | ||
import io.iohk.atala.pollux.core.model.schema.validator.JsonSchemaError.* | ||
import zio.IO | ||
import zio.ZIO | ||
import zio.json.* | ||
import zio.json.JsonDecoder | ||
import zio.json.ast.Json | ||
import zio.json.ast.Json.* | ||
|
||
class SchemaSerDes[S](jsonSchemaSchemaStr: String) { | ||
|
||
def initialiseJsonSchema: IO[JsonSchemaError, JsonSchema] = | ||
JsonSchemaUtils.jsonSchema(jsonSchemaSchemaStr) | ||
|
||
def deserialize( | ||
schema: zio.json.ast.Json | ||
)(using decoder: JsonDecoder[S]): IO[JsonSchemaError, S] = { | ||
deserialize(schema.toString()) | ||
} | ||
|
||
def deserialize( | ||
jsonString: String | ||
)(using decoder: JsonDecoder[S]): IO[JsonSchemaError, S] = { | ||
for { | ||
_ <- validate(jsonString) | ||
anoncredSchema <- | ||
ZIO | ||
.fromEither(decoder.decodeJson(jsonString)) | ||
.mapError(JsonSchemaError.JsonSchemaParsingError.apply) | ||
} yield anoncredSchema | ||
} | ||
|
||
def deserializeAsJson(jsonString: String): IO[JsonSchemaError, Json] = { | ||
for { | ||
_ <- validate(jsonString) | ||
json <- | ||
ZIO | ||
.fromEither(jsonString.fromJson[Json]) | ||
.mapError(JsonSchemaError.JsonSchemaParsingError.apply) | ||
} yield json | ||
} | ||
|
||
def validate(jsonString: String): IO[JsonSchemaError, Unit] = { | ||
for { | ||
jsonSchemaSchema <- JsonSchemaUtils.jsonSchema(jsonSchemaSchemaStr) | ||
schemaValidator = JsonSchemaValidatorImpl(jsonSchemaSchema) | ||
_ <- schemaValidator.validate(jsonString) | ||
} yield {} | ||
} | ||
|
||
} |
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
Oops, something went wrong.