66 lines
1.8 KiB
C#
66 lines
1.8 KiB
C#
namespace LaaProduction
|
|
{
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
internal static class Extensions
|
|
{
|
|
internal static string ComputeBase64Hash(this string value)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(value))
|
|
{
|
|
var bytes = Encoding.UTF8.GetBytes(value);
|
|
var hash = SHA256.Create().ComputeHash(bytes);
|
|
var base64 = Convert.ToBase64String(hash);
|
|
|
|
return base64;
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
internal static bool TryValidate(this object model, out string errors)
|
|
{
|
|
errors = default(string);
|
|
|
|
var validationResults = new List<ValidationResult>();
|
|
var validationContext = new ValidationContext(model);
|
|
|
|
if (!Validator.TryValidateObject(model, validationContext, validationResults, true))
|
|
{
|
|
var errorBuilder = new StringBuilder();
|
|
|
|
foreach (var error in validationResults)
|
|
{
|
|
var errorMessage = error.ErrorMessage;
|
|
|
|
if (string.IsNullOrWhiteSpace(errorMessage))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var property = "Error";
|
|
|
|
foreach (var member in error.MemberNames)
|
|
{
|
|
property = member;
|
|
|
|
break;
|
|
}
|
|
|
|
errorBuilder.AppendLine($"[{property}] -> {errorMessage}");
|
|
}
|
|
|
|
errors = errorBuilder.ToString();
|
|
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
}
|