예제 #1
0
        /// <summary>
        /// Validates that the member with the given name can be read as a <see cref="System.Double"/>, and when read as such
        /// it is within the given range.
        /// </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="min">The lower bound of the range check.</param>
        /// <param name="max">The upper bound of the range check.</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 ValidateRange(this JsonObject value, string key, double min, double max)
        {
            if (value == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
            }

            value.ValidateTypeOf <double>(key);
            return(value.ValidateRange <double>(key, value[key].ReadAs <double>(), new RangeAttribute(min, max)));
        }
예제 #2
0
        /// <summary>
        /// Validates that the member with the given name can be read as the given type, and when read as such
        /// it is within the given range.
        /// </summary>
        /// <typeparam name="T">The type of the member.</typeparam>
        /// <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="min">The lower bound of the range check.</param>
        /// <param name="max">The upper bound of the range check.</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 ValidateRange <T>(this JsonObject value, string key, T min, T max) where T : IComparable
        {
            if (value == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
            }

            if (min == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("min");
            }

            if (max == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("max");
            }

            value.ValidateTypeOf <T>(key);
            return(value.ValidateRange <T>(key, value[key].ReadAs <T>(), new RangeAttribute(typeof(T), min.ToString(), max.ToString())));
        }
예제 #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"); });
        }