Skip to content

Commit

Permalink
Properties validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jernejr committed Feb 24, 2021
1 parent 258091a commit 9d6efcd
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
24 changes: 24 additions & 0 deletions CoreSharp.Validation/AbstractCommonPropertyValidator.cs
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 CoreSharp.Validation/CoreSharpFluentValidationExtensions.cs
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));
}

}
}
24 changes: 24 additions & 0 deletions CoreSharp.Validation/PropertyValidators/GuidValidator.cs
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;
}
}
}
37 changes: 37 additions & 0 deletions CoreSharp.Validation/PropertyValidators/OneOfValidator.cs
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 CoreSharp.Validation/PropertyValidators/PhoneNumberValidator.cs
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);
}
}
}

0 comments on commit 9d6efcd

Please sign in to comment.