2025-02-13 05:46:56 +03:00

30 lines
1.2 KiB
C#

using NorthBridgeContracts.Extensions;
using NorthBridgeContracts.Infrastructure;
using System.Text.RegularExpressions;
using NorthBridgeContracts.Exceptions;
namespace NorthBridgeContracts.DataModels;
public class CustomerDataModel(string id, string fio, string phone, string email, double discountSize) : IValidation
{
public string Id { get; private set; } = id;
public string FIO { get; private set; } = fio;
public string Phone { get; private set; } = phone;
public string Email { get; private set; } = email;
public double DiscountSize { get; private set; } = discountSize;
public void Validate()
{
if (Id.IsEmpty()) throw new ValidationException("Field Id is empty");
if (!Id.IsGuid()) throw new ValidationException("Id must be a GUID");
if (FIO.IsEmpty()) throw new ValidationException("Field FIO is empty");
if (Phone.IsEmpty()) throw new ValidationException("Field Phone is empty");
var phoneRegex = new Regex(@"^\+7\d{10}$");
if (!phoneRegex.IsMatch(Phone)) throw new ValidationException("Invalid phone format");
if (Email.IsEmpty() || !Email.Contains("@")) throw new ValidationException("Invalid email format");
}
}