//-----------------------------------------------------------------------
//
// Copyright (c) aliasvault. All rights reserved.
// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
//
//-----------------------------------------------------------------------
namespace AliasVault.Shared.Models.WebApi;
using System.Text.Json.Serialization;
///
/// Represents the structure of a validation error response from the API.
///
public class ServerValidationErrorResponse
{
///
/// Gets or sets the type of the error.
///
[JsonPropertyName("type")]
public string Type { get; set; } = null!;
///
/// Gets or sets the title of the error.
///
[JsonPropertyName("title")]
public string Title { get; set; } = null!;
///
/// Gets or sets the HTTP status code of the response.
///
[JsonPropertyName("status")]
public int Status { get; set; }
///
/// Gets or sets the validation errors. The key is the name of the field that has the error, and the value is an array of error messages for that field.
///
[JsonPropertyName("errors")]
public Dictionary Errors { get; set; } = new();
///
/// Gets or sets the trace ID of the error.
///
[JsonPropertyName("traceId")]
public string TraceId { get; set; } = null!;
///
/// Creates a new instance of .
///
/// Title of the error.
/// Status code.
/// ServerValidationErrorResponse object.
public static ServerValidationErrorResponse Create(string title, int status)
{
var errors = new Dictionary
{
{ title, [title] },
};
return new ServerValidationErrorResponse
{
Type = "https://tools.ietf.org/html/rfc7231#section-6.5.1",
Title = title,
Errors = errors,
Status = status,
TraceId = Guid.NewGuid().ToString(),
};
}
///
/// Creates a new instance of .
///
/// Array with errors.
/// Status code.
/// ServerValidationErrorResponse object.
public static ServerValidationErrorResponse Create(string[] errorArray, int status)
{
var errors = new Dictionary();
foreach (var t in errorArray)
{
errors.Add(t, new[] { t });
}
return new ServerValidationErrorResponse
{
Type = "https://tools.ietf.org/html/rfc7231#section-6.5.1",
Title = errorArray[0],
Errors = errors,
Status = status,
TraceId = Guid.NewGuid().ToString(),
};
}
}