예제 #1
0
        public void Issue49_RequiredAndAllOfInSingleSchema()
        {
            var fileName = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, @"Files\issue49.json").AdjustForOS();
            var expected = new JsonSchema()
                           .Title("JSON schema for Something")
                           .Schema("http://json-schema.org/draft-04/schema#")
                           .Definition("something", new JsonSchema()
                                       .Type(JsonSchemaType.Object)
                                       .Required("name")
                                       .AllOf(new JsonSchema()
                                              .Property("name", new JsonSchema().Type(JsonSchemaType.String)))
                                       )
                           .Type(JsonSchemaType.Array)
                           .Description("An array of somethings.")
                           .Items(new JsonSchema().Ref("#/definitions/something"));

            var schema = JsonSchemaRegistry.Get(fileName);

            Assert.AreEqual(expected, schema);

            var schemaJson   = schema.ToJson(_serializer);
            var expectedJson = expected.ToJson(_serializer);

            Console.WriteLine(schemaJson);
            Assert.AreEqual(expectedJson, schemaJson);
        }
예제 #2
0
        public void Issue50_MultipleSchemaInSubFoldersShouldReferenceRelatively()
        {
            string path   = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, @"Files\Issue50A.json").AdjustForOS();
            var    schema = JsonSchemaRegistry.Get(path);
            var    json   = new JsonObject
            {
                ["text"] = "something",
                ["refa"] = new JsonObject
                {
                    ["text"] = "something else",
                    ["refb"] = new JsonObject
                    {
                        ["refd"] = new JsonObject
                        {
                            ["refe"] = new JsonObject {
                                ["test"] = "test"
                            },
                            ["text"] = "test"
                        }
                    }
                }
            };
            var results = schema.Validate(json);

            results.AssertValid();
        }
예제 #3
0
        public void Issue173_ReferencedSchemaInParentFolder()
        {
            // this links to the commit after the one that submitted the schema files.
            // otherwise we have a paradox of trying to know the commit hash before the commit is created.
            var baseUri = "https://raw.githubusercontent.com/gregsdennis/Manatee.Json/c264db5c75478e0a33269baba7813901829f8244/Manatee.Json.Tests/Files/";

            var schema = (JsonSchema07)JsonSchemaRegistry.Get($"{baseUri}Issue173/BaseSchema.json");

            var invalid = new JsonObject
            {
                ["localProp"] = new JsonArray {
                    150, "hello", 6
                }
            };
            var valid = new JsonObject
            {
                ["localProp"] = new JsonArray {
                    1, 2, 3, 4
                }
            };

            var result = schema.Validate(invalid);

            result.AssertInvalid();

            result = schema.Validate(valid);
            result.AssertValid();
        }
예제 #4
0
        public void Issue58_UriReferenceSchemaTest()
        {
            const string coreSchemaUri  = "http://example.org/Issue58RefCore.json";
            const string childSchemaUri = "http://example.org/Issue58RefChild.json";

            var coreSchemaPath  = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, @"Files\Issue58RefCore.json").AdjustForOS();
            var childSchemaPath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, @"Files\Issue58RefChild.json").AdjustForOS();

            string coreSchemaText;
            string childSchemaText;

            using (TextReader reader = File.OpenText(coreSchemaPath))
            {
                coreSchemaText = reader.ReadToEnd();
            }

            using (TextReader reader = File.OpenText(childSchemaPath))
            {
                childSchemaText = reader.ReadToEnd();
            }

            var requestedUris = new List <string>();

            try
            {
                JsonSchemaOptions.Download = uri =>
                {
                    requestedUris.Add(uri);
                    switch (uri)
                    {
                    case coreSchemaUri:
                        return(coreSchemaText);

                    case childSchemaUri:
                        return(childSchemaText);
                    }
                    return(coreSchemaText);
                };
                var schema = JsonSchemaRegistry.Get(childSchemaUri);

                var testJson = new JsonObject();
                testJson["myProperty"] = "http://example.org/";

                //Console.WriteLine(testJson);
                //Console.WriteLine(schema.ToJson(null).GetIndentedString());

                var result = schema.Validate(testJson);

                result.AssertValid();
                Assert.AreEqual(requestedUris[0], childSchemaUri);
                Assert.AreEqual(requestedUris[1], coreSchemaUri);
            }
            finally
            {
                JsonSchemaOptions.Download = null;
            }
        }
