Skip to content

Commit

Permalink
fix: remove middleware and generic rulebuilder with rulefor and rulef…
Browse files Browse the repository at this point in the history
…oreach in respective validators
  • Loading branch information
Jan Lesage authored and QuintenGreenstack committed Jan 11, 2024
1 parent cd8c545 commit d25ef42
Show file tree
Hide file tree
Showing 42 changed files with 182 additions and 327 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ namespace AssociationRegistry.Admin.Api.Infrastructure.Extensions;

using ExceptionHandlers;
using FluentValidation;
using HtmlValidation;
using JasperFx.Core.Reflection;
using Microsoft.AspNetCore.Http;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -20,80 +16,15 @@ public static async Task NullValidateAndThrowAsync<T>(
CancellationToken cancellationToken = default)
{
if (instance is null) throw new CouldNotParseRequestException();

await new NoHtmlValidator<T>().ValidateAndThrowAsync(instance, cancellationToken);
await validator.ValidateAndThrowAsync(instance, cancellationToken);
}
}

public class NoHtmlValidator<T> : AbstractValidator<T>
{
private const string TO_BE_STRIPPED_PREFIX = "to-be-stripped-request-prefix";

public NoHtmlValidator()
{
// TODO: nested
// TODO: es kijken naar 'I <3 <Programming>' als waarde?
RecursivelyApplyRule(typeof(T), TO_BE_STRIPPED_PREFIX);
}

private void RecursivelyApplyRule(Type type, string propertyName)
{
var props = type.GetProperties();

foreach (var prop in props)
{
var currentPropertyName = $"{propertyName}.{prop.Name}";

if (prop.HasAttribute<NoHtmlAttribute>())
{
ApplyRuleFor(prop, currentPropertyName);
}
else if (prop.PropertyType.IsArray)
{
if (prop.PropertyType.IsClass)
RecursivelyApplyRule(prop.PropertyType, currentPropertyName);
else
ApplyRuleForEach(prop, currentPropertyName);
}
else if (prop.PropertyType.IsClass)
{
RecursivelyApplyRule(prop.PropertyType, currentPropertyName);
}
}
}

private void ApplyRuleFor(PropertyInfo prop, string propertyName)
{
if (prop.PropertyType == typeof(string))
RuleFor(model => GetPropertyValue(model, prop.Name) as string)
.Cascade(CascadeMode.Continue)
.Must(BeNoHtml!)
.WithName(propertyName.Replace($"{TO_BE_STRIPPED_PREFIX}.", newValue: ""))
.WithErrorCode(StatusCodes.Status400BadRequest.ToString())
.WithMessage(ExceptionMessages.UnsupportedContent)
.When(model => GetPropertyValue(model, prop.Name) != null);
}

private void ApplyRuleForEach(PropertyInfo prop, string propertyName)
{
if (prop.PropertyType == typeof(string[]))
RuleForEach(model => Convert.ChangeType(GetPropertyValue(model, prop.Name), prop.PropertyType) as string[])
.Cascade(CascadeMode.Continue)
.Must(BeNoHtml!)
.WithName(propertyName.Replace($"{TO_BE_STRIPPED_PREFIX}.", newValue: ""))
.WithErrorCode(StatusCodes.Status400BadRequest.ToString())
.WithMessage(ExceptionMessages.UnsupportedContent)
.When(model => GetPropertyValue(model, prop.Name) != null);
}

private static object? GetPropertyValue(T model, string propertyName)
{
var property = typeof(T).GetProperty(propertyName);

return property?.GetValue(model);
}
public static IRuleBuilder<T, string?> MustNotContainHtml<T>(this IRuleBuilder<T, string?> ruleBuilder)
=> ruleBuilder
.Must(NotContainHtml)
.WithErrorCode(StatusCodes.Status400BadRequest.ToString())
.WithMessage(ExceptionMessages.UnsupportedContent);

private static bool BeNoHtml(string propertyValue)
=> !Regex.IsMatch(propertyValue, pattern: "<.*?>");
private static bool NotContainHtml(string? propertyValue)
=> propertyValue is null ? true : !Regex.IsMatch(propertyValue, pattern: "<.*?>");
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace AssociationRegistry.Admin.Api.Verenigingen.Common;

using Infrastructure.HtmlValidation;
using System.Runtime.Serialization;

/// <summary>Een adres van een locatie</summary>
Expand All @@ -9,31 +8,25 @@ public class Adres
{
/// <summary>De straat van het adres</summary>
[DataMember]
[NoHtml]
public string Straatnaam { get; set; } = null!;

/// <summary>Het huisnummer van het adres</summary>
[DataMember]
[NoHtml]
public string Huisnummer { get; set; } = null!;

/// <summary>Het busnummer van het adres</summary>
[DataMember]
[NoHtml]
public string? Busnummer { get; set; }

/// <summary>De postcode van het adres</summary>
[DataMember]
[NoHtml]
public string Postcode { get; set; } = null!;

/// <summary>De gemeente van het adres</summary>
[DataMember]
[NoHtml]
public string Gemeente { get; set; } = null!;

/// <summary>Het land van het adres</summary>
[DataMember]
[NoHtml]
public string Land { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace AssociationRegistry.Admin.Api.Verenigingen.Common;

using Infrastructure.HtmlValidation;
using System.Runtime.Serialization;

/// <summary>De unieke identificator van het adres in een andere bron</summary>
Expand All @@ -9,11 +8,9 @@ public class AdresId
{
/// <summary>De code van de bron van het adres</summary>
[DataMember]
[NoHtml]
public string Broncode { get; set; } = null!;

/// <summary>De unieke identificator volgens de bron</summary>
[DataMember]
[NoHtml]
public string Bronwaarde { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace AssociationRegistry.Admin.Api.Verenigingen.Common;

using FluentValidation;
using Infrastructure.Extensions;
using Infrastructure.Validation;

public class AdresIdValidator : AbstractValidator<AdresId>
Expand All @@ -9,5 +10,8 @@ public AdresIdValidator()
{
this.RequireNotNullOrEmpty(adresId => adresId.Broncode);
this.RequireNotNullOrEmpty(adresId => adresId.Bronwaarde);

RuleFor(m => m.Broncode).MustNotContainHtml();
RuleFor(m => m.Bronwaarde).MustNotContainHtml();
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
namespace AssociationRegistry.Admin.Api.Verenigingen.Common;

using FluentValidation;
using Infrastructure.Extensions;
using Infrastructure.Validation;

public class AdresValidator : AbstractValidator<Adres>
{
public AdresValidator()
{
this.RequireNotNullOrEmpty(adres => adres.Straatnaam);
this.RequireNotNullOrEmpty(locatie => locatie.Huisnummer);
this.RequireNotNullOrEmpty(locatie => locatie.Gemeente);
this.RequireNotNullOrEmpty(locatie => locatie.Land);
this.RequireNotNullOrEmpty(locatie => locatie.Postcode);
this.RequireNotNullOrEmpty(adres => adres.Huisnummer);
this.RequireNotNullOrEmpty(adres => adres.Gemeente);
this.RequireNotNullOrEmpty(adres => adres.Land);
this.RequireNotNullOrEmpty(adres => adres.Postcode);

RuleFor(adres => adres.Straatnaam).MustNotContainHtml();
RuleFor(adres => adres.Huisnummer).MustNotContainHtml();
RuleFor(adres => adres.Busnummer).MustNotContainHtml();
RuleFor(adres => adres.Postcode).MustNotContainHtml();
RuleFor(adres => adres.Gemeente).MustNotContainHtml();
RuleFor(adres => adres.Land).MustNotContainHtml();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace AssociationRegistry.Admin.Api.Verenigingen.Common;

using Infrastructure.HtmlValidation;
using Infrastructure.Swagger;
using System.Runtime.Serialization;
using Vereniging;
Expand All @@ -17,19 +16,16 @@ public class ToeTeVoegenContactgegeven
[SwaggerParameterExample("Telefoon")]
[SwaggerParameterExample("Website")]
[DataMember(Name = "contactgegeventype")]
[NoHtml]
public string Contactgegeventype { get; set; } = null!;

/// <summary>De waarde van het contactgegeven</summary>
[DataMember(Name = "waarde")]
[NoHtml]
public string Waarde { get; set; } = null!;

/// <summary>
/// Vrij veld die het het contactgegeven beschrijft (bijv: algemeen, administratie, ...)
/// </summary>
[DataMember(Name = "beschrijving")]
[NoHtml]
public string? Beschrijving { get; set; }

/// <summary>Duidt het contactgegeven aan als primair contactgegeven</summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace AssociationRegistry.Admin.Api.Verenigingen.Common;

using FluentValidation;
using Infrastructure.Extensions;
using Infrastructure.Validation;
using Vereniging;

Expand All @@ -11,6 +12,10 @@ public ToeTeVoegenContactgegevenValidator()
this.RequireNotNullOrEmpty(contactgegeven => contactgegeven.Waarde);
this.RequireNotNullOrEmpty(contactgegeven => contactgegeven.Contactgegeventype);

RuleFor(contactgegeven => contactgegeven.Beschrijving).MustNotContainHtml();
RuleFor(contactgegeven => contactgegeven.Contactgegeventype).MustNotContainHtml();
RuleFor(contactgegeven => contactgegeven.Waarde).MustNotContainHtml();

RuleFor(contactgegeven => contactgegeven.Contactgegeventype)
.Must(Contactgegeventype.CanParse)
.WithMessage(contactgegeven => $"De waarde {contactgegeven.Contactgegeventype} is geen gekend contactgegeven type.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace AssociationRegistry.Admin.Api.Verenigingen.Common;

using Infrastructure.HtmlValidation;
using System.Runtime.Serialization;
using Vereniging;

Expand All @@ -16,7 +15,6 @@ public class ToeTeVoegenLocatie
/// - Correspondentie - Slechts één maal mogelijk<br />
/// </summary>
[DataMember]
[NoHtml]
public string Locatietype { get; set; } = null!;

/// <summary>Duidt aan dat dit de primaire locatie is</summary>
Expand All @@ -25,7 +23,6 @@ public class ToeTeVoegenLocatie

/// <summary>Een beschrijvende naam voor de locatie</summary>
[DataMember]
[NoHtml]
public string? Naam { get; set; }

/// <summary>De unieke identificator van het adres in een andere bron</summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
namespace AssociationRegistry.Admin.Api.Verenigingen.Common;

using FluentValidation;
using Infrastructure.Extensions;
using Infrastructure.Validation;
using System;
using System.Linq;
using Infrastructure.Validation;
using FluentValidation;
using Vereniging;

public class ToeTeVoegenLocatieValidator : AbstractValidator<ToeTeVoegenLocatie>
Expand All @@ -12,6 +13,9 @@ public ToeTeVoegenLocatieValidator()
{
this.RequireNotNullOrEmpty(locatie => locatie.Locatietype);

RuleFor(locatie => locatie.Naam).MustNotContainHtml();
RuleFor(locatie => locatie.Locatietype).MustNotContainHtml();

RuleFor(locatie => locatie.Locatietype)
.Must(BeAValidLocationTypeValue)
.WithMessage($"'Locatietype' moet een geldige waarde hebben. ({Locatietype.Correspondentie}, {Locatietype.Activiteiten})")
Expand Down Expand Up @@ -49,6 +53,6 @@ private static object ToAnonymousObject(ToeTeVoegenLocatie l)
=> new
{
l.Locatietype, l.Naam, l.Adres?.Straatnaam, l.Adres?.Huisnummer, l.Adres?.Busnummer, l.Adres?.Postcode, l.Adres?.Gemeente,
l.Adres?.Land, l.AdresId?.Bronwaarde, l.AdresId?.Broncode
l.Adres?.Land, l.AdresId?.Bronwaarde, l.AdresId?.Broncode,
};
}
Loading

0 comments on commit d25ef42

Please sign in to comment.