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

feat(validation): adjust schema validation #76

Merged
merged 1 commit into from
Oct 21, 2024
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
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