public void TestMethod_AGSI_Invalid_Json()
        {
            string schema_fp = @"../../../TestData/AGSI/AGSi_JSONSchema_v-0-5-0_Valid.json";
            string json_fp   = @"../../../TestData/AGSI/DC2-test_in_agsi-sample_Fail.json";

            string json   = File.ReadAllText(json_fp);
            string schema = File.ReadAllText(schema_fp);

            bool Result_expected = false;

            ArupComputeResult comparison = ValidateJson.ValidateJsonInput(json, schema);

            string report_HTML = comparison.ArupComputeReport_HTML;

            Assert.AreEqual(Result_expected, comparison.ArupComputeResultItems[0].Value);
        }
        public void TestMethod_AGSI_Invalid_Format()
        {
            string schema_fp = @"../../../TestData/AGSI/AGSi_JSONSchema_v-0-5-0_Valid.json";
            string json_fp   = @"../../../TestData/AGSI/DC2-test_in_agsi-sample_Invalid_Format.json";

            string json   = File.ReadAllText(json_fp);
            string schema = File.ReadAllText(schema_fp);

            double Errors_expected = 1;

            ArupComputeResult comparison = ValidateJson.ValidateJsonInput(json, schema);

            string report_HTML = comparison.ArupComputeReport_HTML;

            Assert.AreEqual(Errors_expected, comparison.Errors.Count);
        }
        public static ArupComputeResult AddResult(bool isValid, List <string> errors)
        {
            ArupComputeResultItem resultItem = new ArupComputeResultItem();

            resultItem.Value       = isValid;
            resultItem.Description = "Valid";
            resultItem.Symbol      = "Valid";

            ArupComputeResult result = new ArupComputeResult();

            result.Errors = new List <string>();
            foreach (string error in errors)
            {
                result.Errors.Add(error);
            }
            result.ArupComputeResultItems = new List <ArupComputeResultItem>();
            result.ArupComputeResultItems.Add(resultItem);
            result.ArupComputeReport_HTML = Report.reportStr;
            Report.reportStr = "";
            return(result);
        }
Beispiel #4
0
        public static ArupComputeResult ValidateJsonInput(
            [Input("Json", "A json input as a string", "", "")]
            string input_json,
            [Input("Schema", "A json schema input as a string", "", "")]
            string input_schema
            )
        {
            // Is the input format correct
            try
            {
                // Convert schema string to LINQ schema
                JSchema schema = JSchema.Parse(input_schema);

                // Creates a JSON object
                JObject json = JObject.Parse(input_json);

                // Validates the object against the schema
                IList <ValidationError> errorMessages;

                // Validate the json
                bool   isValid    = json.IsValid(schema, out errorMessages);
                string isValidStr = (isValid == true) ? "The json input was VALID against the schema." : "The json input was INVALID against the schema.";

                // Add items to the report
                Report.AddTitle("Json validation", Stylings.NormalBig);
                Report.AddText(isValidStr, Stylings.NormalMedium);

                List <string> errorsList = new List <string> {
                };
                // Add errors to the report
                if (isValid == false)
                {
                    List <string> errorMessagesStr = new List <string> {
                    };
                    foreach (ValidationError error in errorMessages)
                    {
                        string errorString = $"{error.Message} Line {error.LineNumber}. Position {error.LinePosition}.";
                        errorMessagesStr.Add(errorString);
                        errorsList.Add(errorString);
                    }
                    Report.AddText("Errors:", Stylings.ErrorMedium);
                    Report.AddList(errorMessagesStr, Stylings.ErrorSmall);
                }

                // Create result
                ArupComputeResult result = Result.AddResult(isValid, errorsList);
                return(result);
            }
            // Is the input format incorrect
            catch
            {
                List <string> errorsList = new List <string> {
                };
                errorsList.Add("The input json or schema was not able to be loaded.");
                Report.AddTitle("Json validation", Stylings.NormalBig);
                Report.AddText("Errors:", Stylings.ErrorMedium);
                Report.AddList(errorsList, Stylings.ErrorSmall);

                ArupComputeResult result = Result.AddResult(false, errorsList);

                return(result);
            }
        }