コード例 #1
0
        /// <summary>
        /// Validate your json with Json.NET Schema. See http://www.newtonsoft.com/jsonschema and https://github.com/FrendsPlatform/Frends.Json
        /// </summary>
        /// <returns>Object { bool IsValid, string Error }</returns>
        public static ValidateResult Validate([CustomDisplay(DisplayOption.Tab)] ValidateInput input, [CustomDisplay(DisplayOption.Tab)] ValidateOption options)
        {
            var    schema = JSchema.Parse(input.JsonSchema);
            JToken jToken = GetJTokenFromInput(input.Json);

            IList <string> errors;
            var            isValid = jToken.IsValid(schema, out errors);

            if (!isValid && options.ThrowOnInvalidJson)
            {
                throw new JsonException($"Json is not valid. {string.Join("; ", errors)}");
            }
            return(new ValidateResult()
            {
                IsValid = isValid, Errors = errors
            });
        }
コード例 #2
0
        /// <summary>
        /// Validate your json with Json.NET Schema. See http://www.newtonsoft.com/jsonschema and https://github.com/FrendsPlatform/Frends.Json
        /// </summary>
        /// <returns>Object { bool IsValid, string Error }</returns>
        public static ValidateResult Validate([PropertyTab] ValidateInput input, [PropertyTab] ValidateOption options)
        {
            IList <string> errors;

            var schema = JSchema.Parse(input.JsonSchema);

            JToken jToken = null;

            try
            {
                jToken = GetJTokenFromInput(input.Json);
            }
            catch (System.Exception exception)
            {
                if (options.ThrowOnInvalidJson)
                {
                    throw;  // re-throw
                }

                errors = new List <string>();
                while (exception != null)
                {
                    errors.Add(exception.Message);
                    exception = exception.InnerException;
                }

                return(new ValidateResult()
                {
                    IsValid = false, Errors = errors
                });
            }

            var isValid = jToken.IsValid(schema, out errors);

            if (!isValid && options.ThrowOnInvalidJson)
            {
                throw new JsonException($"Json is not valid. {string.Join("; ", errors)}");
            }
            return(new ValidateResult()
            {
                IsValid = isValid, Errors = errors
            });
        }