Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. //Object itself.
  2. RuleFor(x => x).NotNull();
  3.  
  4. [Test]
  5. public void RequestObjectIsNull_ExpectError()
  6. {
  7. BusinessRequest request = null;
  8. var result = validator.Validate(request);
  9.  
  10. Assert.IsFalse(result.IsValid);
  11. }
  12.  
  13. public class MyValidator : AbstractValidator<MyObject>
  14. {
  15. public MyValidator()
  16. {
  17. RuleSet("MyRule", () =>
  18. {
  19. RuleFor(x=>x.MyProperty=="Something").WithMessage("failed");
  20. });
  21. }
  22.  
  23. public override Task<ValidationResult> ValidateAsync(ValidationContext<MyObjec>> context, CancellationToken cancellation = default)
  24. {
  25. return context.InstanceToValidate == null ? Task.FromResult(new ValidationResult(new[] { new ValidationFailure("MyObject", "filed with null") })) : base.ValidateAsync(context, cancellation);
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement