public async Task <EntityValidationResult <Delivery> > SendJob(Delivery data) { try { var result = new EntityValidationResult <Delivery>(); var validationResult = _validator.Validate(data); result.IsValid = validationResult.IsValid; result.Object = data; if (validationResult.IsValid) { data.Id = Guid.NewGuid(); _deliveryRepository.Create(data); await _deliveryRepository.SaveAsync(); } else { result.Errors = validationResult.Errors.Select(x => new EntityValidationFailure { PropertyName = x.PropertyName, ErrorMessage = x.ErrorMessage }).ToList(); } return(result); } catch (Exception ex) { throw ex; } }
public (bool isValid, IEnumerable <ValidationResult> errors) Validate() { var validator = new DeliveryValidator(); var result = validator.Validate(this); if (result.IsValid) { return(true, null); } return(false, result.Errors.Select(item => new ValidationResult(item.ErrorMessage, new [] { item.PropertyName }))); }