Skip to content

Commit

Permalink
feat(validation): adjust schema validation (#76)
Browse files Browse the repository at this point in the history
schemaType is made optional
------------------
Refs: #34
Reviewed-By: Evelyn Gurschler <[email protected]>
  • Loading branch information
Phil91 authored Oct 21, 2024
1 parent ad0e791 commit 4f9a315
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 178 deletions.
1 change: 0 additions & 1 deletion docs/api/asr-service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ paths:
parameters:
- name: schemaType
in: query
required: true
schema:
$ref: '#/components/schemas/CredentialSchemaType'
requestBody:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ namespace Org.Eclipse.TractusX.SsiAuthoritySchemaRegistry.Service.BusinessLogic;

public interface ISchemaBusinessLogic : ITransient
{
Task<bool> Validate(CredentialSchemaType schemaType, JsonDocument content, CancellationToken cancellationToken);
Task<bool> Validate(CredentialSchemaType? schemaType, JsonDocument content);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,46 @@
using Json.Schema;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
using Org.Eclipse.TractusX.SsiAuthoritySchemaRegistry.Service.Models;
using System.Collections.Concurrent;
using System.Reflection;
using System.Text.Json;

namespace Org.Eclipse.TractusX.SsiAuthoritySchemaRegistry.Service.BusinessLogic;

public class SchemaBusinessLogic : ISchemaBusinessLogic
{
public async Task<bool> Validate(CredentialSchemaType schemaType, JsonDocument content, CancellationToken cancellationToken)
public async Task<bool> Validate(CredentialSchemaType? schemaType, JsonDocument content)
{
var location = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (location == null)
{
throw new UnexpectedConditionException("Assembly location must be set");
}

if (schemaType is not null)
{
return await ValidateSchema(schemaType.Value, content, location).ConfigureAwait(ConfigureAwaitOptions.None);
}

var options = new ParallelOptions
{
MaxDegreeOfParallelism = 2
};

var typeResult = false;
await Parallel.ForEachAsync(Enum.GetValues<CredentialSchemaType>(), options, async (type, _) =>
{
if (await ValidateSchema(type, content, location).ConfigureAwait(ConfigureAwaitOptions.None))
{
typeResult = true;
}
})
.ConfigureAwait(ConfigureAwaitOptions.None);
return typeResult;
}

private static async Task<bool> ValidateSchema(CredentialSchemaType schemaType, JsonDocument content, string location)
{
var path = Path.Combine(location, "Schemas", $"{schemaType}.schema.json");
var schema = await JsonSchema.FromStream(File.OpenRead(path)).ConfigureAwait(false);
SchemaRegistry.Global.Register(schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public static RouteGroupBuilder MapSchemaApi(this RouteGroupBuilder group)
{
var schema = group.MapGroup("/schema");

schema.MapPost("validate", ([FromQuery] CredentialSchemaType schemaType, [FromBody] JsonDocument content, CancellationToken cancellationToken, ISchemaBusinessLogic logic) => logic.Validate(schemaType, content, cancellationToken))
schema.MapPost("validate", ([FromBody] JsonDocument content, [FromServices] ISchemaBusinessLogic logic, [FromQuery] CredentialSchemaType? schemaType = null) => logic.Validate(schemaType, content))
.WithSwaggerDescription("Gets all credentials with optional filter possibilities",
"Example: POST: api/schema/validate",
"The type of the schema that should be validated",
"Optional: The type of the schema that should be validated",
"The schema as json")
.WithDefaultResponses()
.Produces(StatusCodes.Status200OK, typeof(bool), Constants.JsonContentType);
Expand Down
Loading

0 comments on commit 4f9a315

Please sign in to comment.