-
Notifications
You must be signed in to change notification settings - Fork 26
Refactor repeated GUID validation into a reusable FluentValidation extension #536
Copy link
Copy link
Closed
Copy link
Labels
C#C# related codeC# related codeenhancementNew feature or requestNew feature or requesthelp wantedExtra attention is neededExtra attention is neededrefactoringRefactoring codeRefactoring code
Description
Summary
Currently, multiple validator classes have identical BeAValidGuid methods to validate GUIDs. This results in repeated code and violates the DRY principle.
We should refactor this by creating a reusable FluentValidation extension method to encapsulate GUID validation logic and optionally support localized error messages.
Proposed Solution:
- Create a new static class
ValidatorExtensions, underExtensionsfolder in theFlowSynx.Applicationproject to hold extension methods for IRuleBuilder. - Add a method MustBeValidGuid that validates a string is a valid GUID.
- Update existing validators to use the new extension method instead of repeating BeAValidGuid.
- Ensure localization support can be injected for error messages.
public static class ValidatorExtensions
{
public static IRuleBuilderOptions<T, string> MustBeValidGuid<T>(
this IRuleBuilder<T, string> ruleBuilder, string message)
{
return ruleBuilder
.Must(id => Guid.TryParse(id, out _))
.WithMessage(message);
}
}Usage in validator:
For example in the WorkflowDetailsValidator.cs, it will change to this:
public class WorkflowDetailsValidator : AbstractValidator<WorkflowDetailsRequest>
{
public WorkflowDetailsValidator(ILocalization localization)
{
RuleFor(x => x.WorkflowId)
.NotNull()
.NotEmpty()
.WithMessage(localization.Get("Features_Validation_WorkflowId_MustHaveValue"));
RuleFor(x => x.WorkflowId)
.MustBeValidGuid(localization.Get("Features_Validation_WorkflowId_InvalidGuidFormat"));
}
}Tasks:
- Implement
MustBeValidGuidextension method - Refactor all existing validators to use this extension method
- Add unit tests for the extension method
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
C#C# related codeC# related codeenhancementNew feature or requestNew feature or requesthelp wantedExtra attention is neededExtra attention is neededrefactoringRefactoring codeRefactoring code