-
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.
Merge pull request #64 from PinguApps/add-request-validation
Added validators, and validation of request objects within SDK
- Loading branch information
Showing
20 changed files
with
482 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using FluentValidation; | ||
using FluentValidation.Results; | ||
|
||
namespace PinguApps.Appwrite.Shared.Requests; | ||
public abstract class BaseRequest<TRequest, TValidator> | ||
where TRequest : class | ||
where TValidator : IValidator<TRequest>, new() | ||
{ | ||
public bool IsValid() => Validate().IsValid; | ||
|
||
public ValidationResult Validate(bool throwOnFailures = false) | ||
{ | ||
var validator = new TValidator(); | ||
|
||
if (throwOnFailures) | ||
return validator.Validate(this as TRequest, options => options.ThrowOnFailures()); | ||
|
||
return validator.Validate((this as TRequest)!); | ||
} | ||
} |
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
3 changes: 2 additions & 1 deletion
3
src/PinguApps.Appwrite.Shared/Requests/UpdatePasswordRequest.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
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
13 changes: 13 additions & 0 deletions
13
src/PinguApps.Appwrite.Shared/Requests/Validators/CreateAccountRequestValidator.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,13 @@ | ||
using FluentValidation; | ||
|
||
namespace PinguApps.Appwrite.Shared.Requests.Validators; | ||
public class CreateAccountRequestValidator : AbstractValidator<CreateAccountRequest> | ||
{ | ||
public CreateAccountRequestValidator() | ||
{ | ||
RuleFor(x => x.UserId).NotEmpty().Matches("^[a-zA-Z0-9][a-zA-Z0-9._-]{0,35}$"); | ||
RuleFor(x => x.Email).NotEmpty().EmailAddress(); | ||
RuleFor(x => x.Password).NotEmpty().MinimumLength(8).MaximumLength(256); | ||
RuleFor(x => x.Name).MaximumLength(128); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/PinguApps.Appwrite.Shared/Requests/Validators/UpdateEmailRequestValidator.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,11 @@ | ||
using FluentValidation; | ||
|
||
namespace PinguApps.Appwrite.Shared.Requests.Validators; | ||
public class UpdateEmailRequestValidator : AbstractValidator<UpdateEmailRequest> | ||
{ | ||
public UpdateEmailRequestValidator() | ||
{ | ||
RuleFor(x => x.Email).NotEmpty().EmailAddress(); | ||
RuleFor(x => x.Password).NotEmpty().MinimumLength(8); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/PinguApps.Appwrite.Shared/Requests/Validators/UpdateNameRequestValidator.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,10 @@ | ||
using FluentValidation; | ||
|
||
namespace PinguApps.Appwrite.Shared.Requests.Validators; | ||
public class UpdateNameRequestValidator : AbstractValidator<UpdateNameRequest> | ||
{ | ||
public UpdateNameRequestValidator() | ||
{ | ||
RuleFor(x => x.Name).NotEmpty().MaximumLength(128); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/PinguApps.Appwrite.Shared/Requests/Validators/UpdatePasswordRequestValidator.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,11 @@ | ||
using FluentValidation; | ||
|
||
namespace PinguApps.Appwrite.Shared.Requests.Validators; | ||
public class UpdatePasswordRequestValidator : AbstractValidator<UpdatePasswordRequest> | ||
{ | ||
public UpdatePasswordRequestValidator() | ||
{ | ||
RuleFor(x => x.NewPassword).NotEmpty().MinimumLength(8); | ||
RuleFor(x => x.OldPassword).MinimumLength(8).When(x => x.OldPassword is not null); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/PinguApps.Appwrite.Shared/Requests/Validators/UpdatePhoneRequestValidator.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,11 @@ | ||
using FluentValidation; | ||
|
||
namespace PinguApps.Appwrite.Shared.Requests.Validators; | ||
public class UpdatePhoneRequestValidator : AbstractValidator<UpdatePhoneRequest> | ||
{ | ||
public UpdatePhoneRequestValidator() | ||
{ | ||
RuleFor(x => x.Phone).NotEmpty().Matches("^\\+\\d*$"); | ||
RuleFor(x => x.Password).NotEmpty().MinimumLength(8); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using PinguApps.Appwrite.Shared.Requests; | ||
using FluentValidation; | ||
using PinguApps.Appwrite.Shared.Requests; | ||
|
||
namespace PinguApps.Appwrite.Shared.Tests.Requests; | ||
public class CreateAccountRequestTests | ||
|
@@ -35,4 +36,96 @@ public void Properties_CanBeSet(string email, string password, string? name) | |
Assert.Equal(password, request.Password); | ||
Assert.Equal(name, request.Name); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null, "[email protected]", "Password", null)] | ||
[InlineData("321654987", "[email protected]", "12345678", "Pingu")] | ||
[InlineData("a.s-d_f", "[email protected]", "12345678", "Pingu")] | ||
public void IsValid_WithValidData_ReturnsTrue(string? userId, string email, string password, string? name) | ||
{ | ||
// Arrange | ||
var request = new CreateAccountRequest | ||
{ | ||
Email = email, | ||
Password = password, | ||
Name = name | ||
}; | ||
|
||
if (userId is not null) | ||
{ | ||
request.UserId = userId; | ||
} | ||
|
||
// Act | ||
var isValid = request.IsValid(); | ||
|
||
// Assert | ||
Assert.True(isValid); | ||
} | ||
|
||
[Theory] | ||
[InlineData("badChar^", "[email protected]", "Password", "Pingu")] | ||
[InlineData(".bad", "[email protected]", "Password", "Pingu")] | ||
[InlineData("_bad", "[email protected]", "Password", "Pingu")] | ||
[InlineData("-bad", "[email protected]", "Password", "Pingu")] | ||
[InlineData("", "[email protected]", "Password", "Pingu")] | ||
[InlineData("1234567890123456789012345678901234567", "[email protected]", "Password", "Pingu")] | ||
[InlineData(null, "not an email", "Password", "Pingu")] | ||
[InlineData(null, "", "Password", "Pingu")] | ||
[InlineData(null, "[email protected]", "short", "Pingu")] | ||
[InlineData(null, "[email protected]", "", "Pingu")] | ||
public void IsValid_WithInvalidData_ReturnsFalse(string? userId, string email, string password, string? name) | ||
{ | ||
// Arrange | ||
var request = new CreateAccountRequest | ||
{ | ||
Email = email, | ||
Password = password, | ||
Name = name | ||
}; | ||
|
||
if (userId is not null) | ||
{ | ||
request.UserId = userId; | ||
} | ||
|
||
// Act | ||
var isValid = request.IsValid(); | ||
|
||
// Assert | ||
Assert.False(isValid); | ||
} | ||
|
||
[Fact] | ||
public void Validate_WithThrowOnFailuresTrue_ThrowsValidationExceptionOnFailure() | ||
{ | ||
// Arrange | ||
var request = new CreateAccountRequest | ||
{ | ||
UserId = ".badChar^", | ||
Email = "not an email", | ||
Password = "short" | ||
}; | ||
|
||
// Assert | ||
Assert.Throws<ValidationException>(() => request.Validate(true)); | ||
} | ||
|
||
[Fact] | ||
public void Validate_WithThrowOnFailuresFalse_ReturnsInvalidResultOnFailure() | ||
{ | ||
// Arrange | ||
var request = new CreateAccountRequest | ||
{ | ||
UserId = ".badChar^", | ||
Email = "not an email", | ||
Password = "short" | ||
}; | ||
|
||
// Act | ||
var result = request.Validate(false); | ||
|
||
// Assert | ||
Assert.False(result.IsValid); | ||
} | ||
} |
Oops, something went wrong.