public override void Validate(Application application) { // if app is null, cant carry out any other validation if (application == null) { throw new ValidationException("Object", "Application cannot be null"); } var errors = new ValidationErrorList(); /// check that name is provided and it doesnt alreadt exist if (string.IsNullOrEmpty(application.Name)) { errors.Add("Name", "Name is required"); } else { var existingName = GetByName(application.Name); if (existingName != null && (existingName.Id != application.Id)) { errors.Add("Name", "Name already exists"); } } if (string.IsNullOrEmpty(application.Platform)) { errors.Add("Platform", "Platform is required"); } if (errors.Count > 0) { throw new ValidationException(errors); } }
public override void Validate(Token token) { // if token is null, cant carry out any other validation if (token == null) throw new ValidationException("Object", "Token cannot be null"); var errors = new ValidationErrorList(); // ... }
public override void Validate(User user) { // if user is null, cant carry out any other validation if (user == null) { throw new ValidationException("Object", "User cannot be null"); } var errors = new ValidationErrorList(); /// check that email is provided, in correct format, and it doesnt alreadt exist if (string.IsNullOrEmpty(user.EmailAddress)) { errors.Add("EmailAddress", "Email Address is required"); } else if (Regex.Match(user.EmailAddress, @"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$").Success == false) { errors.Add("EmailAddress", "Email Address format is invalid"); } else { var existingEmail = GetByEmail(user.EmailAddress); if (existingEmail != null && (existingEmail.Id != user.Id)) { errors.Add("EmailAddress", "Email Address already exists"); } else if (user.Id > 0) { // if updating a user, prevent user changing admin email address var previousValues = GetById(user.Id); if (previousValues.EmailAddress.ToLower() == "*****@*****.**" && user.EmailAddress.ToLower() != previousValues.EmailAddress.ToLower()) { errors.Add("EmailAddress", "You cannot change the email address of this user"); } } } if (string.IsNullOrEmpty(user.Password)) { if (user.Id == 0) { errors.Add("Password", "Password is required"); } } else if (user.Password.Length < 4) { errors.Add("Password", "Password must be at least 4 characters"); } if (errors.Count > 0) { throw new ValidationException(errors); } }
/// <summary> /// Get all error codes and messages from Validationerrors /// </summary> /// <param name="validationErrors"></param> /// <returns></returns> public static List <ResultMessage> GetAllValidationErrorCodesAndMessages(ValidationErrorList validationErrors) { List <ResultMessage> resultMessages = new List <ResultMessage>(); foreach (ValidationError validationError in validationErrors.Errors.ToList()) { resultMessages.Add(new ResultMessage { Key = validationError.ErrorMessageResourceKey, Message = validationError.ErrorDescription }); } return(resultMessages); }
public async Task <ValidationErrorList> ValidateSubscription(Subscription subscription) { _logger.ForContext <NewsletterService>().Information("ValidateSubscription(subscription: {@Subscription})", subscription); ValidationErrorList errorList = new ValidationErrorList(); if (subscription == null) { errorList.AddError("form", "nullSubscription"); return(errorList); } await ValidateEmail(subscription, errorList); await ValidateReferrer(subscription, errorList); return(errorList); }
public async Task <ApiResponse <bool> > Subscribe(Subscription subscription) { _logger.ForContext <NewsletterService>().Information("Subscribe(subscription: {@Subscription})"); var errorList = new ValidationErrorList(); try { subscription.StartDate = DateTime.UtcNow; _db.Subscriptions.Add(subscription); await _db.SaveChangesAsync(); } catch (Exception ex) { _logger.Error(ex, "Unhandled Exception"); errorList.AddError("form", "unknownError"); return(new ApiResponse <bool>(false, errorList.Errors)); } return(new ApiResponse <bool>(true, null)); }
public async Task <ActionResult> Subscribe([FromBody] Subscription subscription) { _logger.ForContext <NewsletterController>().Information("Subscribe(subscription: {@Subscription})", subscription);; try { ValidationErrorList validationErrors = await _newsletterService.ValidateSubscription(subscription); if (!validationErrors.isValid) { return(new JsonResult(new ApiResponse <bool>(false, validationErrors.Errors))); } return(new JsonResult(await _newsletterService.Subscribe(subscription))); } catch (Exception ex) { _logger.ForContext <NewsletterController>().Error($"Something went wrong: {ex}"); return(StatusCode(500, "Internal server error")); } }
public override void Validate(Group group) { // if group is null, cant carry out any other validation if (group == null) { throw new ValidationException("Object", "Group cannot be null"); } var errors = new ValidationErrorList(); /// check that name is provided and it doesnt alreadt exist if (string.IsNullOrEmpty(group.Name)) { errors.Add("Name", "Name is required"); } else { var existingName = GetByName(group.Name); if (existingName != null && (existingName.Id != group.Id)) { errors.Add("Name", "Name already exists"); } else if (group.Id > 0) { // if updating a group, prevent name changes to the administrators group var previousValues = GetById(group.Id); if (previousValues.Name.ToLower() == "administrators" && group.Name.ToLower() != previousValues.Name.ToLower()) { errors.Add("Name", "You cannot change the name of this group"); } } } if (errors.Count > 0) { throw new ValidationException(errors); } }
public ImportDigest() { ErrorList = new ValidationErrorList(); Sheet = new List <ExcelCell>(); }
private void ShowErrors(string[] validationErrors) { ValidationErrorList.DataSource = validationErrors; ValidationErrorList.DataBind(); }
private void ShowErrors() { ValidationErrorList.DataSource = _validationErrors; ValidationErrorList.DataBind(); }
private async Task <ValidationErrorList> ValidateReferrer(Subscription subscription, ValidationErrorList errorList) { _logger.ForContext <NewsletterService>().Debug("ValidateName(subscription: {@Subscription})", subscription); if (string.IsNullOrWhiteSpace(subscription.Referrer)) { errorList.AddError(nameof(subscription.Referrer), "required"); } return(errorList); }
private async Task <ValidationErrorList> ValidateEmail(Subscription subscription, ValidationErrorList errorList) { _logger.ForContext <NewsletterService>().Debug("ValidateEmail(subscription: {@Subscription})", subscription); if (string.IsNullOrWhiteSpace(subscription.Email)) { errorList.AddError(nameof(subscription.Email), "required"); } if (!string.IsNullOrWhiteSpace(subscription.Email) && (!new EmailAddressAttribute().IsValid(subscription.Email) || !Regex.Match(subscription.Email, @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$", RegexOptions.IgnoreCase).Success)) { errorList.AddError(nameof(subscription.Email), "email"); } if (await _db.Subscriptions.AnyAsync(s => s.Email == subscription.Email)) { errorList.AddError(nameof(subscription.Email), "existingEmail"); } return(errorList); }
public ValidationException(ValidationErrorList errors) : base("Validation Error(s). Inspect error list for details") { Errors = errors; }