Skip to content

Instantly share code, notes, and snippets.

@ProximaB
Created July 10, 2019 07:06
Show Gist options
  • Save ProximaB/67718d8c88d3d8aeee2543d127f54a51 to your computer and use it in GitHub Desktop.
Save ProximaB/67718d8c88d3d8aeee2543d127f54a51 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PinP.Service
{
public delegate bool Validation();
public class ValidatorDtos
{
private List<ValidationRuleDtos> _validationRules;
public void AddRule(ValidationRuleDtos rule) => _validationRules.Add(rule);
public string ValidationResultByFieledName(string filedName) => _validationRules.FirstOrDefault(r => r.FieldName.Equals(filedName))?.ValidateResult() ?? string.Empty;
public string ValidateResults() => string.Join(", ", _validationRules.Select(r => r.ValidateResult()).Where(r => !string.IsNullOrEmpty(r)));
}
public class ValidationRuleDtos
{
public string FieldName { get; set; }
public string ErrorDescription { get; set; }
public event Validation CustomValidation;
public ValidationRuleDtos(string fieldName, string errorDescription)
{
FieldName = fieldName;
ErrorDescription = errorDescription;
}
public ValidationRuleDtos(string fieldName, Func<bool> customValidation, string errorDescription)
{
FieldName = fieldName;
CustomValidation += () => customValidation();
ErrorDescription = errorDescription;
}
public bool Check()
{
if (CustomValidation != null)
{
return CustomValidation();
}
return true;
}
public string ValidateResult() => Check() ? string.Empty : ErrorDescription;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment