-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
complex form type validation (#1282)
* create ComplexFormTypeValidator and test it * wire up complex form type validation, fix some failing tests due to bad input * add basic entry validator as an example of validating dependent properties like EntryId on sense * set a custom message for Sense.EntryId validation * add a validation method to MiniLcmValidators * defined MultiString validators for required and no empty values
- Loading branch information
Showing
14 changed files
with
189 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
backend/FwLite/MiniLcm.Tests/Validators/ComplexFormTypeValidationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using FluentValidation.TestHelper; | ||
using MiniLcm.Validators; | ||
|
||
namespace MiniLcm.Tests.Validators; | ||
|
||
public class ComplexFormTypeValidationTests | ||
{ | ||
private readonly ComplexFormTypeValidator _validator = new(); | ||
|
||
[Fact] | ||
public void FailsForEmptyName() | ||
{ | ||
var complexFormType = new ComplexFormType() { Name = new MultiString() }; | ||
_validator.TestValidate(complexFormType).ShouldHaveValidationErrorFor(c => c.Name); | ||
} | ||
[Fact] | ||
public void FailsForNameWithEmptyStringValue() | ||
{ | ||
var complexFormType = new ComplexFormType() { Name = new(){ { "en", string.Empty } } }; | ||
_validator.TestValidate(complexFormType).ShouldHaveValidationErrorFor(c => c.Name); | ||
} | ||
|
||
[Fact] | ||
public void FailsForNonNullDeletedAt() | ||
{ | ||
var complexFormType = new ComplexFormType() | ||
{ | ||
Name = new() { { "en", "test" } }, DeletedAt = DateTimeOffset.UtcNow | ||
}; | ||
_validator.TestValidate(complexFormType).ShouldHaveValidationErrorFor(c => c.DeletedAt); | ||
} | ||
|
||
[Fact] | ||
public void Succeeds() | ||
{ | ||
var complexFormType = new ComplexFormType() | ||
{ | ||
Name = new() { { "en", "test" } }, | ||
DeletedAt = null | ||
}; | ||
_validator.TestValidate(complexFormType).ShouldNotHaveAnyValidationErrors(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
backend/FwLite/MiniLcm.Tests/Validators/EntryValidatorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using FluentValidation.TestHelper; | ||
using MiniLcm.Validators; | ||
|
||
namespace MiniLcm.Tests.Validators; | ||
|
||
public class EntryValidatorTests | ||
{ | ||
private readonly EntryValidator _validator = new(); | ||
|
||
[Fact] | ||
public void Succeeds_WhenSenseEntryIdIsGuidEmpty() | ||
{ | ||
var entryId = Guid.NewGuid(); | ||
var entry = new Entry() { Id = entryId, Senses = [new Sense() { EntryId = Guid.Empty, }] }; | ||
_validator.TestValidate(entry).ShouldNotHaveAnyValidationErrors(); | ||
} | ||
|
||
[Fact] | ||
public void Succeeds_WhenSenseEntryIdMatchesEntry() | ||
{ | ||
|
||
var entryId = Guid.NewGuid(); | ||
var entry = new Entry() { Id = entryId, Senses = [new Sense() { EntryId = entryId, }] }; | ||
_validator.TestValidate(entry).ShouldNotHaveAnyValidationErrors(); | ||
} | ||
|
||
[Fact] | ||
public void Fails_WhenSenseEntryIdDoesNotMatchEntry() | ||
{ | ||
var entryId = Guid.NewGuid(); | ||
var entry = new Entry() { Id = entryId, Senses = [new Sense() { EntryId = Guid.NewGuid(), }] }; | ||
_validator.TestValidate(entry).ShouldHaveValidationErrorFor("Senses[0].EntryId"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
backend/FwLite/MiniLcm/Validators/ComplexFormTypeValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using FluentValidation; | ||
using FluentValidation.Validators; | ||
using MiniLcm.Models; | ||
|
||
namespace MiniLcm.Validators; | ||
|
||
internal class ComplexFormTypeValidator : AbstractValidator<ComplexFormType> | ||
{ | ||
public ComplexFormTypeValidator() | ||
{ | ||
RuleFor(c => c.DeletedAt).Null(); | ||
RuleFor(c => c.Name).Required(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using FluentValidation; | ||
using MiniLcm.Models; | ||
|
||
namespace MiniLcm.Validators; | ||
|
||
public class EntryValidator : AbstractValidator<Entry> | ||
{ | ||
public EntryValidator() | ||
{ | ||
RuleForEach(e => e.Senses).SetValidator(entry => new SenseValidator(entry)); | ||
//todo just a stub as an example for senses | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using FluentValidation; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using MiniLcm.Models; | ||
|
||
namespace MiniLcm.Validators; | ||
|
||
public record MiniLcmValidators(IValidator<ComplexFormType> ComplexFormTypeValidator) | ||
{ | ||
public async Task ValidateAndThrow(ComplexFormType value) | ||
{ | ||
await ComplexFormTypeValidator.ValidateAndThrowAsync(value); | ||
} | ||
} | ||
|
||
public static class MiniLcmValidatorsExtensions | ||
{ | ||
public static IServiceCollection AddMiniLcmValidators(this IServiceCollection services) | ||
{ | ||
services.AddTransient<MiniLcmValidators>(); | ||
services.AddTransient<IValidator<ComplexFormType>, ComplexFormTypeValidator>(); | ||
return services; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using FluentValidation; | ||
using MiniLcm.Models; | ||
|
||
namespace MiniLcm.Validators; | ||
|
||
internal static class MultiStringValidator | ||
{ | ||
public static IRuleBuilderOptions<T, MultiString> Required<T>(this IRuleBuilder<T, MultiString> ruleBuilder) | ||
{ | ||
return ruleBuilder.NotEmpty().NoEmptyValues(); | ||
} | ||
public static IRuleBuilderOptions<T, MultiString> NoEmptyValues<T>(this IRuleBuilder<T, MultiString> ruleBuilder) | ||
{ | ||
return ruleBuilder.Must(ms => ms.Values.All(v => !string.IsNullOrEmpty(v.Value))).WithMessage((parent, ms) => | ||
$"MultiString must not contain empty values, but [{string.Join(", ", ms.Values.Where(v => string.IsNullOrWhiteSpace(v.Value)).Select(v => v.Key))}] was empty"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using FluentValidation; | ||
using MiniLcm.Models; | ||
|
||
namespace MiniLcm.Validators; | ||
|
||
public class SenseValidator : AbstractValidator<Sense> | ||
{ | ||
public SenseValidator() | ||
{ | ||
//todo add validation for the other properties | ||
} | ||
|
||
public SenseValidator(Entry entry): this() | ||
{ | ||
//it's ok if senses EntryId is an Empty guid | ||
RuleFor(s => s.EntryId).Equal(entry.Id).When(s => s.EntryId != Guid.Empty).WithMessage(sense => $"Sense (Id: {sense.Id}) EntryId must match Entry {entry.Id}, but instead was {sense.EntryId}"); | ||
} | ||
} |