public void ValidateReturnsValidOnUnknownFormat()
        {
            JsonSchemaOptions.OutputFormat = SchemaValidationOutputFormat.Hierarchy;
            var schema   = new JsonSchema().Type(JsonSchemaType.String).Format(StringFormat.GetFormat("Int32"));
            var json     = (JsonValue)"32";
            var expected = new SchemaValidationResults
            {
                IsValid          = true,
                RelativeLocation = JsonPointer.Parse("#"),
                InstanceLocation = JsonPointer.Parse("#"),
                NestedResults    = new List <SchemaValidationResults>
                {
                    new SchemaValidationResults
                    {
                        IsValid          = true,
                        RelativeLocation = JsonPointer.Parse("#/type"),
                        InstanceLocation = JsonPointer.Parse("#"),
                        Keyword          = "type"
                    },
                    new SchemaValidationResults
                    {
                        IsValid          = true,
                        RelativeLocation = JsonPointer.Parse("#/format"),
                        InstanceLocation = JsonPointer.Parse("#"),
                        Keyword          = "format",
                        AnnotationValue  = "Int32"
                    }
                }
            };

            var results = schema.Validate(json);

            results.AssertValid(expected);
        }
        public void ValidateReturnsInvalidOnUnknownFormat()
        {
            JsonSchemaOptions.AllowUnknownFormats = false;
            JsonSchemaOptions.OutputFormat        = SchemaValidationOutputFormat.Hierarchy;
            var schema   = new JsonSchema().Type(JsonSchemaType.String).Format(StringFormat.GetFormat("Int32"));
            var json     = (JsonValue)"32";
            var expected = new SchemaValidationResults
            {
                IsValid          = false,
                RelativeLocation = JsonPointer.Parse("#/format"),
                InstanceLocation = JsonPointer.Parse("#"),
                Keyword          = "format",
                AdditionalInfo   = new JsonObject
                {
                    ["format"]        = "Int32",
                    ["isKnownFormat"] = false
                }
            };

            try
            {
                var results = schema.Validate(json);

                results.AssertInvalid(expected);
            }
            finally
            {
                JsonSchemaOptions.AllowUnknownFormats = true;
            }
        }
        public void ValidateReturnsInvalidOnUnknownFormat()
        {
            var schema   = new JsonSchema().Type(JsonSchemaType.String).Format("Int32");
            var json     = (JsonValue)"32";
            var expected = new SchemaValidationResults
            {
                IsValid          = false,
                RelativeLocation = JsonPointer.Parse("#/format"),
                InstanceLocation = JsonPointer.Parse("#"),
                Keyword          = "format",
                ErrorMessage     = "\"32\" is not in an acceptable \"Int32\" format.",
                AnnotationValue  = "Int32",
                AdditionalInfo   = new JsonObject
                {
                    ["actual"]        = "32",
                    ["format"]        = "Int32",
                    ["isKnownFormat"] = false
                }
            };

            var results = schema.Validate(json, new JsonSchemaOptions
            {
                OutputFormat        = SchemaValidationOutputFormat.Detailed,
                AllowUnknownFormats = false
            });

            results.AssertInvalid(expected);
        }
        public void FromJson(JsonValue json, JsonSerializer serializer)
        {
            var obj = json.Object;

            Basic    = serializer.Deserialize <SchemaValidationResults>(obj["basic"]);
            Detailed = serializer.Deserialize <SchemaValidationResults>(obj["detailed"]);
            Verbose  = serializer.Deserialize <SchemaValidationResults>(obj["verbose"]);
        }
Пример #5
0
        /// <summary>
        /// Sets the behavior configuration
        /// </summary>
        /// <param name="behaviorConfiguration"></param>
        /// <returns></returns>
        public bool SetBehaviorConfiguration(JsonValue behaviorConfiguration)
        {
            SchemaValidationResults schemaValidationResults = GetConfigurationJsonSchema().Validate(behaviorConfiguration);

            if (schemaValidationResults.IsValid)
            {
                BehaviorConfiguration = behaviorConfiguration;
            }
            return(schemaValidationResults.IsValid);
        }
        public static void AssertValid(this SchemaValidationResults results)
        {
            if (!results.Valid)
            {
                Console.WriteLine(string.Join(Environment.NewLine, results.Errors));
            }

            Assert.AreEqual(0, results.Errors.Count());
            Assert.AreEqual(true, results.Valid);
        }
