(feature): patch model base, validatable object

This commit is contained in:
Maksym Sadovnychyy 2024-11-28 17:15:52 +01:00
parent 547c8ce1fb
commit 1e51993d13
2 changed files with 11 additions and 4 deletions

View File

@ -1,12 +1,19 @@
using System.Reflection; using System.ComponentModel.DataAnnotations;
using System.Reflection;
using MaksIT.Core.Webapi.Models; using MaksIT.Core.Webapi.Models;
namespace MaksIT.Core.Abstractions.Webapi; namespace MaksIT.Core.Abstractions.Webapi;
public abstract class PatchRequestModelBase : RequestModelBase { public abstract class PatchRequestModelBase : RequestModelBase, IValidatableObject {
public bool HasNonNullPatchField => GetType() private bool HasNonNullPatchField => GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance) .GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(prop => prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(PatchField<>)) .Where(prop => prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(PatchField<>))
.Any(prop => prop.GetValue(this) != null); .Any(prop => prop.GetValue(this) != null);
public virtual IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
if (!HasNonNullPatchField) {
yield return new ValidationResult("At least one patch field must be provided", new string[] { "PatchField" });
}
}
} }

View File

@ -8,7 +8,7 @@
<!-- NuGet package metadata --> <!-- NuGet package metadata -->
<PackageId>MaksIT.Core</PackageId> <PackageId>MaksIT.Core</PackageId>
<Version>1.2.0</Version> <Version>1.2.1</Version>
<Authors>Maksym Sadovnychyy</Authors> <Authors>Maksym Sadovnychyy</Authors>
<Company>MAKS-IT</Company> <Company>MAKS-IT</Company>
<Product>MaksIT.Core</Product> <Product>MaksIT.Core</Product>