Package | Version | Popularity |
---|---|---|
Gleeman.EffectiveValidator |
dotnet
CLI
> dotnet add package Gleeman.EffectiveValidator --version 2.0.0
[CheckCreditCard(CreditCardType creditCard)]
[CheckDate(string startDate, string endDate)]
[CheckEmail]
[CheckName]
[CheckPassport]
[CheckPhone]
[CheckRange(int min, int max)]
[CheckRequired]
[CheckStringEmpty]
[CheckStringLenght(int minLenght, int maxLenght)]
[CheckZipCode]
public record CreatePersonDto
{
[CheckName("is invalid!")]
[CheckStringLenght(3, 10, "should be between 3 and 10 characters!")]
public string FirstName { get; init; }
[CheckName("is invalid")]
[CheckStringEmpty("should not be empty!")]
public string LastName { get; init; }
[CheckDate("1950/1/1", "2005/1/1", "is invalid!")]
public DateTime DateOfBirth { get; init; }
[CheckRange(18, 73, "is invalid!")]
public int Age { get; init; }
[CheckEmail("is invalid!")]
public string Email { get; init; }
[CheckPhone("is invalid")]
public string PhoneNumber { get; init; }
[CheckPassport("is invalid!")]
public string PassportNumber { get; init; }
[CheckCreditCard(CreditCardType.Visa, "is invalid!")]
public string CreditCartNumber { get; init; }
[CheckZipCode("is invalid!")]
public string ZipCode { get; init; }
[CheckRequired("is required!")]
public bool? IsActive { get; init; }
}
[HttpPost]
public IActionResult PostPerson(CreatePersonDto createPerson)
{
Person person = new()
{
FirstName = createPerson.FirstName,
LastName = createPerson.LastName,
DateOfBirth = createPerson.DateOfBirth,
Age = createPerson.Age,
Email = createPerson.Email,
PhoneNumber = createPerson.PhoneNumber,
CreditCartNumber = createPerson.CreditCartNumber,
PassportNumber = createPerson.PassportNumber,
ZipCode = createPerson.ZipCode,
IsActive = createPerson.IsActive
};
var validator = new EffectiveValidator<CreatePersonDto>();
var validationResult = validator.Validate(createPerson);
if (validationResult.IsValid)
{
using var dbContext = new AppDbContext();
dbContext.Persons.Add(person);
dbContext.SaveChanges();
return Created("", createPerson);
}
return BadRequest(validationResult.ErrorMessages);
}