Пример #7
0
        public static void AssertValid(this SchemaValidationResults results, SchemaValidationResults expected = null)
        {
            Console.WriteLine("expected:\n{0}", _serializer.Serialize(expected).GetIndentedString());
            Console.WriteLine("actual:\n{0}", _serializer.Serialize(results).GetIndentedString());

            if (expected == null)
            {
                Assert.IsTrue(results.IsValid);
            }
            else
            {
                Assert.AreEqual(expected, results);
            }
        }
        public JSONValidStruct validtor(JToken data)
        {
            JSONValidStruct _JSONValidStruct = new JSONValidStruct();

            JsonSerializer serializer = new JsonSerializer();

            JsonSchemaOptions.OutputFormat = SchemaValidationOutputFormat.List;

            JsonValue jsonvalue = JsonValue.Parse(data.ToString());

            SchemaValidationResults Results = _schema.Validate(jsonvalue);

            _JSONValidStruct.status  = Results.IsValid;
            _JSONValidStruct.message = Results.ToJson(serializer).ToString();
            _JSONValidStruct.result  = Results;

            return(_JSONValidStruct);
        }
Пример #9
0
        public void FromJson(JsonValue json, JsonSerializer serializer)
        {
            var obj = json.Object;

            Description = obj.TryGetString("description");
            Data        = obj["data"];
            Valid       = obj["valid"].Boolean;
            var output = obj.TryGetObject("output");

            if (output != null)
            {
                Output = serializer.Deserialize <SchemaTestOutputSet>(output);
            }
            var results = obj.TryGetObject("outputGeneration")?.TryGetObject("verbose");

            if (results != null)
            {
                OutputGeneration = serializer.Deserialize <SchemaValidationResults>(results);
            }
        }
Пример #10
0
        public void Issue194_refNoIntuitiveErrorMessage()
        {
            JsonSchemaOptions.OutputFormat = SchemaValidationOutputFormat.Detailed;
            var actual = new JsonSchema()
                         .Id("http://myschema.org/test194")
                         .Ref("#/definitions/apredefinedtype")
                         .Definition("apredefinedtype", new JsonSchema()
                                     .Type(JsonSchemaType.Object)
                                     .Property("prop1", new JsonSchema().Enum("ændring", "test"))
                                     .Required("prop1"));
            var jObject = new JsonObject
            {
                ["prop11"] = "ændring",
                ["prop2"]  = new JsonObject {
                    ["prop3"] = "ændring"
                }
            };
            var expected = new SchemaValidationResults
            {
                IsValid          = false,
                RelativeLocation = JsonPointer.Parse("#/$ref/required"),
                AbsoluteLocation = new Uri("http://myschema.org/test194#/definitions/apredefinedtype/required"),
                InstanceLocation = JsonPointer.Parse("#"),
                Keyword          = "required",
                ErrorMessage     = "The properties [\"prop1\"] are required.",
                AdditionalInfo   = new JsonObject
                {
                    ["properties"] = new JsonArray {
                        "prop1"
                    }
                }
            };

            var messages = actual.Validate(jObject);

            messages.AssertInvalid(expected);
        }
Пример #11
0
        /// <summary>
        /// Possibility to check if SagaStep data is valid before progressing
        /// </summary>
        /// <param name="sagaStep"></param>
        /// <returns></returns>
        public bool DataIsValid(ISagaStep sagaStep)
        {
            SchemaValidationResults results = CheckData(sagaStep);

            return(results.IsValid);
        }
        public void Flattens()
        {
            var initial = new SchemaValidationResults
            {
                IsValid          = false,
                RelativeLocation = JsonPointer.Parse("#"),
                InstanceLocation = JsonPointer.Parse("#"),
                NestedResults    =
                {
                    new SchemaValidationResults
                                        {
                                        IsValid          = false,
                                        Keyword          = "allOf",
                                        RelativeLocation = JsonPointer.Parse("#/allOf"),
                                        InstanceLocation = JsonPointer.Parse("#"),
                                        NestedResults    =
                                        {
                                        new SchemaValidationResults
                                        {
                                        IsValid          = false,
                                        RelativeLocation = JsonPointer.Parse("#/allOf/0"),
                                        InstanceLocation = JsonPointer.Parse("#"),
                                        NestedResults    =
                                        {
                                        new SchemaValidationResults
                                        {
                                        IsValid          = false,
                                        Keyword          = "type",
                                        RelativeLocation = JsonPointer.Parse("#/allOf/0/type"),
                                        InstanceLocation = JsonPointer.Parse("#"),
                                        AdditionalInfo   =
                                        {
                                        ["expected"] = "array",
                                        ["actual"]   = "object"
                                        }
                                        }
                                        }
                                        },
                                        new SchemaValidationResults
                                        {
                                        IsValid          = false,
                                        RelativeLocation = JsonPointer.Parse("#/allOf/1"),
                                        InstanceLocation = JsonPointer.Parse("#"),
                                        NestedResults    =
                                        {
                                        new SchemaValidationResults
                                        {
                                        IsValid          = false,
                                        Keyword          = "type",
                                        RelativeLocation = JsonPointer.Parse("#/allOf/1/type"),
                                        InstanceLocation = JsonPointer.Parse("#"),
                                        AdditionalInfo   =
                                        {
                                        ["expected"] = "number",
                                        ["actual"]   = "object"
                                        }
                                        }
                                        }
                                        }
                                        }
                                        }
                }
            };

            var expected = new SchemaValidationResults
            {
                IsValid       = false,
                NestedResults =
                {
                    new SchemaValidationResults
                        {
                        IsValid          = false,
                        Keyword          = "allOf",
                        RelativeLocation = JsonPointer.Parse("#/allOf"),
                        InstanceLocation = JsonPointer.Parse("#"),
                        },
                    new SchemaValidationResults
                        {
                        IsValid          = false,
                        Keyword          = "type",
                        RelativeLocation = JsonPointer.Parse("#/allOf/0/type"),
                        InstanceLocation = JsonPointer.Parse("#"),
                        AdditionalInfo   =
                        {
                        ["expected"] = "array",
                        ["actual"]   = "object"
                        }
                        },
                    new SchemaValidationResults
                        {
                        IsValid          = false,
                        Keyword          = "type",
                        RelativeLocation = JsonPointer.Parse("#/allOf/1/type"),
                        InstanceLocation = JsonPointer.Parse("#"),
                        AdditionalInfo   =
                        {
                        ["expected"] = "number",
                        ["actual"]   = "object"
                        }
                        }
                }
            };

            var actual = initial.Flatten();

            actual.AssertInvalid(expected);
        }
 public static void AssertInvalid(this SchemaValidationResults results)
 {
     Assert.AreNotEqual(0, results.Errors.Count());
     Assert.AreEqual(false, results.Valid);
 }
