Esempio n. 1
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"/>, matches the given regular expression pattern.
        /// </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="pattern">The regular expression pattern.</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 ValidateRegularExpression(this JsonObject value, string key, string pattern)
        {
            if (value == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
            }

            value.ValidatePresence(key);

            string jsonValue = value[key].ReadAs <string>();

            ValidationContext context = new ValidationContext(value, null, null);

            context.MemberName = key;
            Validator.ValidateValue(jsonValue, context, new List <ValidationAttribute> {
                new RegularExpressionAttribute(pattern)
            });
            return(value);
        }
Esempio n. 2
0
        /// <summary>
        /// Validates this object with a custom method.
        /// </summary>
        /// <param name="value">The <see cref="System.Json.JsonObject"/> to which the validation will be applied.</param>
        /// <param name="key">The name of the key to be passed to the validation context.</param>
        /// <param name="type">Tye type where the custom method is located.</param>
        /// <param name="method">The name of the method used to perform the validation.</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 ValidateCustomValidator(this JsonObject value, string key, Type type, string method)
        {
            if (value == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
            }

            value.ValidatePresence(key);

            ValidationContext context = new ValidationContext(value, null, null);

            context.MemberName = key;
            List <ValidationAttribute> attrib = new List <ValidationAttribute> {
                new CustomValidationAttribute(type, method)
            };

            Validator.ValidateValue(value[key], context, attrib);
            return(value);
        }
Esempio n. 3
0
        public static JsonObject ValidateTypeOf <T>(this JsonObject value, string key)
        {
            if (value == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
            }

            value.ValidatePresence(key);

            T tempOfT;

            if (!value[key].TryReadAs <T>(out tempOfT))
            {
                ValidationResult failedResult = new ValidationResult(SG.GetString(SR.NamedValueNotOfType, key, typeof(T).FullName), new List <string> {
                    key
                });
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ValidationException(failedResult, null, null));
            }

            return(value);
        }
Esempio n. 4
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 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="minimumLength">The minimum length allowed for the member value.</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 minimumLength, int maximumLength)
        {
            if (value == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
            }

            value.ValidatePresence(key);

            string jsonValue = value[key].ReadAs <string>();

            ValidationContext context = new ValidationContext(value, null, null);

            context.MemberName = key;
            Validator.ValidateValue(jsonValue, context, new List <ValidationAttribute> {
                new StringLengthAttribute(maximumLength)
                {
                    MinimumLength = minimumLength
                }
            });
            return(value);
        }
Esempio n. 5
0
        /// <summary>
        /// Validates that this object contains a member with the given name, and the value of the member can be
        /// converted into the given enum type.
        /// </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="enumType">The enum type to validate the 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 ValidateEnum(this JsonObject value, string key, Type enumType)
        {
            if (value == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
            }

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

            value.ValidatePresence(key);

            string jsonValue = value[key].ReadAs <string>();

            ValidationContext context = new ValidationContext(value, null, null);

            context.MemberName = key;
            Validator.ValidateValue(jsonValue, context, new List <ValidationAttribute> {
                new EnumDataTypeAttribute(enumType)
            });
            return(value);
        }
Esempio n. 6
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"); });
        }