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(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 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); } }