Пример #14
0
        public void ValidateReturnsValidOnAllValid()
        {
            JsonSchemaOptions.OutputFormat = SchemaValidationOutputFormat.Verbose;

            var schema = new JsonSchema()
                         .AllOf(new JsonSchema()
                                .Type(JsonSchemaType.Number)
                                .Minimum(10),
                                new JsonSchema()
                                .Type(JsonSchemaType.Number)
                                .Maximum(20));

            var json = (JsonValue)15;

            var expected = new SchemaValidationResults
            {
                IsValid          = true,
                RelativeLocation = JsonPointer.Parse("#"),
                InstanceLocation = JsonPointer.Parse("#"),
                NestedResults    =
                {
                    new SchemaValidationResults
                    {
                        IsValid          = true,
                        RelativeLocation = JsonPointer.Parse("#/allOf"),
                        InstanceLocation = JsonPointer.Parse("#"),
                        Keyword          = "allOf",
                        NestedResults    =
                        {
                            new SchemaValidationResults
                            {
                                IsValid          = true,
                                RelativeLocation = JsonPointer.Parse("#/allOf/0"),
                                InstanceLocation = JsonPointer.Parse("#"),
                                NestedResults    =
                                {
                                    new SchemaValidationResults
                                    {
                                        IsValid          = true,
                                        RelativeLocation = JsonPointer.Parse("#/allOf/0/type"),
                                        InstanceLocation = JsonPointer.Parse("#"),
                                        Keyword          = "type"
                                    },
                                    new SchemaValidationResults
                                    {
                                        IsValid          = true,
                                        RelativeLocation = JsonPointer.Parse("#/allOf/0/minimum"),
                                        InstanceLocation = JsonPointer.Parse("#"),
                                        Keyword          = "minimum"
                                    }
                                }
                            },
                            new SchemaValidationResults
                            {
                                IsValid          = true,
                                RelativeLocation = JsonPointer.Parse("#/allOf/1"),
                                InstanceLocation = JsonPointer.Parse("#"),
                                NestedResults    =
                                {
                                    new SchemaValidationResults
                                    {
                                        IsValid          = true,
                                        RelativeLocation = JsonPointer.Parse("#/allOf/1/type"),
                                        InstanceLocation = JsonPointer.Parse("#"),
                                        Keyword          = "type"
                                    },
                                    new SchemaValidationResults
                                    {
                                        IsValid          = true,
                                        RelativeLocation = JsonPointer.Parse("#/allOf/1/maximum"),
                                        InstanceLocation = JsonPointer.Parse("#"),
                                        Keyword          = "maximum"
                                    }
                                }
                            }
                        }
                    }
                }
            };

            var results = schema.Validate(json);

            results.AssertValid(expected);
        }
