Example #1
0
        /// <summary>
        ///     Validates the objects fields content
        /// </summary>
        /// <param name="validationType"></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 OrderFieldBadFormatException("Both PaymentDetails and NoChargeDetails are missing - at least one should be specified");
            }
            if (PaymentDetails != null && PaymentDetails.Length > 0)
            {
                PaymentDetails.ToList().ForEach(item => item.Validate(validationType));
            }
            else
            {
                NoChargeAmount.Validate(validationType);
            }

            if (validationType == Validations.Weak)
            {
                if (BillingAddress == null && ShippingAddress == null)
                {
                    throw new OrderFieldBadFormatException("Both shipping and billing addresses are missing - at least one should be specified");
                }

                if (BillingAddress != null)
                {
                    BillingAddress.Validate(validationType);
                }
                else
                {
                    ShippingAddress.Validate(validationType);
                }

                if (CustomerBrowserIp != null)
                {
                    InputValidators.ValidateIp(CustomerBrowserIp);
                }
            }
            else
            {
                InputValidators.ValidateObjectNotNull(BillingAddress, "Billing Address");
                BillingAddress.Validate(validationType);
                InputValidators.ValidateObjectNotNull(ShippingAddress, "Shipping Address");
                ShippingAddress.Validate(validationType);
                InputValidators.ValidateIp(CustomerBrowserIp);
            }

            InputValidators.ValidateObjectNotNull(Customer, "Customer");
            Customer.Validate(validationType);
            InputValidators.ValidateEmail(Email);
            InputValidators.ValidateCurrency(Currency);
            InputValidators.ValidateZeroOrPositiveValue(TotalPrice.GetValueOrDefault(), "Total Price");
            InputValidators.ValidateValuedString(Gateway, "Gateway");
            InputValidators.ValidateDateNotDefault(CreatedAt.GetValueOrDefault(), "Created At");
            InputValidators.ValidateDateNotDefault(UpdatedAt.GetValueOrDefault(), "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");
            }
        }