public PreviewViewModelValidator(IQnaApiClient qnaApiClient) { RuleFor(vm => vm).Custom((vm, context) => { if (vm.SequenceNo == null) { context.AddFailure(nameof(vm.SequenceNo), SequenceNoFieldEmptyError); } if (string.IsNullOrEmpty(vm.ApplicationData)) { context.AddFailure(nameof(vm.ApplicationData), ApplicaitonDataEmptyError); } else { if (!IsValidJson(vm.ApplicationData)) { context.AddFailure(nameof(vm.ApplicationData), InvalidJson); } } if (string.IsNullOrEmpty(vm.ProjectType)) { context.AddFailure(nameof(vm.ProjectType), ProjectTypeFieldEmptyError); } else { bool projectTypeExists = false; var projects = qnaApiClient.GetProjects().Result; foreach (var _ in from project in projects let workflow = qnaApiClient.GetWorkflows(project.Id).Result where workflow != null && workflow.Exists(x => x.Type.Equals(vm.ProjectType, System.StringComparison.OrdinalIgnoreCase)) select new { }) { projectTypeExists = true; break; } if (!projectTypeExists) { context.AddFailure(nameof(vm.ProjectType), ProjectTypeFieldDoesNotExistError); } } }); }