예제 #5
0
        public void Issue45a_Utf8SupportInReferenceSchemaEnums()
        {
            var fileName = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, @"Files\baseSchema.json").AdjustForOS();

            const string jsonString = "{\"prop1\": \"ændring\", \"prop2\": {\"prop3\": \"ændring\"}}";
            var          schema     = JsonSchemaRegistry.Get(fileName);
            var          json       = JsonValue.Parse(jsonString);

            var result = schema.Validate(json);

            result.AssertValid();
        }
예제 #6
0
        private static IJsonSchema _GetFileSchema(SchemaAttribute attribute)
        {
            var uri = attribute.Source;

            if (!Uri.TryCreate(uri, UriKind.Absolute, out _))
            {
                uri = System.IO.Path.Combine(Directory.GetCurrentDirectory(), uri);
            }
            var schema = JsonSchemaRegistry.Get(uri);

            return(schema);
        }
예제 #7
0
        public void Issue49_RequiredAndAllOfInSingleSchema()
        {
            var fileName = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, @"Files\issue49.json").AdjustForOS();
            var expected = new JsonSchema04
            {
                Title       = "JSON schema for Something",
                Schema      = "http://json-schema.org/draft-04/schema#",
                Definitions = new Dictionary <string, IJsonSchema>
                {
                    ["something"] = new JsonSchema04
                    {
                        Type  = JsonSchemaType.Object,
                        AllOf = new[]
                        {
                            new JsonSchema04
                            {
                                Properties = new Dictionary <string, IJsonSchema>
                                {
                                    ["name"] = new JsonSchema04 {
                                        Type = JsonSchemaType.String
                                    }
                                }
                            }
                        },
                        Required = new List <string> {
                            "name"
                        }
                    }
                },
                Type        = JsonSchemaType.Array,
                Description = "An array of somethings.",
                Items       = new JsonSchemaReference("#/definitions/something", typeof(JsonSchema04))
            };

            var schema = JsonSchemaRegistry.Get(fileName);

            Assert.AreEqual(expected, schema);

            var schemaJson   = schema.ToJson(null);
            var expectedJson = expected.ToJson(null);

            Console.WriteLine(schemaJson);
            Assert.AreEqual(expectedJson, schemaJson);
        }
예제 #8
0
        public void Issue173_ReferencedSchemaInParentFolder()
        {
            try
            {
                // this links to the commit after the one that submitted the schema files.
                // otherwise we have a paradox of trying to know the commit hash before the commit is created.
                var baseUri = "https://raw.githubusercontent.com/gregsdennis/Manatee.Json/c264db5c75478e0a33269baba7813901829f8244/Manatee.Json.Tests/Files/";

                var schema = JsonSchemaRegistry.Get($"{baseUri}Issue173/BaseSchema.json");

                var invalid = new JsonObject
                {
                    ["localProp"] = new JsonArray {
                        150, "hello", 6
                    }
                };
                var valid = new JsonObject
                {
                    ["localProp"] = new JsonArray {
                        1, 2, 3, 4
                    }
                };

                var result = schema.Validate(invalid);
                result.AssertInvalid();

                result = schema.Validate(valid);
                result.AssertValid();
            }
            catch (WebException)
            {
                Assert.Inconclusive();
            }
            catch (AggregateException e)
            {
                if (e.InnerExceptions.OfType <WebException>().Any() ||
                    e.InnerExceptions.OfType <HttpRequestException>().Any())
                {
                    Assert.Inconclusive();
                }
                throw;
            }
        }
        public static OpenApiDocument ManateeRegisterJsonSchema(string _specLocation)
        {
            var exFiles = "";

            foreach (var file in Directory.GetFiles(_specLocation))
            {
                //var textFromOpenApiSchemaFile = File.ReadAllText(file);
                try
                {
                    JsonSchemaRegistry.Register(JsonSchemaRegistry.Get(file));
                }
                catch (Exception ex)
                {
                    exFiles += file + "\n\n";
                    exFiles += ex.ToString() + "\n\n";
                }
            }

            return(null);
        }
예제 #10
0
        public void Issue45b_Utf8SupportInReferenceSchemaEnums()
        {
            var fileName = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, @"Files\baseSchema.json").AdjustForOS();

            const string jsonString = "{\"prop1\": \"ændring\", \"prop2\": {\"prop3\": \"ændring\"}}";
            var          schema     = JsonSchemaRegistry.Get(fileName);
            var          json       = JsonValue.Parse(jsonString);

            var result = schema.Validate(json);

            foreach (var error in result.NestedResults)
            {
                Console.WriteLine(error);
            }
            Console.WriteLine(schema.ToJson(_serializer));
            var refSchema = schema.Properties()["prop2"].RefResolved();

            Console.WriteLine(refSchema.ToJson(_serializer));
            Console.WriteLine(json);

            result.AssertValid();
        }