Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

complex form type validation #1282

Merged
merged 7 commits into from
Nov 29, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ internal class ComplexFormTypeValidator : AbstractValidator<ComplexFormType>
public ComplexFormTypeValidator()
{
RuleFor(c => c.DeletedAt).Null();
RuleFor(c => c.Name).ValidMultiString();
RuleFor(c => c.Name).Required();
}
}
9 changes: 7 additions & 2 deletions backend/FwLite/MiniLcm/Validators/MultiStringValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ namespace MiniLcm.Validators;

internal static class MultiStringValidator
{
public static IRuleBuilderOptions<T, MultiString> ValidMultiString<T>(this IRuleBuilder<T, MultiString> ruleBuilder)
public static IRuleBuilderOptions<T, MultiString> Required<T>(this IRuleBuilder<T, MultiString> ruleBuilder)
{
return ruleBuilder.NotEmpty();
return ruleBuilder.NotEmpty().NoEmptyValues();
}
public static IRuleBuilderOptions<T, MultiString> NoEmptyValues<T>(this IRuleBuilder<T, MultiString> ruleBuilder)
hahn-kev marked this conversation as resolved.
Show resolved Hide resolved
{
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");
}
}