diff --git a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/error/CredentialSchemaError.scala b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/error/CredentialSchemaError.scala index d62eed2912..b1557c3503 100644 --- a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/error/CredentialSchemaError.scala +++ b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/error/CredentialSchemaError.scala @@ -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 } diff --git a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/CredentialSchema.scala b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/CredentialSchema.scala index a328f3ec89..68bc2d00f7 100644 --- a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/CredentialSchema.scala +++ b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/CredentialSchema.scala @@ -2,19 +2,18 @@ package io.iohk.atala.pollux.core.model.schema import io.iohk.atala.pollux.core.model.error.CredentialSchemaError import io.iohk.atala.pollux.core.model.error.CredentialSchemaError.* -import io.iohk.atala.pollux.core.model.schema.`type`.{ - AnoncredSchemaType, - CredentialJsonSchemaType, - CredentialSchemaType -} -import io.iohk.atala.pollux.core.model.schema.validator.CredentialJsonSchemaValidator +import io.iohk.atala.pollux.core.model.schema.`type`.AnoncredSchemaType +import io.iohk.atala.pollux.core.model.schema.`type`.CredentialJsonSchemaType +import io.iohk.atala.pollux.core.model.schema.`type`.CredentialSchemaType +import io.iohk.atala.pollux.core.model.schema.validator.JsonSchemaValidatorImpl import io.iohk.atala.pollux.core.service.URIDereferencer import zio.* import zio.json.* import zio.prelude.Validation import java.net.URI -import java.time.{OffsetDateTime, ZoneOffset} +import java.time.OffsetDateTime +import java.time.ZoneOffset import java.util.UUID type Schema = zio.json.ast.Json @@ -133,8 +132,8 @@ object CredentialSchema { ) )(resolvedSchemaType.`type`)(`type` => `type` == CredentialJsonSchemaType.`type`) .toZIO - schemaValidator <- CredentialJsonSchemaValidator.from(vcSchema.schema) - _ <- schemaValidator.validate(claims) + schemaValidator <- JsonSchemaValidatorImpl.from(vcSchema.schema).mapError(SchemaError.apply) + _ <- schemaValidator.validate(claims).mapError(SchemaError.apply) } yield () } @@ -152,7 +151,7 @@ object CredentialSchema { def validateCredentialSchema(vcSchema: CredentialSchema): IO[CredentialSchemaError, Unit] = { for { resolvedSchemaType <- resolveCredentialSchemaType(vcSchema.`type`) - _ <- resolvedSchemaType.validate(vcSchema.schema) + _ <- resolvedSchemaType.validate(vcSchema.schema).mapError(SchemaError.apply) } yield () } diff --git a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/AnoncredSchemaType.scala b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/AnoncredSchemaType.scala index e901bf9ee9..10c53f6f5f 100644 --- a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/AnoncredSchemaType.scala +++ b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/AnoncredSchemaType.scala @@ -1,23 +1,25 @@ package io.iohk.atala.pollux.core.model.schema.`type` import com.networknt.schema.* -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.`type`.anoncred.{AnoncredSchemaSchemaV1, AnoncredSchemaSchemaVersion} -import io.iohk.atala.pollux.core.model.schema.common.JsonSchemaUtils -import io.iohk.atala.pollux.core.model.schema.validator.CredentialJsonSchemaValidator +import io.iohk.atala.pollux.core.model.schema.`type`.anoncred.AnoncredSchemaSerDesV1 +import io.iohk.atala.pollux.core.model.schema.`type`.anoncred.AnoncredSchemaSerDesV1.* +import io.iohk.atala.pollux.core.model.schema.validator.JsonSchemaError +import io.iohk.atala.pollux.core.model.schema.validator.JsonSchemaUtils +import io.iohk.atala.pollux.core.model.schema.validator.JsonSchemaValidatorImpl +import io.iohk.atala.pollux.core.model.schema.validator.SchemaSerDes import zio.* import zio.json.* object AnoncredSchemaType extends CredentialSchemaType { - private val anoncredSchemaSchemaVersion: AnoncredSchemaSchemaVersion = AnoncredSchemaSchemaV1 - val `type`: String = AnoncredSchemaSchemaV1.version + val anondcredShemaBasedSerDes: SchemaSerDes[AnoncredSchemaSerDesV1] = AnoncredSchemaSerDesV1.schemaSerDes + val `type`: String = AnoncredSchemaSerDesV1.version - override def validate(schema: Schema): IO[CredentialSchemaError, Unit] = { + override def validate(schema: Schema): IO[JsonSchemaError, Unit] = { for { - jsonSchemaSchema <- anoncredSchemaSchemaVersion.initialiseJsonSchema - schemaValidator = CredentialJsonSchemaValidator(jsonSchemaSchema) + jsonSchemaSchema <- anondcredShemaBasedSerDes.initialiseJsonSchema + schemaValidator = JsonSchemaValidatorImpl(jsonSchemaSchema) jsonSchemaNode <- JsonSchemaUtils.toJsonNode(schema) _ <- schemaValidator.validate(jsonSchemaNode) } yield () diff --git a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/CredentialJsonSchemaType.scala b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/CredentialJsonSchemaType.scala index f33c4c5a80..414cab8e12 100644 --- a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/CredentialJsonSchemaType.scala +++ b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/CredentialJsonSchemaType.scala @@ -1,8 +1,8 @@ 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.CredentialJsonSchemaValidator +import io.iohk.atala.pollux.core.model.schema.validator.JsonSchemaError +import io.iohk.atala.pollux.core.model.schema.validator.JsonSchemaValidatorImpl import zio.* import zio.json.* @@ -11,8 +11,8 @@ object CredentialJsonSchemaType extends CredentialSchemaType { override val `type`: String = VC_JSON_SCHEMA_URI - override def validate(schema: Schema): IO[CredentialSchemaError, Unit] = + override def validate(schema: Schema): IO[JsonSchemaError, Unit] = for { - _ <- CredentialJsonSchemaValidator.from(schema) + _ <- JsonSchemaValidatorImpl.from(schema) } yield () } diff --git a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/CredentialSchemaType.scala b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/CredentialSchemaType.scala index 63d4ecc2bc..dccc047b0f 100644 --- a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/CredentialSchemaType.scala +++ b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/CredentialSchemaType.scala @@ -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] } diff --git a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/anoncred/AnoncredSchemaSchemaVersion.scala b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/anoncred/AnoncredSchemaSchemaVersion.scala deleted file mode 100644 index aeada60fd9..0000000000 --- a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/anoncred/AnoncredSchemaSchemaVersion.scala +++ /dev/null @@ -1,11 +0,0 @@ -package io.iohk.atala.pollux.core.model.schema.`type`.anoncred - -import com.networknt.schema.JsonSchema -import io.iohk.atala.pollux.core.model.error.CredentialSchemaError -import zio.IO - -trait AnoncredSchemaSchemaVersion { - val version: String - - def initialiseJsonSchema: IO[CredentialSchemaError, JsonSchema] -} diff --git a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/anoncred/AnoncredSchemaSchemaV1.scala b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/anoncred/AnoncredSchemaSerDesV1.scala similarity index 60% rename from pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/anoncred/AnoncredSchemaSchemaV1.scala rename to pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/anoncred/AnoncredSchemaSerDesV1.scala index 791f2518f1..580a2dab54 100644 --- a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/anoncred/AnoncredSchemaSchemaV1.scala +++ b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/type/anoncred/AnoncredSchemaSerDesV1.scala @@ -1,13 +1,19 @@ package io.iohk.atala.pollux.core.model.schema.`type`.anoncred -import com.networknt.schema.* -import io.iohk.atala.pollux.core.model.error.CredentialSchemaError -import io.iohk.atala.pollux.core.model.schema.common.JsonSchemaUtils +import io.iohk.atala.pollux.core.model.schema.validator.SchemaSerDes import zio.* +import zio.json.* -object AnoncredSchemaSchemaV1 extends AnoncredSchemaSchemaVersion { - val version: String = AnoncredSchemaSchemaV1.getClass.getSimpleName - private val jsonSchemaSchemaStr: String = +case class AnoncredSchemaSerDesV1( + name: String, + version: String, + attrNames: Set[String], + issuerId: String +) + +object AnoncredSchemaSerDesV1 { + val version: String = "AnoncredSchemaV1" + private val schema: String = """ |{ | "$schema": "http://json-schema.org/draft-07/schema#", @@ -40,6 +46,9 @@ object AnoncredSchemaSchemaV1 extends AnoncredSchemaSchemaVersion { |} |""".stripMargin - override def initialiseJsonSchema: IO[CredentialSchemaError, JsonSchema] = - JsonSchemaUtils.jsonSchema(jsonSchemaSchemaStr) + val schemaSerDes: SchemaSerDes[AnoncredSchemaSerDesV1] = SchemaSerDes(schema) + + given JsonEncoder[AnoncredSchemaSerDesV1] = DeriveJsonEncoder.gen[AnoncredSchemaSerDesV1] + + given JsonDecoder[AnoncredSchemaSerDesV1] = DeriveJsonDecoder.gen[AnoncredSchemaSerDesV1] } diff --git a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/CredentialSchemaValidator.scala b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/CredentialSchemaValidator.scala deleted file mode 100644 index 280c38d7ee..0000000000 --- a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/CredentialSchemaValidator.scala +++ /dev/null @@ -1,13 +0,0 @@ -package io.iohk.atala.pollux.core.model.schema.validator - -import com.fasterxml.jackson.databind.JsonNode -import io.iohk.atala.pollux.core.model.error.CredentialSchemaError -import zio.* - -trait CredentialSchemaValidator { - def validate(claims: String): IO[CredentialSchemaError, Unit] - - def validate(claimsJsonNode: JsonNode): IO[CredentialSchemaError, Unit] = { - validate(claimsJsonNode.toString) - } -} diff --git a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/JsonSchemaError.scala b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/JsonSchemaError.scala new file mode 100644 index 0000000000..5fbff289a0 --- /dev/null +++ b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/JsonSchemaError.scala @@ -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 +} diff --git a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/common/JsonSchemaUtils.scala b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/JsonSchemaUtils.scala similarity index 77% rename from pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/common/JsonSchemaUtils.scala rename to pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/JsonSchemaUtils.scala index dbf3927727..3cf6fc46a6 100644 --- a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/common/JsonSchemaUtils.scala +++ b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/JsonSchemaUtils.scala @@ -1,10 +1,10 @@ -package io.iohk.atala.pollux.core.model.schema.common +package io.iohk.atala.pollux.core.model.schema.validator -import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper} +import com.fasterxml.jackson.databind.JsonNode +import com.fasterxml.jackson.databind.ObjectMapper import com.networknt.schema.* import com.networknt.schema.SpecVersion.VersionFlag -import io.iohk.atala.pollux.core.model.error.CredentialSchemaError -import io.iohk.atala.pollux.core.model.error.CredentialSchemaError.* +import io.iohk.atala.pollux.core.model.schema.validator.JsonSchemaError.* import zio.* import zio.json.ast.Json @@ -12,7 +12,7 @@ object JsonSchemaUtils { def jsonSchema( schema: String, supportedVersions: IndexedSeq[VersionFlag] = IndexedSeq.empty - ): IO[CredentialSchemaError, JsonSchema] = { + ): IO[JsonSchemaError, JsonSchema] = { for { jsonSchemaNode <- toJsonNode(schema) specVersion <- ZIO @@ -37,15 +37,15 @@ object JsonSchemaUtils { def from( schema: Json, supportedVersions: IndexedSeq[VersionFlag] = IndexedSeq.empty - ): IO[CredentialSchemaError, JsonSchema] = { + ): IO[JsonSchemaError, JsonSchema] = { jsonSchema(schema.toString(), supportedVersions) } - def toJsonNode(json: Json): IO[CredentialSchemaError, JsonNode] = { + def toJsonNode(json: Json): IO[JsonSchemaError, JsonNode] = { toJsonNode(json.toString()) } - def toJsonNode(json: String): IO[CredentialSchemaError, JsonNode] = { + def toJsonNode(json: String): IO[JsonSchemaError, JsonNode] = { for { mapper <- ZIO.attempt(new ObjectMapper()).mapError(t => UnexpectedError(t.getMessage)) jsonSchemaNode <- ZIO diff --git a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/JsonSchemaValidator.scala b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/JsonSchemaValidator.scala new file mode 100644 index 0000000000..39ac14cbe7 --- /dev/null +++ b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/JsonSchemaValidator.scala @@ -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) + } +} diff --git a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/CredentialJsonSchemaValidator.scala b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/JsonSchemaValidatorImpl.scala similarity index 53% rename from pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/CredentialJsonSchemaValidator.scala rename to pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/JsonSchemaValidatorImpl.scala index 0edd22f1db..ccc0e2d662 100644 --- a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/CredentialJsonSchemaValidator.scala +++ b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/JsonSchemaValidatorImpl.scala @@ -4,33 +4,32 @@ import com.networknt.schema.* import io.iohk.atala.pollux.core.model.error.CredentialSchemaError 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.common.JsonSchemaUtils import zio.* -case class CredentialJsonSchemaValidator(schemaValidator: JsonSchema) extends CredentialSchemaValidator { - override def validate(claims: String): IO[CredentialSchemaError, Unit] = { +case class JsonSchemaValidatorImpl(schemaValidator: JsonSchema) extends JsonSchemaValidator { + override def validate(jsonString: String): IO[JsonSchemaError, Unit] = { import scala.jdk.CollectionConverters.* for { // Convert claims to JsonNode - jsonClaims <- JsonSchemaUtils.toJsonNode(claims) + jsonClaims <- JsonSchemaUtils.toJsonNode(jsonString) // Validate claims JsonNode validationMessages <- ZIO .attempt(schemaValidator.validate(jsonClaims).asScala.toSeq) - .mapError(t => ClaimsValidationError(Seq(t.getMessage))) + .mapError(t => JsonSchemaError.JsonValidationErrors(Seq(t.getMessage))) validationResult <- if (validationMessages.isEmpty) ZIO.unit - else ZIO.fail(ClaimsValidationError(validationMessages.map(_.getMessage))) + else ZIO.fail(JsonSchemaError.JsonValidationErrors(validationMessages.map(_.getMessage))) } yield validationResult } } -object CredentialJsonSchemaValidator { - def from(schema: Schema): IO[CredentialSchemaError, CredentialJsonSchemaValidator] = { +object JsonSchemaValidatorImpl { + def from(schema: Schema): IO[JsonSchemaError, JsonSchemaValidatorImpl] = { for { jsonSchema <- JsonSchemaUtils.from(schema, IndexedSeq(SpecVersion.VersionFlag.V202012)) - } yield CredentialJsonSchemaValidator(jsonSchema) + } yield JsonSchemaValidatorImpl(jsonSchema) } } diff --git a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/SchemaSerDes.scala b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/SchemaSerDes.scala new file mode 100644 index 0000000000..2c9717c380 --- /dev/null +++ b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/model/schema/validator/SchemaSerDes.scala @@ -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 {} + } + +} diff --git a/pollux/lib/core/src/test/scala/io/iohk/atala/pollux/core/model/schema/AnoncredSchemaTypeSpec.scala b/pollux/lib/core/src/test/scala/io/iohk/atala/pollux/core/model/schema/AnoncredSchemaTypeSpec.scala index 372998a60c..ca6c2cf013 100644 --- a/pollux/lib/core/src/test/scala/io/iohk/atala/pollux/core/model/schema/AnoncredSchemaTypeSpec.scala +++ b/pollux/lib/core/src/test/scala/io/iohk/atala/pollux/core/model/schema/AnoncredSchemaTypeSpec.scala @@ -1,7 +1,7 @@ package io.iohk.atala.pollux.core.model.schema -import io.iohk.atala.pollux.core.model.error.CredentialSchemaError import io.iohk.atala.pollux.core.model.schema.`type`.AnoncredSchemaType +import io.iohk.atala.pollux.core.model.schema.validator.JsonSchemaError import zio.* import zio.json.* import zio.json.ast.Json @@ -154,7 +154,7 @@ object AnoncredSchemaTypeSpec extends ZIOSpecDefault { def failsWithErrors(errorMessages: Iterable[String]) = { fails( - isSubtype[CredentialSchemaError.ClaimsValidationError]( + isSubtype[JsonSchemaError.JsonValidationErrors]( hasField("errors", _.errors, hasSameElementsDistinct(errorMessages)) ) ) diff --git a/pollux/lib/core/src/test/scala/io/iohk/atala/pollux/core/model/schema/CredentialSchemaSpec.scala b/pollux/lib/core/src/test/scala/io/iohk/atala/pollux/core/model/schema/CredentialSchemaSpec.scala index fc19c77dbb..5e4c25c019 100644 --- a/pollux/lib/core/src/test/scala/io/iohk/atala/pollux/core/model/schema/CredentialSchemaSpec.scala +++ b/pollux/lib/core/src/test/scala/io/iohk/atala/pollux/core/model/schema/CredentialSchemaSpec.scala @@ -1,15 +1,22 @@ package io.iohk.atala.pollux.core.model.schema import io.iohk.atala.pollux.core.model.error.CredentialSchemaError +import io.iohk.atala.pollux.core.model.error.CredentialSchemaError.SchemaError import io.iohk.atala.pollux.core.model.schema.AnoncredSchemaTypeSpec.test -import io.iohk.atala.pollux.core.model.schema.`type`.anoncred.AnoncredSchemaSchemaV1 -import io.iohk.atala.pollux.core.model.schema.`type`.{AnoncredSchemaType, CredentialJsonSchemaType} +import io.iohk.atala.pollux.core.model.schema.`type`.anoncred.AnoncredSchemaSerDesV1 +import io.iohk.atala.pollux.core.model.schema.`type`.AnoncredSchemaType +import io.iohk.atala.pollux.core.model.schema.`type`.CredentialJsonSchemaType +import io.iohk.atala.pollux.core.model.schema.validator.JsonSchemaError.JsonValidationErrors import zio.Scope import zio.json.* import zio.json.ast.Json import zio.json.ast.Json.* import zio.test.Assertion.* -import zio.test.{Assertion, Spec, TestEnvironment, ZIOSpecDefault, assertZIO} +import zio.test.Assertion +import zio.test.Spec +import zio.test.TestEnvironment +import zio.test.ZIOSpecDefault +import zio.test.assertZIO import java.time.OffsetDateTime import java.util.UUID @@ -61,7 +68,7 @@ object CredentialSchemaSpec extends ZIOSpecDefault { authored = OffsetDateTime.parse("2022-03-10T12:00:00Z"), tags = Seq("tag1", "tag2"), description = "Anoncred Schema", - `type` = AnoncredSchemaSchemaV1.version, + `type` = AnoncredSchemaSerDesV1.version, schema = jsonSchema.fromJson[Json].getOrElse(Json.Null) ) assertZIO(CredentialSchema.validateCredentialSchema(credentialSchema))(isUnit) @@ -85,11 +92,11 @@ object CredentialSchemaSpec extends ZIOSpecDefault { authored = OffsetDateTime.parse("2022-03-10T12:00:00Z"), tags = Seq("tag1", "tag2"), description = "Anoncred Schema", - `type` = AnoncredSchemaSchemaV1.version, + `type` = AnoncredSchemaSerDesV1.version, schema = jsonSchema.fromJson[Json].getOrElse(Json.Null) ) assertZIO(CredentialSchema.validateCredentialSchema(credentialSchema).exit)( - AnoncredSchemaTypeSpec.failsWithErrors( + failsWithJsonValidationErrors( Seq("$.attrNames: the items in the array must be unique") ) ) @@ -128,4 +135,18 @@ object CredentialSchemaSpec extends ZIOSpecDefault { } ) ) + + def failsWithJsonValidationErrors(errorMessages: Iterable[String]) = { + fails( + isSubtype[SchemaError]( + hasField( + "schemaError", + _.schemaError, + isSubtype[JsonValidationErrors]( + hasField("errors", _.errors, hasSameElementsDistinct(errorMessages)) + ) + ) + ) + ) + } } diff --git a/prism-agent/service/server/src/test/scala/io/iohk/atala/pollux/CredentialSchemaAnoncredSpec.scala b/prism-agent/service/server/src/test/scala/io/iohk/atala/pollux/CredentialSchemaAnoncredSpec.scala index 98c4a239e4..0d77873cd0 100644 --- a/prism-agent/service/server/src/test/scala/io/iohk/atala/pollux/CredentialSchemaAnoncredSpec.scala +++ b/prism-agent/service/server/src/test/scala/io/iohk/atala/pollux/CredentialSchemaAnoncredSpec.scala @@ -2,7 +2,7 @@ package io.iohk.atala.pollux import io.iohk.atala.api.http.ErrorResponse import io.iohk.atala.container.util.MigrationAspects.* -import io.iohk.atala.pollux.core.model.schema.`type`.anoncred.AnoncredSchemaSchemaV1 +import io.iohk.atala.pollux.core.model.schema.`type`.anoncred.AnoncredSchemaSerDesV1 import io.iohk.atala.pollux.core.model.schema.`type`.{AnoncredSchemaType, CredentialJsonSchemaType} import io.iohk.atala.pollux.credentialschema.* import io.iohk.atala.pollux.credentialschema.controller.CredentialSchemaController @@ -70,7 +70,7 @@ object CredentialSchemaAnoncredSpec extends ZIOSpecDefault with CredentialSchema suite("Anoncred Schema Creation")( test("should create new Schema") { - val schemaInput = createCredentialSchemaInput(AnoncredSchemaSchemaV1.version) + val schemaInput = createCredentialSchemaInput(AnoncredSchemaSerDesV1.version) for { response <- createResponse[CredentialSchemaResponse](AnoncredSchemaType.`type`) statusCodeIs201 = assert(response.code)(equalTo(StatusCode.Created))