Пример #15
0
        public void ValidateReturnsErrorOnAnyInvalid()
        {
            JsonSchemaOptions.OutputFormat = SchemaValidationOutputFormat.Verbose;

            var schema = new JsonSchema()
                         .AllOf(new JsonSchema().Type(JsonSchemaType.Array),
                                new JsonSchema().Type(JsonSchemaType.Number));

            var json = new JsonObject();

            var expected = new SchemaValidationResults
            {
                IsValid          = false,
                RelativeLocation = JsonPointer.Parse("#"),
                InstanceLocation = JsonPointer.Parse("#"),
                NestedResults    =
                {
                    new SchemaValidationResults
                                        {
                                        IsValid          = false,
                                        Keyword          = "allOf",
                                        RelativeLocation = JsonPointer.Parse("#/allOf"),
                                        InstanceLocation = JsonPointer.Parse("#"),
                                        ErrorMessage     = "2 of 2 subschemas failed validation.",
                                        AdditionalInfo   =
                                        {
                                        ["failed"] = 2,
                                        ["total"]  = 2
                                        },
                                        NestedResults =
                                        {
                                        new SchemaValidationResults
                                        {
                                        IsValid          = false,
                                        RelativeLocation = JsonPointer.Parse("#/allOf/0"),
                                        InstanceLocation = JsonPointer.Parse("#"),
                                        NestedResults    =
                                        {
                                        new SchemaValidationResults
                                        {
                                        IsValid          = false,
                                        Keyword          = "type",
                                        RelativeLocation = JsonPointer.Parse("#/allOf/0/type"),
                                        InstanceLocation = JsonPointer.Parse("#"),
                                        ErrorMessage     = "Values of type \"object\" are not one of the allowed types \"array\".",
                                        AdditionalInfo   =
                                        {
                                        ["allowed"] = "array",
                                        ["actual"]  = "object"
                                        }
                                        }
                                        }
                                        },
                                        new SchemaValidationResults
                                        {
                                        IsValid          = false,
                                        RelativeLocation = JsonPointer.Parse("#/allOf/1"),
                                        InstanceLocation = JsonPointer.Parse("#"),
                                        NestedResults    =
                                        {
                                        new SchemaValidationResults
                                        {
                                        IsValid          = false,
                                        Keyword          = "type",
                                        RelativeLocation = JsonPointer.Parse("#/allOf/1/type"),
                                        InstanceLocation = JsonPointer.Parse("#"),
                                        ErrorMessage     = "Values of type \"object\" are not one of the allowed types \"number\".",
                                        AdditionalInfo   =
                                        {
                                        ["allowed"] = "number",
                                        ["actual"]  = "object"
                                        }
                                        }
                                        }
                                        }
                                        }
                                        }
                }
            };

            var results = schema.Validate(json);

            results.AssertInvalid(expected);
        }
Пример #16
0
        public void ValidateReturnsErrorOnAnyInvalid()
        {
            JsonSchemaOptions.OutputFormat = SchemaValidationOutputFormat.VerboseHierarchy;

            var schema = new JsonSchema()
                         .AllOf(new JsonSchema().Type(JsonSchemaType.Array),
                                new JsonSchema().Type(JsonSchemaType.Number));

            var json = new JsonObject();

            var expected = new SchemaValidationResults
            {
                IsValid          = false,
                RelativeLocation = JsonPointer.Parse("#"),
                InstanceLocation = JsonPointer.Parse("#"),
                NestedResults    =
                {
                    new SchemaValidationResults
                                        {
                                        IsValid          = false,
                                        Keyword          = "allOf",
                                        RelativeLocation = JsonPointer.Parse("#/allOf"),
                                        InstanceLocation = JsonPointer.Parse("#"),
                                        NestedResults    =
                                        {
                                        new SchemaValidationResults
                                        {
                                        IsValid          = false,
                                        RelativeLocation = JsonPointer.Parse("#/allOf/0"),
                                        InstanceLocation = JsonPointer.Parse("#"),
                                        NestedResults    =
                                        {
                                        new SchemaValidationResults
                                        {
                                        IsValid          = false,
                                        Keyword          = "type",
                                        RelativeLocation = JsonPointer.Parse("#/allOf/0/type"),
                                        InstanceLocation = JsonPointer.Parse("#"),
                                        AdditionalInfo   =
                                        {
                                        ["expected"] = "array",
                                        ["actual"]   = "object"
                                        }
                                        }
                                        }
                                        },
                                        new SchemaValidationResults
                                        {
                                        IsValid          = false,
                                        RelativeLocation = JsonPointer.Parse("#/allOf/1"),
                                        InstanceLocation = JsonPointer.Parse("#"),
                                        NestedResults    =
                                        {
                                        new SchemaValidationResults
                                        {
                                        IsValid          = false,
                                        Keyword          = "type",
                                        RelativeLocation = JsonPointer.Parse("#/allOf/1/type"),
                                        InstanceLocation = JsonPointer.Parse("#"),
                                        AdditionalInfo   =
                                        {
                                        ["expected"] = "number",
                                        ["actual"]   = "object"
                                        }
                                        }
                                        }
                                        }
                                        }
                                        }
                }
            };

            var results = schema.Validate(json);

            results.AssertInvalid(expected);
        }