public override void Validate(Validations validationType = Validations.Weak) { base.Validate(validationType); // All properties are optional, so we only validated when they're filled. if (LineItems != null) { LineItems.ToList().ForEach(item => item.Validate(validationType)); } if (ShippingAddress != null) { ShippingLines.ToList().ForEach(item => item.Validate(validationType)); } if (PaymentDetails != null && PaymentDetails.Length > 0) { PaymentDetails.ToList().ForEach(item => item.Validate(validationType)); } if (NoChargeAmount != null) { NoChargeAmount.Validate(validationType); } if (validationType == Validations.Weak) { if (BillingAddress != null) { BillingAddress.Validate(validationType); } else if (ShippingAddress != null) { ShippingAddress.Validate(validationType); } } else { if (BillingAddress != null) { BillingAddress.Validate(validationType); } if (ShippingAddress != null) { ShippingAddress.Validate(validationType); } } if (Customer != null) { Customer.Validate(validationType); } if (!string.IsNullOrEmpty(Email)) { InputValidators.ValidateEmail(Email); } if (!string.IsNullOrEmpty(CustomerBrowserIp)) { InputValidators.ValidateIp(CustomerBrowserIp); } if (!string.IsNullOrEmpty(Currency)) { InputValidators.ValidateCurrency(Currency); } if (TotalPrice.HasValue) { InputValidators.ValidateZeroOrPositiveValue(TotalPrice.Value, "Total Price"); } if (CreatedAt != null) { InputValidators.ValidateDateNotDefault(CreatedAt.Value, "Created At"); } if (UpdatedAt != null) { InputValidators.ValidateDateNotDefault(UpdatedAt.Value, "Updated At"); } if (DiscountCodes != null && DiscountCodes.Length > 0) { DiscountCodes.ToList().ForEach(item => item.Validate(validationType)); } if (TotalPriceUsd.HasValue) { InputValidators.ValidateZeroOrPositiveValue(TotalPriceUsd.Value, "Total Price USD"); } if (TotalDiscounts.HasValue) { InputValidators.ValidateZeroOrPositiveValue(TotalDiscounts.Value, "Total Discounts"); } if (ClosedAt.HasValue) { InputValidators.ValidateDateNotDefault(ClosedAt.Value, "Closed At"); } if (Decision != null) { Decision.Validate(validationType); } }
/// <summary> /// Validates the objects fields content /// </summary> /// <param name="isWeak">Should use weak validations or strong</param> /// <exception cref="OrderFieldBadFormatException">throws an exception if one of the parameters doesn't match the expected format</exception> public override void Validate(Validations validationType = Validations.Weak) { base.Validate(validationType); InputValidators.ValidateObjectNotNull(LineItems, "Line Items"); LineItems.ToList().ForEach(item => item.Validate(validationType)); InputValidators.ValidateObjectNotNull(ShippingLines, "Shipping Lines"); ShippingLines.ToList().ForEach(item => item.Validate(validationType)); if (PaymentDetails == null && NoChargeAmount == null) { throw new Exceptions.OrderFieldBadFormatException("Both PaymentDetails and NoChargeDetails are missing - at least one should be specified"); } if (PaymentDetails != null) { PaymentDetails.Validate(validationType); } else { NoChargeAmount.Validate(validationType); } if (validationType == Validations.Weak) { if (BillingAddress == null && ShippingAddress == null) { throw new Exceptions.OrderFieldBadFormatException("Both shipping and billing addresses are missing - at least one should be specified"); } if (BillingAddress != null) { BillingAddress.Validate(validationType); } else { ShippingAddress.Validate(validationType); } } else { InputValidators.ValidateObjectNotNull(BillingAddress, "Billing Address"); BillingAddress.Validate(validationType); InputValidators.ValidateObjectNotNull(ShippingAddress, "Shipping Address"); ShippingAddress.Validate(validationType); } InputValidators.ValidateObjectNotNull(Customer, "Customer"); Customer.Validate(validationType); InputValidators.ValidateEmail(Email); InputValidators.ValidateIp(CustomerBrowserIp); InputValidators.ValidateCurrency(Currency); InputValidators.ValidateZeroOrPositiveValue(TotalPrice.Value, "Total Price"); InputValidators.ValidateValuedString(Gateway, "Gateway"); InputValidators.ValidateDateNotDefault(CreatedAt.Value, "Created At"); InputValidators.ValidateDateNotDefault(UpdatedAt.Value, "Updated At"); // optional fields validations if (DiscountCodes != null && DiscountCodes.Length > 0) { DiscountCodes.ToList().ForEach(item => item.Validate(validationType)); } if (TotalPriceUsd.HasValue) { InputValidators.ValidateZeroOrPositiveValue(TotalPriceUsd.Value, "Total Price USD"); } if (TotalDiscounts.HasValue) { InputValidators.ValidateZeroOrPositiveValue(TotalDiscounts.Value, "Total Discounts"); } if (ClosedAt.HasValue) { InputValidators.ValidateDateNotDefault(ClosedAt.Value, "Closed At"); } }