init backend
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
using AipsCore.Domain.Abstract;
|
||||
using AipsCore.Domain.Abstract.Rule;
|
||||
|
||||
namespace AipsCore.Domain.Common.Validation.Rules;
|
||||
|
||||
public class CharsetRule : AbstractRule
|
||||
{
|
||||
private readonly string _stringValue;
|
||||
private readonly char[] _charset;
|
||||
|
||||
protected CharsetRule(string stringValue, char[] charset)
|
||||
{
|
||||
_stringValue = stringValue;
|
||||
_charset = charset;
|
||||
}
|
||||
|
||||
protected override string ErrorCode => "charset";
|
||||
protected override string ErrorMessage => $"Forbidden characters in '{ValueObjectName}'";
|
||||
|
||||
public override bool Validate()
|
||||
{
|
||||
foreach (char character in _stringValue)
|
||||
{
|
||||
if (!_charset.Contains(character))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
32
dotnet/AipsCore/Domain/Common/Validation/Rules/EmailRule.cs
Normal file
32
dotnet/AipsCore/Domain/Common/Validation/Rules/EmailRule.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System.Net.Mail;
|
||||
using AipsCore.Domain.Abstract;
|
||||
using AipsCore.Domain.Abstract.Rule;
|
||||
|
||||
namespace AipsCore.Domain.Common.Validation.Rules;
|
||||
|
||||
public class EmailRule : AbstractRule
|
||||
{
|
||||
protected override string ErrorCode => "email_invalid";
|
||||
protected override string ErrorMessage => "Email is not in the valid format";
|
||||
|
||||
private readonly string _emailValue;
|
||||
|
||||
public EmailRule(string emailValue)
|
||||
{
|
||||
_emailValue = emailValue;
|
||||
}
|
||||
|
||||
public override bool Validate()
|
||||
{
|
||||
try
|
||||
{
|
||||
_ = new MailAddress(_emailValue);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using AipsCore.Domain.Abstract;
|
||||
using AipsCore.Domain.Abstract.Rule;
|
||||
|
||||
namespace AipsCore.Domain.Common.Validation.Rules;
|
||||
|
||||
public class ExactLength : AbstractRule
|
||||
{
|
||||
private readonly string _stringValue;
|
||||
private readonly int _exactLentgh;
|
||||
protected override string ErrorCode => "exact_length";
|
||||
protected override string ErrorMessage
|
||||
=> $"Length of '{ValueObjectName}' must be {_exactLentgh} characters";
|
||||
|
||||
public ExactLength(string stringValue, int exactLentgh)
|
||||
{
|
||||
_stringValue = stringValue;
|
||||
_exactLentgh = exactLentgh;
|
||||
}
|
||||
|
||||
public override bool Validate()
|
||||
{
|
||||
return _stringValue.Length == _exactLentgh;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using AipsCore.Domain.Abstract;
|
||||
using AipsCore.Domain.Abstract.Rule;
|
||||
|
||||
namespace AipsCore.Domain.Common.Validation.Rules;
|
||||
|
||||
public class MaxLengthRule : AbstractRule
|
||||
{
|
||||
private readonly string _stringValue;
|
||||
private readonly int _maximumLentgh;
|
||||
protected override string ErrorCode => "minimum_length";
|
||||
protected override string ErrorMessage
|
||||
=> $"Length of '{ValueObjectName}' must be at most {_maximumLentgh} characters";
|
||||
|
||||
public MaxLengthRule(string stringValue, int maximumLentgh)
|
||||
{
|
||||
_stringValue = stringValue;
|
||||
_maximumLentgh = maximumLentgh;
|
||||
}
|
||||
|
||||
public override bool Validate()
|
||||
{
|
||||
return _stringValue.Length <= _maximumLentgh;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using AipsCore.Domain.Abstract;
|
||||
using AipsCore.Domain.Abstract.Rule;
|
||||
|
||||
namespace AipsCore.Domain.Common.Validation.Rules;
|
||||
|
||||
public class MinLengthRule : AbstractRule
|
||||
{
|
||||
private readonly string _stringValue;
|
||||
private readonly int _minimumLentgh;
|
||||
protected override string ErrorCode => "minimum_length";
|
||||
protected override string ErrorMessage
|
||||
=> $"Length of '{ValueObjectName}' must be at least {_minimumLentgh} characters";
|
||||
|
||||
public MinLengthRule(string stringValue, int minimumLentgh)
|
||||
{
|
||||
_stringValue = stringValue;
|
||||
_minimumLentgh = minimumLentgh;
|
||||
}
|
||||
|
||||
public override bool Validate()
|
||||
{
|
||||
return _stringValue.Length >= _minimumLentgh;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace AipsCore.Domain.Common.Validation;
|
||||
|
||||
public record ValidationError(string Code, string Message);
|
||||
@@ -0,0 +1,13 @@
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace AipsCore.Domain.Common.Validation;
|
||||
|
||||
public class ValidationException : Exception
|
||||
{
|
||||
public ICollection<ValidationError> ValidationErrors { get; private init; }
|
||||
|
||||
public ValidationException(ICollection<ValidationError> validationErrors)
|
||||
{
|
||||
ValidationErrors = validationErrors;
|
||||
}
|
||||
}
|
||||
55
dotnet/AipsCore/Domain/Common/Validation/Validator.cs
Normal file
55
dotnet/AipsCore/Domain/Common/Validation/Validator.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using AipsCore.Domain.Abstract;
|
||||
using AipsCore.Domain.Abstract.Rule;
|
||||
|
||||
namespace AipsCore.Domain.Common.Validation;
|
||||
|
||||
public class Validator
|
||||
{
|
||||
private readonly ICollection<IRule> _validationRules;
|
||||
private readonly string _valueObjectName;
|
||||
|
||||
public Validator(ICollection<IRule> validationRules, string valueObjectName)
|
||||
{
|
||||
_validationRules = validationRules;
|
||||
_valueObjectName = valueObjectName;
|
||||
}
|
||||
|
||||
public bool Success { get; private set; } = false;
|
||||
|
||||
private ValidationException? _validationException = null;
|
||||
|
||||
public ValidationException GetValidationException()
|
||||
{
|
||||
if (_validationException is null)
|
||||
{
|
||||
return new ValidationException([]);
|
||||
}
|
||||
return _validationException;
|
||||
}
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
List<ValidationError> errors = [];
|
||||
|
||||
foreach (var validationRule in _validationRules)
|
||||
{
|
||||
validationRule.ValueObjectName = _valueObjectName;
|
||||
|
||||
if (!validationRule.Validate())
|
||||
{
|
||||
errors.Add(validationRule.GetError());
|
||||
}
|
||||
}
|
||||
|
||||
if (errors.Any())
|
||||
{
|
||||
Success = false;
|
||||
_validationException = new ValidationException(errors.ToArray());
|
||||
}
|
||||
else
|
||||
{
|
||||
Success = true;
|
||||
_validationException = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
22
dotnet/AipsCore/Domain/Common/ValueObjects/DomainId.cs
Normal file
22
dotnet/AipsCore/Domain/Common/ValueObjects/DomainId.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using AipsCore.Domain.Abstract;
|
||||
using AipsCore.Domain.Abstract.Rule;
|
||||
using AipsCore.Domain.Abstract.ValueObject;
|
||||
using AipsCore.Domain.Common.Validation;
|
||||
|
||||
namespace AipsCore.Domain.Common.ValueObjects;
|
||||
|
||||
public record DomainId : AbstractValueObject
|
||||
{
|
||||
public string IdValue { get; init; }
|
||||
|
||||
public DomainId(string IdValue)
|
||||
{
|
||||
this.IdValue = IdValue;
|
||||
Validate();
|
||||
}
|
||||
|
||||
protected override ICollection<IRule> GetValidationRules()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
26
dotnet/AipsCore/Domain/Common/ValueObjects/Email.cs
Normal file
26
dotnet/AipsCore/Domain/Common/ValueObjects/Email.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using AipsCore.Domain.Abstract;
|
||||
using AipsCore.Domain.Abstract.Rule;
|
||||
using AipsCore.Domain.Abstract.ValueObject;
|
||||
using AipsCore.Domain.Common.Validation;
|
||||
using AipsCore.Domain.Common.Validation.Rules;
|
||||
|
||||
namespace AipsCore.Domain.Common.ValueObjects;
|
||||
|
||||
public record Email : AbstractValueObject
|
||||
{
|
||||
public string EmailValue { get; init; }
|
||||
|
||||
public Email(string EmailValue)
|
||||
{
|
||||
this.EmailValue = EmailValue;
|
||||
Validate();
|
||||
}
|
||||
|
||||
protected override ICollection<IRule> GetValidationRules()
|
||||
{
|
||||
return
|
||||
[
|
||||
new EmailRule(EmailValue)
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user