private bool ValidatePet(Pet pet) { var petValidator = new PetValidator(); pet.ValidationResult = petValidator.Validate(pet); return(pet.ValidationResult.Errors.Any()); }
public Pet Insert(Pet pet) { var petValidator = new PetValidator(); pet.ValidationResult = petValidator.Validate(pet); if (pet.ValidationResult.Errors.Any()) { return(pet); } return(_petShopRepository.Insert(pet)); }
public void TestPetValidation() { IValidator <Pet> validator = new PetValidator(); Pet validPet = new Pet() { ID = 1, Name = "kiki", Age = 12, Gender = GenderEnum.FEMININ, Species = "koala" }; Pet petInvalid = new Pet() { ID = 2, Name = "", Age = -6, Gender = GenderEnum.UNKNOWN, Species = "" }; //validating a valid pet validator.Validate(validPet); //trying to validate a pet with invalid name, age and species try { validator.Validate(petInvalid); Assert.Fail(); } catch (ValidationException ve) { Assert.AreEqual(ve.Message, "Invalid name!\nInvalid age!\nInvalid species!\n"); } }