ForObject() public méthode

public ForObject ( string objectName ) : ValidationErrors
objectName string
Résultat ValidationErrors
 public void ForObject_WorksWithAllCommonCasing()
 {
     ValidationErrors nestedErrors = new ValidationErrors();
     ValidationErrors errors = new ValidationErrors();
     errors.AddErrors("credit-card", nestedErrors);
     Assert.AreEqual(nestedErrors, errors.ForObject("credit-card"));
     Assert.AreEqual(nestedErrors, errors.ForObject("credit_card"));
     Assert.AreEqual(nestedErrors, errors.ForObject("creditCard"));
     Assert.AreEqual(nestedErrors, errors.ForObject("CreditCard"));
 }
        public void ForObject_WithNestedErrors()
        {
            ValidationErrors addressErrors = new ValidationErrors();
            addressErrors.AddError("country_name", new ValidationError("country_name", "91803", "invalid country"));

            ValidationErrors errors = new ValidationErrors();
            errors.AddErrors("address", addressErrors);
            Assert.AreEqual(ValidationErrorCode.ADDRESS_COUNTRY_NAME_IS_NOT_ACCEPTED, errors.ForObject("address").OnField("country_name")[0].Code);
            Assert.AreEqual("invalid country", errors.ForObject("address").OnField("country_name")[0].Message);
        }
        public void DeepCount_WithNestedErrors()
        {
            ValidationErrors addressErrors = new ValidationErrors();
            addressErrors.AddError("country_name", new ValidationError("country_name", "1", "invalid country"));
            addressErrors.AddError("another_field", new ValidationError("another_field", "2", "another message"));

            ValidationErrors errors = new ValidationErrors();
            errors.AddError("some_field", new ValidationError("some_field", "3", "some message"));
            errors.AddErrors("address", addressErrors);

            Assert.AreEqual(3, errors.DeepCount);
            Assert.AreEqual(1, errors.Count);

            Assert.AreEqual(2, errors.ForObject("address").DeepCount);
            Assert.AreEqual(2, errors.ForObject("address").Count);
        }
 public void ForObject_WithNonExistingObject()
 {
     ValidationErrors errors = new ValidationErrors();
     Assert.AreEqual(0, errors.ForObject("address").Count);
 }
        public void DeepAll_ReturnsValidationErrorsAtEveryLevel()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            builder.Append("<api-error-response>");
            builder.Append("  <errors>");
            builder.Append("    <customer>");
            builder.Append("      <errors type=\"array\">");
            builder.Append("        <error>");
            builder.Append("          <code>81608</code>");
            builder.Append("          <message>First name is too long.</message>");
            builder.Append("          <attribute type=\"symbol\">first_name</attribute>");
            builder.Append("        </error>");
            builder.Append("      </errors>");
            builder.Append("      <credit-card>");
            builder.Append("        <errors type=\"array\">");
            builder.Append("          <error>");
            builder.Append("            <code>81715</code>");
            builder.Append("            <message>Credit card number is invalid.</message>");
            builder.Append("            <attribute type=\"symbol\">number</attribute>");
            builder.Append("          </error>");
            builder.Append("          <error>");
            builder.Append("            <code>81710</code>");
            builder.Append("            <message>Expiration date is invalid.</message>");
            builder.Append("            <attribute type=\"symbol\">expiration_date</attribute>");
            builder.Append("          </error>");
            builder.Append("        </errors>");
            builder.Append("      </credit-card>");
            builder.Append("    </customer>");
            builder.Append("    <errors type=\"array\"/>");
            builder.Append("  </errors>");
            builder.Append("</api-error-response>");

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(builder.ToString());
            ValidationErrors errors = new ValidationErrors(new NodeWrapper(doc.ChildNodes[1]));

            Assert.AreEqual(3, errors.DeepAll().Count);

            Assert.AreEqual("first_name", errors.DeepAll()[0].Attribute);
            Assert.AreEqual(ValidationErrorCode.CUSTOMER_FIRST_NAME_IS_TOO_LONG, errors.DeepAll()[0].Code);

            Assert.AreEqual("number", errors.DeepAll()[1].Attribute);
            Assert.AreEqual(ValidationErrorCode.CREDIT_CARD_NUMBER_IS_INVALID, errors.DeepAll()[1].Code);

            Assert.AreEqual("expiration_date", errors.DeepAll()[2].Attribute);
            Assert.AreEqual(ValidationErrorCode.CREDIT_CARD_EXPIRATION_DATE_IS_INVALID, errors.DeepAll()[2].Code);


            Assert.AreEqual(3, errors.ForObject("customer").DeepAll().Count);

            Assert.AreEqual("first_name", errors.ForObject("customer").DeepAll()[0].Attribute);
            Assert.AreEqual(ValidationErrorCode.CUSTOMER_FIRST_NAME_IS_TOO_LONG, errors.ForObject("customer").DeepAll()[0].Code);

            Assert.AreEqual("number", errors.ForObject("customer").DeepAll()[1].Attribute);
            Assert.AreEqual(ValidationErrorCode.CREDIT_CARD_NUMBER_IS_INVALID, errors.ForObject("customer").DeepAll()[1].Code);

            Assert.AreEqual("expiration_date", errors.ForObject("customer").DeepAll()[2].Attribute);
            Assert.AreEqual(ValidationErrorCode.CREDIT_CARD_EXPIRATION_DATE_IS_INVALID, errors.ForObject("customer").DeepAll()[2].Code);
        }
        public void Constructor_ParsesValidationErrorsAtMultipleLevels()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            builder.Append("<api-error-response>");
            builder.Append("  <errors>");
            builder.Append("    <customer>");
            builder.Append("      <errors type=\"array\">");
            builder.Append("        <error>");
            builder.Append("          <code>81608</code>");
            builder.Append("          <message>First name is too long.</message>");
            builder.Append("          <attribute type=\"symbol\">first_name</attribute>");
            builder.Append("        </error>");
            builder.Append("      </errors>");
            builder.Append("      <credit-card>");
            builder.Append("        <billing-address>");
            builder.Append("          <errors type=\"array\">");
            builder.Append("            <error>");
            builder.Append("              <code>91803</code>");
            builder.Append("              <message>Country name is not an accepted country.</message>");
            builder.Append("              <attribute type=\"symbol\">country_name</attribute>");
            builder.Append("            </error>");
            builder.Append("          </errors>");
            builder.Append("        </billing-address>");
            builder.Append("        <errors type=\"array\">");
            builder.Append("          <error>");
            builder.Append("            <code>81715</code>");
            builder.Append("            <message>Credit card number is invalid.</message>");
            builder.Append("            <attribute type=\"symbol\">number</attribute>");
            builder.Append("          </error>");
            builder.Append("        </errors>");
            builder.Append("      </credit-card>");
            builder.Append("    </customer>");
            builder.Append("    <errors type=\"array\"/>");
            builder.Append("  </errors>");
            builder.Append("</api-error-response>");

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(builder.ToString());
            ValidationErrors errors = new ValidationErrors(new NodeWrapper(doc.ChildNodes[1]));

            Assert.AreEqual(3, errors.DeepCount);
            Assert.AreEqual(0, errors.Count);

            Assert.AreEqual(3, errors.ForObject("customer").DeepCount);
            Assert.AreEqual(1, errors.ForObject("customer").Count);
            Assert.AreEqual(ValidationErrorCode.CUSTOMER_FIRST_NAME_IS_TOO_LONG, errors.ForObject("customer").OnField("first_name")[0].Code);

            Assert.AreEqual(2, errors.ForObject("customer").ForObject("credit-card").DeepCount);
            Assert.AreEqual(1, errors.ForObject("customer").ForObject("credit-card").Count);
            Assert.AreEqual(ValidationErrorCode.CREDIT_CARD_NUMBER_IS_INVALID, errors.ForObject("customer").ForObject("credit-card").OnField("number")[0].Code);

            Assert.AreEqual(1, errors.ForObject("customer").ForObject("credit-card").ForObject("billing-address").DeepCount);
            Assert.AreEqual(1, errors.ForObject("customer").ForObject("credit-card").ForObject("billing-address").Count);
            Assert.AreEqual(ValidationErrorCode.ADDRESS_COUNTRY_NAME_IS_NOT_ACCEPTED, errors.ForObject("customer").ForObject("credit-card").ForObject("billing-address").OnField("country_name")[0].Code);
        }
        public void Constructor_ParsesMultipleErrorsOnSingleField()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            builder.Append("<api-error-response>");
            builder.Append("  <errors>");
            builder.Append("    <transaction>");
            builder.Append("      <errors type=\"array\">");
            builder.Append("        <error>");
            builder.Append("          <code>91516</code>");
            builder.Append("          <message>Cannot provide both payment_method_token and customer_id unless the payment_method belongs to the customer.</message>");
            builder.Append("          <attribute type=\"symbol\">base</attribute>");
            builder.Append("        </error>");
            builder.Append("        <error>");
            builder.Append("          <code>91515</code>");
            builder.Append("          <message>Cannot provide both payment_method_token and credit_card attributes.</message>");
            builder.Append("          <attribute type=\"symbol\">base</attribute>");
            builder.Append("        </error>");
            builder.Append("      </errors>");
            builder.Append("    </transaction>");
            builder.Append("    <errors type=\"array\"/>");
            builder.Append("  </errors>");
            builder.Append("</api-error-response>");

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(builder.ToString());
            ValidationErrors errors = new ValidationErrors(new NodeWrapper(doc.ChildNodes[1]));
            Assert.AreEqual(2, errors.DeepCount);
            Assert.AreEqual(2, errors.ForObject("transaction").OnField("base").Count);
        }
        public void Constructor_ParsesValidationErrorOnNestedObject()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            builder.Append("<api-error-response>");
            builder.Append("  <errors>");
            builder.Append("    <errors type=\"array\"/>");
            builder.Append("    <credit-card>");
            builder.Append("      <billing-address>");
            builder.Append("        <errors type=\"array\">");
            builder.Append("          <error>");
            builder.Append("            <code>91803</code>");
            builder.Append("            <message>Country name is not an accepted country.</message>");
            builder.Append("            <attribute type=\"symbol\">country_name</attribute>");
            builder.Append("          </error>");
            builder.Append("        </errors>");
            builder.Append("      </billing-address>");
            builder.Append("      <errors type=\"array\"/>");
            builder.Append("    </credit-card>");
            builder.Append("  </errors>");
            builder.Append("</api-error-response>");

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(builder.ToString());
            ValidationErrors errors = new ValidationErrors(new NodeWrapper(doc.ChildNodes[1]));
            Assert.AreEqual(1, errors.DeepCount);
            Assert.AreEqual(ValidationErrorCode.ADDRESS_COUNTRY_NAME_IS_NOT_ACCEPTED, errors.ForObject("credit-card").ForObject("billing-address").OnField("country_name")[0].Code);
        }
        public void Constructor_ParsesMulitpleValidationErrorsOnOneObject()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            builder.Append("<api-error-response>");
            builder.Append("  <errors>");
            builder.Append("    <address>");
            builder.Append("      <errors type=\"array\">");
            builder.Append("        <error>");
            builder.Append("          <code>91803</code>");
            builder.Append("          <message>Country name is not an accepted country.</message>");
            builder.Append("          <attribute type=\"symbol\">country_name</attribute>");
            builder.Append("        </error>");
            builder.Append("        <error>");
            builder.Append("          <code>81812</code>");
            builder.Append("          <message>Street address is too long.</message>");
            builder.Append("          <attribute type=\"symbol\">street_address</attribute>");
            builder.Append("        </error>");
            builder.Append("      </errors>");
            builder.Append("    </address>");
            builder.Append("    <errors type=\"array\"/>");
            builder.Append("  </errors>");
            builder.Append("</api-error-response>");

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(builder.ToString());
            ValidationErrors errors = new ValidationErrors(new NodeWrapper(doc.ChildNodes[1]));
            Assert.AreEqual(2, errors.DeepCount);
            Assert.AreEqual(ValidationErrorCode.ADDRESS_COUNTRY_NAME_IS_NOT_ACCEPTED, errors.ForObject("address").OnField("country_name")[0].Code);
            Assert.AreEqual(ValidationErrorCode.ADDRESS_STREET_ADDRESS_IS_TOO_LONG, errors.ForObject("address").OnField("street_address")[0].Code);
        }