public void ValidateSku_When_not_uniqe_Then_fails() { var validator = new ProductRequestCustomValidator(); var request = ProductCreateRequestBuilder.Create() .WithProduct(ProductDtoBuilder.Create() .WithVariant(ProductVariantDtoBuilder.Create() .WithSku("001") .Build()) .WithVariant(ProductVariantDtoBuilder.Create() .WithSku("000") .Build()) .Build()) .Build(); var errors = new ArgumentErrorsCollection(); var dbContextMock = GetDbContextMock(); validator.ValidateSkus(dbContextMock.Object, request, errors); dbContextMock.VerifyGet(c => c.Products, Times.AtLeast(request.Product.Variants.Count)); Assert.IsNotEmpty(errors); Assert.IsTrue(errors.HasArgument("Product.Variants.Sku"), errors[0].ArgumentName); }
public void ValidateSku_When_not_uniqe_Then_fails() { var validator = new ProductRequestCustomValidator(); var request = ProductCreateRequestBuilder.Create() .WithProduct(ProductDtoBuilder.Create() .WithVariant(ProductVariantDtoBuilder.Create() .WithSku("001") .Build()) .WithVariant(ProductVariantDtoBuilder.Create() .WithSku("000") .Build()) .Build()) .Build(); var errors = new ArgumentErrorsCollection(); var dbContextMock = GetDbContextMock(); validator.ValidateSkus(dbContextMock.Object, request, errors); dbContextMock.VerifyGet(c => c.Products, Times.AtLeast(request.Product.Variants.Count)); Assert.IsNotEmpty(errors); Assert.IsTrue(errors.HasArgument("Product.Variants.Sku"), errors[0].ArgumentName); }
public virtual void ValidatePartner(IDbContext dbContext, PartnerAuthenticateRequest request, ArgumentErrorsCollection errors) { var action = new RequestValidatorAction <PartnerAuthenticateRequest, string>(r => r.Key, (key) => IsPartnerExists(dbContext, key) && IsPartnerSecretMatches(dbContext, key, request.Secret), PartnerRequestCustomValidatorErrorCode.PartnerNotFound); TryValidate(action, request, errors); }
public static bool VisitAndValidateProperties(object dto, ArgumentErrorsCollection errors, string rootName = null) { var valid = true; var validationResults = new List<ValidationResult>(); var validationContext = new ValidationContext(dto, serviceProvider: null, items: null); valid = Validator.TryValidateObject(dto, validationContext, validationResults, true); //check if o is collection if (dto.GetType().GetInterface("IEnumerable") != null) { var enumerator = ((IEnumerable)dto).GetEnumerator(); while (enumerator.MoveNext()) { valid = VisitAndValidateProperties(enumerator.Current, errors, rootName) && valid; } } else { foreach (var propertyInfo in dto.GetType().GetProperties()) { if (!(propertyInfo.PropertyType.IsPrimitive || propertyInfo.PropertyType == typeof(String) || propertyInfo.PropertyType == typeof(Decimal) || propertyInfo.PropertyType == typeof(Decimal?) || propertyInfo.PropertyType == typeof(DateTime) || propertyInfo.PropertyType == typeof(DateTime?))) { var val = propertyInfo.GetValue(dto); if (val != null) { valid = VisitAndValidateProperties(val, errors, (rootName != null ? rootName + "." : "") + propertyInfo.Name) && valid; } } } } foreach (var validationResult in validationResults) { foreach (var memberName in validationResult.MemberNames) { errors[(rootName != null ? rootName + "." : "") + memberName] = new Error(RequestValidatorErrorCode.DtoValidationFailed, validationResult.ErrorMessage); } } return valid; }
public void ValidateStoreId_When_existing_store_Then_passes() { var validator = new ProductRequestCustomValidator(); var errors = new ArgumentErrorsCollection(); var requestMock = new Mock <ProductGetByIdRequest>(); var dbContextMock = GetDbContextMock(); requestMock.SetupGet(r => r.StoreId).Returns(10); validator.ValidateStoreId(dbContextMock.Object, requestMock.Object, errors); dbContextMock.VerifyGet(c => c.Stores, Times.Once); Assert.IsEmpty(errors); }
public void ValidateStoreId_When_existing_store_Then_passes() { var validator = new ProductRequestCustomValidator(); var errors = new ArgumentErrorsCollection(); var requestMock = new Mock<ProductGetByIdRequest>(); var dbContextMock = GetDbContextMock(); requestMock.SetupGet(r => r.StoreId).Returns(10); validator.ValidateStoreId(dbContextMock.Object, requestMock.Object, errors); dbContextMock.VerifyGet(c => c.Stores, Times.Once); Assert.IsEmpty(errors); }
public static bool VisitAndValidateProperties(object dto, ArgumentErrorsCollection errors, string rootName = null) { var valid = true; var validationResults = new List <ValidationResult>(); var validationContext = new ValidationContext(dto, serviceProvider: null, items: null); valid = Validator.TryValidateObject(dto, validationContext, validationResults, true); //check if o is collection if (dto.GetType().GetInterface("IEnumerable") != null) { var enumerator = ((IEnumerable)dto).GetEnumerator(); while (enumerator.MoveNext()) { valid = VisitAndValidateProperties(enumerator.Current, errors, rootName) && valid; } } else { foreach (var propertyInfo in dto.GetType().GetProperties()) { if (!(propertyInfo.PropertyType.IsPrimitive || propertyInfo.PropertyType == typeof(Decimal) || propertyInfo.PropertyType == typeof(String))) { var val = propertyInfo.GetValue(dto); if (val != null) { valid = VisitAndValidateProperties(val, errors, (rootName != null ? rootName + "." : "") + propertyInfo.Name) && valid; } } } } foreach (var validationResult in validationResults) { foreach (var memberName in validationResult.MemberNames) { errors[(rootName != null ? rootName + "." : "") + memberName] = new Error(RequestValidatorErrorCode.DtoValidationFailed, validationResult.ErrorMessage); } } return(valid); }
public bool TryValidate <TEntity, TMember>(RequestValidatorAction <TEntity, TMember> action, TEntity entity, ArgumentErrorsCollection existingErrors) { var memberName = PropertyHelper.GetName(action.ForMember); if (existingErrors[memberName] == null) { var memberValue = action.ForMember.Compile().Invoke(entity); var result = action.ValidationExpression.Compile().Invoke(memberValue); if (!result) { var errorMessage = RequestValidatorErrorMessagesDictionary.Instance[action.ValidationErrorCode]; existingErrors[memberName] = new Error(action.ValidationErrorCode, errorMessage); } return(result); } return(false); }
public void ValidateSku_When_unique_Then_passes() { var validator = new ProductRequestCustomValidator(); var request = ProductCreateRequestBuilder.Create().WithProduct( ProductDtoBuilder.Create().WithVariant( ProductVariantDtoBuilder.Create().WithSku("001") .Build()) .Build()) .Build(); var errors = new ArgumentErrorsCollection(); var dbContextMock = GetDbContextMock(); validator.ValidateSkus(dbContextMock.Object, request, errors); dbContextMock.VerifyGet(c => c.Products, Times.Once); Assert.IsEmpty(errors); }
public void ValidateSku_When_unique_Then_passes() { var validator = new ProductRequestCustomValidator(); var request = ProductCreateRequestBuilder.Create().WithProduct( ProductDtoBuilder.Create().WithVariant( ProductVariantDtoBuilder.Create().WithSku("001") .Build()) .Build()) .Build(); var errors = new ArgumentErrorsCollection(); var dbContextMock = GetDbContextMock(); validator.ValidateSkus(dbContextMock.Object, request, errors); dbContextMock.VerifyGet(c => c.Products, Times.Once); Assert.IsEmpty(errors); }
protected TExternalResponse ErrorResponse(Error error, ArgumentErrorsCollection argumentErrors = null, string extraMessage = null) { Response.IsSuccess = false; Response.Error = error; if (argumentErrors != null) { foreach (var argumentError in argumentErrors) { Response.ArgumentErrors[argumentError.ArgumentName] = argumentError; } } if (extraMessage != null) { Response.Error.ErrorMessage += Environment.NewLine + extraMessage; } return(Response); }
public InternalResponse() { ArgumentErrors = new ArgumentErrorsCollection(); }
protected InternalResponse() { ArgumentErrors = new ArgumentErrorsCollection(); }