-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jernejr
committed
Feb 24, 2021
1 parent
258091a
commit 9d6efcd
Showing
5 changed files
with
147 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using FluentValidation.Resources; | ||
using FluentValidation.Results; | ||
using FluentValidation.Validators; | ||
|
||
namespace CoreSharp.Validation | ||
{ | ||
|
||
public abstract class AbstractCommonPropertyValidator : PropertyValidator | ||
{ | ||
protected AbstractCommonPropertyValidator(IStringSource errorMessageSource) : base(errorMessageSource) | ||
{ | ||
} | ||
|
||
protected abstract string ErrorTemplate { get; } | ||
|
||
protected override ValidationFailure CreateValidationError(PropertyValidatorContext context) | ||
{ | ||
var failure = base.CreateValidationError(context); | ||
failure.ErrorMessage = context.MessageFormatter.BuildMessage(ErrorTemplate); | ||
return failure; | ||
} | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
CoreSharp.Validation/CoreSharpFluentValidationExtensions.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,32 @@ | ||
using System.Collections.Generic; | ||
using CoreSharp.Common.Models; | ||
using CoreSharp.Validation; | ||
|
||
namespace FluentValidation | ||
{ | ||
public static class CoreSharpFluentValidationExtensions | ||
{ | ||
public static IRuleBuilderOptions<T, string> PhoneNumber<T>(this IRuleBuilder<T, string> ruleBuilder) | ||
{ | ||
return ruleBuilder.SetValidator(new PhoneNumberValidator()); | ||
} | ||
|
||
public static IRuleBuilderOptions<T, string> Guid<T>(this IRuleBuilder<T, string> ruleBuilder, string format = "D") | ||
{ | ||
return ruleBuilder.SetValidator(new GuidValidator(format)); | ||
} | ||
|
||
public static void PagedListFilter<T>(this AbstractValidator<T> validator, PagingConfiguration configuration) | ||
where T : PagedListFilter | ||
{ | ||
validator.RuleFor(x => x.Skip).GreaterThanOrEqualTo(0).LessThanOrEqualTo(configuration.MaxSkip); | ||
validator.RuleFor(x => x.Take).GreaterThan(0).LessThanOrEqualTo(configuration.MaxTake); | ||
} | ||
|
||
public static IRuleBuilderOptions<T, string> OneOf<T, TProp>(this IRuleBuilder<T, string> ruleBuilder, IEnumerable<TProp> validOptions, string errorMessage = "Value is not valid.") | ||
{ | ||
return ruleBuilder.SetValidator(new OneOfValidator<TProp>(validOptions, errorMessage)); | ||
} | ||
|
||
} | ||
} |
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,24 @@ | ||
using System; | ||
using FluentValidation.Resources; | ||
using FluentValidation.Validators; | ||
|
||
namespace CoreSharp.Validation | ||
{ | ||
public class GuidValidator : AbstractCommonPropertyValidator | ||
{ | ||
private readonly string _format; | ||
|
||
public GuidValidator(string format = "D") : base(new LanguageStringSource(nameof(GuidValidator))) | ||
{ | ||
_format = format; | ||
} | ||
|
||
protected override string ErrorTemplate => "Value is not a valid GUID."; | ||
|
||
protected override bool IsValid(PropertyValidatorContext context) | ||
{ | ||
bool valid = Guid.TryParseExact(context.PropertyValue as string, _format, out Guid guid); | ||
return valid; | ||
} | ||
} | ||
} |
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,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using FluentValidation.Resources; | ||
using FluentValidation.Validators; | ||
|
||
namespace CoreSharp.Validation | ||
{ | ||
|
||
public class OneOfValidator<T> : AbstractCommonPropertyValidator | ||
{ | ||
private readonly IList<T> _validOptions; | ||
private readonly string _errorMessage; | ||
|
||
public OneOfValidator(IEnumerable<T> validoptions, string errorMessage = "Value is not valid.") : base(new LanguageStringSource(nameof(OneOfValidator<T>))) | ||
{ | ||
_validOptions = validoptions.ToList(); | ||
_errorMessage = errorMessage; | ||
} | ||
|
||
protected override string ErrorTemplate => _errorMessage; | ||
|
||
protected override bool IsValid(PropertyValidatorContext context) | ||
{ | ||
try | ||
{ | ||
var val = (T)context.PropertyValue; | ||
var valid = _validOptions.Contains(val); | ||
return valid; | ||
} catch(Exception) | ||
{ | ||
return false; | ||
} | ||
|
||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
CoreSharp.Validation/PropertyValidators/PhoneNumberValidator.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,30 @@ | ||
using System.Text.RegularExpressions; | ||
using FluentValidation.Resources; | ||
using FluentValidation.Validators; | ||
|
||
namespace CoreSharp.Validation | ||
{ | ||
|
||
public class PhoneNumberValidator : AbstractCommonPropertyValidator | ||
{ | ||
|
||
private static Regex _regex = new Regex(@"^[0-9]{2,20}$"); | ||
|
||
protected override string ErrorTemplate => "Value is not a valid phone number."; | ||
|
||
public PhoneNumberValidator() : base(new LanguageStringSource(nameof(PhoneNumberValidator))) | ||
{ | ||
} | ||
|
||
protected override bool IsValid(PropertyValidatorContext context) | ||
{ | ||
if (context.PropertyValue == null) | ||
{ | ||
return true; | ||
} | ||
|
||
var phone = context.PropertyValue?.ToString(); | ||
return _regex.IsMatch(phone); | ||
} | ||
} | ||
} |