Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
- Implemented comprehensive unit tests for RabbitMqTransportServer, covering constructor, disposal, connection management, event handlers, and exception handling. - Added configuration tests for RabbitMqTransportServer to validate SSL, durable queues, auto-recovery, and custom virtual host options. - Created unit tests for UdpFrameProtocol, including frame parsing and serialization, header size validation, and round-trip data preservation. - Developed tests for UdpTransportClient, focusing on connection handling, event subscriptions, and exception scenarios. - Established tests for UdpTransportServer, ensuring proper start/stop behavior, connection state management, and event handling. - Included tests for UdpTransportOptions to verify default values and modification capabilities. - Enhanced service registration tests for Udp transport services in the dependency injection container.
102 lines
3.3 KiB
C#
102 lines
3.3 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace StellaOps.Microservice.Validation;
|
|
|
|
/// <summary>
|
|
/// RFC 7807 Problem Details for schema validation failures.
|
|
/// Returns HTTP 422 Unprocessable Entity.
|
|
/// </summary>
|
|
public sealed class ValidationProblemDetails
|
|
{
|
|
private static readonly JsonSerializerOptions SerializerOptions = new()
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
|
};
|
|
|
|
/// <summary>
|
|
/// A URI reference identifying the problem type.
|
|
/// </summary>
|
|
[JsonPropertyName("type")]
|
|
public string Type { get; init; } = "https://stellaops.io/errors/schema-validation";
|
|
|
|
/// <summary>
|
|
/// A short, human-readable summary of the problem.
|
|
/// </summary>
|
|
[JsonPropertyName("title")]
|
|
public string Title { get; init; } = "Schema Validation Failed";
|
|
|
|
/// <summary>
|
|
/// The HTTP status code (422).
|
|
/// </summary>
|
|
[JsonPropertyName("status")]
|
|
public int Status { get; init; } = 422;
|
|
|
|
/// <summary>
|
|
/// A human-readable explanation specific to this occurrence.
|
|
/// </summary>
|
|
[JsonPropertyName("detail")]
|
|
public string? Detail { get; init; }
|
|
|
|
/// <summary>
|
|
/// A URI reference identifying the specific occurrence (the endpoint path).
|
|
/// </summary>
|
|
[JsonPropertyName("instance")]
|
|
public string? Instance { get; init; }
|
|
|
|
/// <summary>
|
|
/// The trace/correlation ID for distributed tracing.
|
|
/// </summary>
|
|
[JsonPropertyName("traceId")]
|
|
public string? TraceId { get; init; }
|
|
|
|
/// <summary>
|
|
/// The list of validation errors.
|
|
/// </summary>
|
|
[JsonPropertyName("errors")]
|
|
public IReadOnlyList<SchemaValidationError> Errors { get; init; } = [];
|
|
|
|
/// <summary>
|
|
/// Creates a ValidationProblemDetails for schema validation failures.
|
|
/// </summary>
|
|
/// <param name="method">The HTTP method.</param>
|
|
/// <param name="path">The endpoint path.</param>
|
|
/// <param name="direction">Request or response validation.</param>
|
|
/// <param name="errors">The validation errors.</param>
|
|
/// <param name="correlationId">Optional correlation ID.</param>
|
|
public static ValidationProblemDetails Create(
|
|
string method,
|
|
string path,
|
|
SchemaDirection direction,
|
|
IReadOnlyList<SchemaValidationError> errors,
|
|
string? correlationId = null)
|
|
{
|
|
var directionText = direction == SchemaDirection.Request ? "request" : "response";
|
|
return new ValidationProblemDetails
|
|
{
|
|
Detail = $"{char.ToUpperInvariant(directionText[0])}{directionText[1..]} body failed schema validation for {method} {path}",
|
|
Instance = path,
|
|
TraceId = correlationId,
|
|
Errors = errors
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts this problem details to a RawResponse.
|
|
/// </summary>
|
|
public RawResponse ToRawResponse()
|
|
{
|
|
var json = JsonSerializer.SerializeToUtf8Bytes(this, SerializerOptions);
|
|
var headers = new HeaderCollection();
|
|
headers.Set("Content-Type", "application/problem+json; charset=utf-8");
|
|
|
|
return new RawResponse
|
|
{
|
|
StatusCode = Status,
|
|
Headers = headers,
|
|
Body = new MemoryStream(json)
|
|
};
|
|
}
|
|
}
|