public void ValidationOfNullLogsNoError() { Validator validator = new ObjectCollectionValidator(typeof(ObjectCollectionValidatorFixtureReferencedTestClass)); ValidationResults validationResults = validator.Validate(null); Assert.IsTrue(validationResults.IsValid); }
static void ValidatingACollectionOfObjects() { // Create a collection of two objects, one with valid values and one with invalid values. List <IProduct> productList = new List <IProduct>(); Product validProduct = new Product(); PopulateValidProduct(validProduct); productList.Add(validProduct); Product invalidProduct = new Product(); PopulateInvalidProduct(invalidProduct); productList.Add(invalidProduct); Console.WriteLine("Created and populated a collection of the Product class."); // Create an Object Collection Validator for the collection type. Validator collValidator = new ObjectCollectionValidator(typeof(Product)); // Validate all of the objects in the collection. ValidationResults results = collValidator.Validate(productList); // Display the results. ShowValidationResults(results); }
public Response <Model.Customer> Create(Model.Customer customer) { ICustomerRepository customerRepository = null; IAddressRepository addressRepository = null; Validator <Model.Customer> validator = ValidationFactory.CreateValidator <Model.Customer>("Create"); Validator validatorAddress = new ObjectCollectionValidator(typeof(Model.Address), "Create"); try { #region Validation if (!validator.Validate(customer).IsValid) { response.Status = ResponseStatus.ErrorValidation; response.ErrorValue = (int)ErrorCode.InvalidData; foreach (var result in validator.Validate(customer)) { if (!response.Validations.ContainsKey(result.Key)) { response.Validations.Add(result.Key, result.Message); } } return(response); } if (customer.Addresses.IsNullOrEmpty()) { response.Status = ResponseStatus.ErrorValidation; response.ErrorValue = (int)ErrorCode.InvalidData; response.Validations.Add("Address", "Address must have at least one"); return(response); } else { if (!validatorAddress.Validate(customer.Addresses).IsValid) { response.Status = ResponseStatus.ErrorValidation; response.ErrorValue = (int)ErrorCode.InvalidData; foreach (var result in validatorAddress.Validate(customer.Addresses)) { if (!response.Validations.ContainsKey(result.Key)) { response.Validations.Add(result.Key, result.Message); } } return(response); } else // Validate Email { foreach (Model.Address address in customer.Addresses) { if (!address.PrimaryEmail.IsNullOrEmpty()) { Validator <Model.Address> valEmail = ValidationFactory.CreateValidator <Model.Address>("Email"); if (!valEmail.Validate(address).IsValid) { response.Status = ResponseStatus.ErrorValidation; response.ErrorValue = (int)ErrorCode.InvalidData; foreach (var result in valEmail.Validate(address)) { if (!response.Validations.ContainsKey(result.Key)) { response.Validations.Add(result.Key, result.Message); } } return(response); } } } } } #endregion Validation #region Create using (var ctx = new ChickenScratchContext()) { // Set default customer.Status = "A"; customer.TokenSecret = Guid.NewGuid().ToString(); customer.FromDate = DateTime.UtcNow; customer.ToDate = DateTime.UtcNow.AddYears(1); // Create Customer customerRepository = new CustomerRepository(ctx); customerRepository.Add(customer); // Create Adrress addressRepository = new AddressRepository(ctx); addressRepository.Inserts(customer.Addresses); customerRepository.Save(); response.Result = customer; response.Status = ResponseStatus.Success; } #endregion Create return(response); } catch (Exception ex) { throw ex; } }
/// <summary> /// Method to get validation results for collection of attribute driven class /// </summary> /// <typeparam name="T">Generic type</typeparam> /// <param name="items">collection of item</param> /// <returns>returns validation result</returns> public static ValidationResults GetValidationResult <T>(List <T> items) where T : class { Validator collectionValidator = new ObjectCollectionValidator(typeof(T)); return(collectionValidator.Validate(items)); }