Пример #1
0
        public JsonObject Post(JsonObject contact)
        {
            if (contact == null)
            {
                throw new ArgumentNullException("contact");
            }

            contact.ValidateStringLength("What", 1, MaxFieldLength)
                   .ValidateCustomValidator("StartDate", typeof(CustomValidator), "ValidateMeetingTime")
                   .ValidateStringLength("Where", 1, MaxFieldLength)
                   .ValidateStringLength("Description", MaxFieldLength)
                   .ValidateCustomValidator("ReminderValue", typeof(CustomValidator), "ValidateReminder")
                   .ValidateEnum("ShowMeAs", typeof(ShowMeAs))
                   .ValidateCustomValidator("Guests", typeof(CustomValidator), "ValidateGuestEmails");

            string modifyEventName = "ModifyEvent";
            if (contact.ContainsKey(modifyEventName))
            {
                contact.ValidateTypeOf<bool>(modifyEventName);
            }

            string inviteOthersName = "InviteOthers";
            if (contact.ContainsKey(inviteOthersName))
            {
                contact.ValidateTypeOf<bool>(inviteOthersName);
            }

            string seeGuestListName = "SeeGuestList";
            if (contact.ContainsKey(seeGuestListName))
            {
                contact.ValidateTypeOf<bool>(seeGuestListName);
            }

            return new JsonObject();
        }
Пример #2
0
        /// <summary>
        /// Validates that this object contains a member with the given name, and the value of the member, read as
        /// <see cref="System.String"/>, has a length not greater than the given value.
        /// </summary>
        /// <param name="value">The <see cref="System.Json.JsonObject"/> to which the validation will be applied.</param>
        /// <param name="key">The key of the member to search.</param>
        /// <param name="maximumLength">The maximum length allowed for the member value.</param>
        /// <returns>This object, so that other validation operations can be chained.</returns>
        /// <exception cref="System.ComponentModel.DataAnnotations.ValidationException">If the validation failed.</exception>
        public static JsonObject ValidateStringLength(this JsonObject value, string key, int maximumLength)
        {
            if (value == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
            }

            return(value.ValidateStringLength(key, 0, maximumLength));
        }
Пример #3
0
        public void ValidationAPITest()
        {
            JsonObject jo = new JsonObject();
            jo.Add("date", new DateTime(2000, 1, 1, 0, 0, 0));
            jo.Add("int", 1);
            jo.Add("double", 1.1);
            jo.Add("string", "12CharString");
            jo.Add("enum", "Number");

            jo.ValidatePresence("date")
              .ValidateEnum("enum", typeof(JsonType))
              .ValidateRange("double", 0.1, 1.2)
              .ValidateRange("int", 0, 2)
              .ValidateRange<DateTime>("date", DateTime.MinValue, DateTime.MaxValue)
              .ValidateRegularExpression("int", "^.+$")
              .ValidateStringLength("string", 15)
              .ValidateStringLength("string", 0, 15)
              .ValidateTypeOf<double>("int")
              .ValidateCustomValidator("string", typeof(MyCustomValidationClass), "IsStringContainsCharSimple")
              .ValidateCustomValidator("string", typeof(MyCustomValidationClass), "IsStringContainsCharComplex");

            ExceptionTestHelper.ExpectException<ValidationException>(delegate { jo.ValidatePresence("invalidkey"); });
            ExceptionTestHelper.ExpectException<ValidationException>(delegate { jo.ValidateEnum("date", typeof(JsonType)); });
            ExceptionTestHelper.ExpectException<ValidationException>(delegate { jo.ValidateRange("double", 2.2, 3.2); });
            ExceptionTestHelper.ExpectException<ValidationException>(delegate { jo.ValidateRange("int", 2, 3); });
            ExceptionTestHelper.ExpectException<ValidationException>(delegate { jo.ValidateRange<DateTime>("date", DateTime.MaxValue, DateTime.MaxValue); });
            ExceptionTestHelper.ExpectException<ValidationException>(delegate { jo.ValidateRegularExpression("string", "doesnotmatch"); });
            ExceptionTestHelper.ExpectException<ValidationException>(delegate { jo.ValidateStringLength("string", 10); });
            ExceptionTestHelper.ExpectException<ValidationException>(delegate { jo.ValidateStringLength("string", 15, 25); });
            ExceptionTestHelper.ExpectException<ValidationException>(delegate { jo.ValidateTypeOf<double>("date"); });
            ExceptionTestHelper.ExpectException<ValidationException>(delegate { jo.ValidateCustomValidator("enum", typeof(MyCustomValidationClass), "IsStringContainsCharSimple"); });
            ExceptionTestHelper.ExpectException<ValidationException>(delegate { jo.ValidateCustomValidator("enum", typeof(MyCustomValidationClass), "IsStringContainsCharComplex"); });
        }