private void SetCustomerAddressesRule() { var key = "addresses"; if (RequestJsonDictionary.ContainsKey(key)) { RuleForEach(c => c.Addresses) .Custom((addressDto, validationContext) => { var addressJsonDictionary = GetRequestJsonDictionaryCollectionItemDictionary(key, addressDto); var validator = new AddressDtoValidator(HttpContextAccessor, JsonHelper, addressJsonDictionary); //force create validation for new addresses if (addressDto.Id == 0) { validator.HttpMethod = HttpMethod.Post; } var validationResult = validator.Validate(addressDto); MergeValidationResult(validationContext, validationResult); }); } }
protected void SetGreaterThanZeroCreateOrUpdateRule(Expression <Func <T, int?> > expression, string errorMessage, string requestValueKey) { if (HttpMethod == HttpMethod.Post || RequestJsonDictionary.ContainsKey(requestValueKey)) { SetGreaterThanZeroRule(expression, errorMessage); } }
private void SetShoppingCartItemsRule() { var key = "shopping_cart_items"; if (RequestJsonDictionary.ContainsKey(key)) { RuleForEach(c => c.ShoppingCartItems) .Custom((shoppingCartItemDto, validationContext) => { var shoppingCartItemJsonDictionary = GetRequestJsonDictionaryCollectionItemDictionary(key, shoppingCartItemDto); var validator = new ShoppingCartItemDtoValidator(HttpContextAccessor, JsonHelper, shoppingCartItemJsonDictionary); //force create validation for new addresses if (shoppingCartItemDto.Id == 0) { validator.HttpMethod = HttpMethod.Post; } var validationResult = validator.Validate(shoppingCartItemDto); MergeValidationResult(validationContext, validationResult); }); } }
private void SetRentalDateRules() { if (RequestJsonDictionary.ContainsKey("rental_start_date_utc") || RequestJsonDictionary.ContainsKey("rental_end_date_utc")) { RuleFor(x => x.RentalStartDateUtc) .NotNull() .WithMessage("Please provide a rental start date"); RuleFor(x => x.RentalEndDateUtc) .NotNull() .WithMessage("Please provide a rental end date"); RuleFor(dto => dto) .Must(dto => dto.RentalStartDateUtc < dto.RentalEndDateUtc) .WithMessage("Rental start date should be before rental end date"); RuleFor(dto => dto) .Must(dto => dto.RentalStartDateUtc > dto.CreatedOnUtc) .WithMessage("Rental start date should be the future date"); RuleFor(dto => dto) .Must(dto => dto.RentalEndDateUtc > dto.CreatedOnUtc) .WithMessage("Rental end date should be the future date"); } }
protected void SetNotNullOrEmptyCreateOrUpdateRule(Expression <Func <T, string> > expression, string errorMessage, string requestValueKey) { if (HttpMethod == HttpMethod.Post || RequestJsonDictionary.ContainsKey(requestValueKey)) { SetNotNullOrEmptyRule(expression, errorMessage); } }
public SpecificationAttributeOptionDtoValidator(IHttpContextAccessor httpContextAccessor, IJsonHelper jsonHelper, Dictionary <string, object> requestJsonDictionary) : base(httpContextAccessor, jsonHelper, requestJsonDictionary) { if (HttpMethod == HttpMethod.Post) { //apply "create" rules RuleFor(x => x.Id).Equal(0).WithMessage("id must be zero or null for new records"); ApplyNameRule(); ApplySpecificationAttributeIdRule(); } else if (HttpMethod == HttpMethod.Put) { //apply "update" rules RuleFor(x => x.Id).GreaterThan(0).WithMessage("invalid id"); if (RequestJsonDictionary.ContainsKey("name")) { ApplyNameRule(); } if (RequestJsonDictionary.ContainsKey("specification_attribute_id")) { ApplySpecificationAttributeIdRule(); } } }
private void SetShippingAddressRule() { var key = "shipping_address"; if (RequestJsonDictionary.ContainsKey(key)) { RuleFor(c => c.ShippingAddress).SetValidator(new AddressDtoValidator(HttpContextAccessor, JsonHelper, (Dictionary <string, object>)RequestJsonDictionary[key])); } }
private void SetShoppingCartTypeRule() { if (HttpMethod == HttpMethod.Post || RequestJsonDictionary.ContainsKey("shopping_cart_type")) { RuleFor(x => x.ShoppingCartType) .NotNull() .Must(x => { var parsed = Enum.TryParse(x, true, out ShoppingCartType _); return(parsed); }) .WithMessage("Please provide a valid shopping cart type"); } }
private void SetRolesRule() { if (HttpMethod == HttpMethod.Post || RequestJsonDictionary.ContainsKey("role_ids")) { IList <CustomerRole> customerRoles = null; // async validation: https://docs.fluentvalidation.net/en/latest/async.html RuleFor(x => x.RoleIds) .NotNull() .Must(roles => roles.Count > 0) .WithMessage("role_ids required") .DependentRules(() => RuleFor(dto => dto.RoleIds) .MustAsync(async(roleIds, cancellation) => { if (customerRoles == null) { customerRoles = await _customerRolesHelper.GetValidCustomerRolesAsync(roleIds); } var isInGuestAndRegisterRoles = _customerRolesHelper.IsInGuestsRole(customerRoles) && _customerRolesHelper.IsInRegisteredRole(customerRoles); // Customer can not be in guest and register roles simultaneously return(!isInGuestAndRegisterRoles); }) .WithMessage("must not be in guest and register roles simultaneously") .DependentRules(() => RuleFor(dto => dto.RoleIds) .MustAsync(async(roleIds, cancellation) => { if (customerRoles == null) { customerRoles = await _customerRolesHelper.GetValidCustomerRolesAsync(roleIds); } var isInGuestOrRegisterRoles = _customerRolesHelper.IsInGuestsRole(customerRoles) || _customerRolesHelper.IsInRegisteredRole(customerRoles); // Customer must be in either guest or register role. return(isInGuestOrRegisterRoles); }) .WithMessage("must be in guest or register role") ) ); } }
private void SetRoleIdsRule() { if (HttpMethod == HttpMethod.Post || RequestJsonDictionary.ContainsKey("role_ids")) { IList <Role> roles = null; RuleFor(x => x.RoleIds) .NotNull() .Must(r => r.Count > 0) .WithMessage("role_ids required") .DependentRules(() => RuleFor(dto => dto.RoleIds) .Must(roleIds => { if (roles == null) { roles = _userRolesHelper.GetValidRoles(roleIds); } var isInGuestAndRegisterRoles = _userRolesHelper.IsInGuestsRole(roles) && _userRolesHelper.IsInRegisteredRole(roles); return(!isInGuestAndRegisterRoles); }) .WithMessage("must not be in guest and register roles simultaneously") .DependentRules(() => RuleFor(dto => dto.RoleIds) .Must(roleIds => { if (roles == null) { roles = _userRolesHelper.GetValidRoles(roleIds); } var isInGuestOrRegisterRoles = _userRolesHelper.IsInGuestsRole(roles) || _userRolesHelper.IsInRegisteredRole(roles); return(isInGuestOrRegisterRoles); }) .WithMessage("must be in guest or register role") ) ); } }
private void SetStoreIdsRule() { if (HttpMethod == HttpMethod.Post || RequestJsonDictionary.ContainsKey("store_ids")) { IList <Store> stores = null; RuleFor(x => x.StoreIds) .NotNull() .Must(r => r.Count > 0) .WithMessage("store_ids required") .DependentRules(() => RuleFor(dto => dto.StoreIds) .Must(storeIds => { if (stores == null) { stores = _storeHelper.GetValidStores(storeIds); } return(stores.Any()); }) .WithMessage("invalid store_ids") ); } }
private void SetBranchNoRule() { if (HttpMethod == HttpMethod.Post || RequestJsonDictionary.ContainsKey("branch_no")) { Store store = null; RuleFor(x => x.P_BranchNo) .NotNull() .NotEmpty() .Must(id => id > 0) .WithMessage("branch_no required") .DependentRules(() => RuleFor(dto => dto.P_BranchNo) .Must(id => { if (store == null) { store = _storeHelper.GetValidStore(id.GetValueOrDefault()); } return(store != null); }).WithMessage("invalid branch_no") ); } }
private void SetReviewTypeMappingRule() { var key = "review_type_mappings"; if (RequestJsonDictionary.ContainsKey(key)) { RuleForEach(pr => pr.ReviewTypeMappingsDto) .Custom((reviewTypeMappingDto, validationContext) => { var productTypeMappinDtoJsonDictionary = GetRequestJsonDictionaryCollectionItemDictionary(key, reviewTypeMappingDto); var validator = new ProductReviewReviewTypeMappingDtoValidator(HttpContextAccessor, JsonHelper, productTypeMappinDtoJsonDictionary); //force create validation for new review type mapping if (reviewTypeMappingDto.Id == 0) { validator.HttpMethod = HttpMethod.Post; } var validationResult = validator.Validate(reviewTypeMappingDto); MergeValidationResult(validationContext, validationResult); }); } }
private void SetStoreIdRule() { if (HttpMethod == HttpMethod.Post || RequestJsonDictionary.ContainsKey("store_id")) { Store store = null; RuleFor(x => x.StoreId) .NotNull() .NotEmpty() .Must(id => id > 0) .WithMessage("store_id required") .DependentRules(() => RuleFor(dto => dto.StoreId) .Must(id => { if (store == null) { store = _storeHelper.GetValidStore(id); } return(store != null); }).WithMessage("invalid store_id") ); } }