Skip to content

Commit

Permalink
Add validation extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Aymen TROUDI authored and Aymen TROUDI committed Aug 6, 2023
1 parent 157f7fe commit 37dd825
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 16 deletions.
5 changes: 5 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sdk": {
"allowPrerelease": false
}
}
23 changes: 23 additions & 0 deletions src/App/Extensions/FluentValidationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using FluentValidation;

namespace App.Extensions;

public static class FluentValidationExtensions
{
public static IRuleBuilderOptions<T, TProperty> Required<T, TProperty>(this IRuleBuilderInitial<T, TProperty> ruleBuilder)
{
DefaultValidatorOptions.Configurable(ruleBuilder).CascadeMode = CascadeMode.Stop;

return ruleBuilder
.NotNull()
.NotEmpty()
.WithMessage("{PropertyName} is required.");
}

public static IRuleBuilderOptions<T, string> FileExists<T>(this IRuleBuilder<T, string> ruleBuilder)
{
return ruleBuilder
.Must<T, string>((Func<T, string, bool>) ((x, val) => File.Exists(val)))
.WithMessage("File {PropertyName} does not exist.");
}
}
3 changes: 2 additions & 1 deletion src/App/Validators/JwtDecodeCommandValidator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using App.Commands;
using App.Extensions;
using FluentValidation;

namespace App.Validators;
Expand All @@ -8,6 +9,6 @@ public class JwtDecodeCommandValidator : AbstractValidator<JwtDecodeCommand>
public JwtDecodeCommandValidator()
{
RuleFor(x => x.Token)
.NotEmpty();
.Required();
}
}
12 changes: 5 additions & 7 deletions src/App/Validators/JwtGenerateCommandValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ public JwtGenerateCommandValidator()
When(x => string.IsNullOrWhiteSpace(x.ParametersFile), () =>
{
RuleFor(x => x.Certificate)
.Cascade(CascadeMode.Stop)
.NotEmpty()
.Must(File.Exists).WithMessage("File '{PropertyValue}' does not exist.");
.Required()
.FileExists();
RuleFor(x => x.Password)
.NotEmpty();
.Required();
})
.Otherwise(() =>
{
RuleFor(x => x.ParametersFile)
.Cascade(CascadeMode.Stop)
.NotEmpty()
.Must(File.Exists);
.Required()
.FileExists();
});
}
}
15 changes: 7 additions & 8 deletions src/App/Validators/JwtValidateCommandValidator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using App.Commands;
using App.Extensions;
using FluentValidation;

namespace App.Validators;
Expand All @@ -8,23 +9,21 @@ public class JwtValidateCommandValidator : AbstractValidator<JwtValidateCommand>
public JwtValidateCommandValidator()
{
RuleFor(x => x.Token)
.NotEmpty();
.Required();

When(x => string.IsNullOrWhiteSpace(x.ParametersFile), () =>
{
RuleFor(x => x.Certificate)
.Cascade(CascadeMode.Stop)
.NotEmpty()
.Must(File.Exists).WithMessage("File '{PropertyValue}' does not exist.");
.Required()
.FileExists();
RuleFor(x => x.Password)
.NotEmpty();
.Required();
})
.Otherwise(() =>
{
RuleFor(x => x.ParametersFile)
.Cascade(CascadeMode.Stop)
.NotEmpty()
.Must(File.Exists);
.Required()
.FileExists();
});
}
}

0 comments on commit 37dd825

Please sign in to comment.