Skip to content

Instantly share code, notes, and snippets.

@bjornmicallef
Created September 4, 2020 14:00
Show Gist options
  • Save bjornmicallef/9fe86a1315aa4bf232127e42072e931e to your computer and use it in GitHub Desktop.
Save bjornmicallef/9fe86a1315aa4bf232127e42072e931e to your computer and use it in GitHub Desktop.
public class PurchasePlaystationGameDto
{
public int ID { get; set; }
public int UserID { get; set; }
public decimal Price { get; set; }
public bool PlaystationPlusMember { get; set; }
}
public class PurchasePlaystationGameDtoValidator : AbstractValidator
{
public PurchasePlaystationGameDtoValidator()
{
RuleFor(x => x.ID).NotNull().GreaterThan(0).WithMessage("ID must be greater than 0.");
RuleFor(x => x.UserID).NotNull().GreaterThan(0).WithMessage("UserID must be greater than 0.");
RuleFor(x => x.Price).Must(BeGreaterThanZeroForNonMembers).WithMessage("Price must be greater than 0");
}
private bool BeGreaterThanZeroForNonMembers(PurchasePlaystationGameDto dtoInstance, decimal price)
{
// game is free for members :)
if (!dtoInstance.PlaystationPlusMember & price <= 0)
{
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment