Exemple #1
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"); });
        }