コード例 #1
0
        public void Online(IJsonSchema schema)
        {
            try
            {
                // TODO: Catch web exceptions and assert inconclusive.
                var localSchemaJson = schema.ToJson(null);

                var onlineSchemaText = JsonSchemaOptions.Download(schema.Id);
                var onlineSchemaJson = JsonValue.Parse(onlineSchemaText);
                var onlineSchema     = JsonSchemaFactory.FromJson(onlineSchemaJson);

                var localValidation  = schema.Validate(onlineSchemaJson);
                var onlineValidation = onlineSchema.Validate(localSchemaJson);

                Assert.AreEqual(onlineSchema, schema);

                onlineValidation.AssertValid();
                localValidation.AssertValid();

                Assert.AreEqual(onlineSchemaJson, localSchemaJson);
            }
            catch (WebException)
            {
                Assert.Inconclusive();
            }
            catch (AggregateException e)
            {
                if (e.InnerExceptions.OfType <WebException>().Any())
                {
                    Assert.Inconclusive();
                }
                throw;
            }
        }
コード例 #2
0
ファイル: ClientTest.cs プロジェクト: uyghurjava/Manatee.Json
        public void DeserializeSchema_TypePropertyIsArray_Issue14()
        {
            JsonSchemaFactory.SetDefaultSchemaVersion <JsonSchema04>();

            var text     = "{\"type\":\"object\",\"properties\":{\"home\":{\"type\":[\"object\",\"null\"],\"properties\":{\"street\":{\"type\":\"string\"}}}}}";
            var json     = JsonValue.Parse(text);
            var expected = new JsonSchema04
            {
                Type       = JsonSchemaType.Object,
                Properties = new Dictionary <string, IJsonSchema>
                {
                    ["home"] = new JsonSchema04
                    {
                        Type       = JsonSchemaType.Object | JsonSchemaType.Null,
                        Properties = new Dictionary <string, IJsonSchema>
                        {
                            ["street"] = new JsonSchema04 {
                                Type = JsonSchemaType.String
                            }
                        }
                    }
                }
            };

            var actual = JsonSchemaFactory.FromJson(json);

            Assert.AreEqual(expected, actual);
        }
コード例 #3
0
        public void QuasiSchema7IfFalseElseFalse()
        {
            JsonSchemaFactory.RegisterExtendedSchema(QuasiSchema7.MetaQuasi.Id, () => new QuasiSchema7(), QuasiSchema7.MetaQuasi);
            JsonSchemaPropertyValidatorFactory.RegisterValidator(new QuasiSchema7IfThenElseValidator());

            var schema = new QuasiSchema7
            {
                Schema = QuasiSchema7.MetaQuasi.Id,
                If     = new QuasiSchema7 {
                    Type = JsonSchemaType.Integer
                },
                Then = JsonSchema06.False,
                Else = new QuasiSchema7 {
                    Type = JsonSchemaType.String
                }
            };

            JsonValue json = false;

            var results = schema.Validate(json);

            results.AssertInvalid();

            json = QuasiSchema7.MetaQuasi.ToJson(new JsonSerializer());

            Console.WriteLine(json.GetIndentedString());
        }
コード例 #4
0
        public ActionParameterSchemas(ApplicationModel applicationModel, HypermediaExtensionsOptions hypermediaOptions)
        {
            var actionParameterTypes = applicationModel.ActionParameterTypes.Values.Select(_ => _.Type);

            schemaByTypeName = actionParameterTypes.ToImmutableDictionary(
                t => t.BeautifulName(),
                t => JsonSchemaFactory.Generate(t).GetAwaiter().GetResult(),
                hypermediaOptions.CaseSensitiveParameterMatching ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase
                );
        }
コード例 #5
0
        public SchemaValidationResults Validate(IJsonSchema schema, JsonValue json, JsonValue root)
        {
            var errors      = new List <SchemaValidationError>();
            var definitions = json.Object["definitions"];

            if (definitions.Type != JsonValueType.Object)
            {
                errors.Add(new SchemaValidationError("definitions", "Property 'definitions' must contain an object."));
            }
            var metaSchema = JsonSchemaFactory.GetMetaSchema(schema.GetType());

            foreach (var value in definitions.Object.Values)
            {
                errors.AddRange(metaSchema.Validate(value).Errors);
            }
            return(new SchemaValidationResults(errors));
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: vsajip/Manatee.Json
        static void Main(string[] args)
        {
            var json = new JsonObject
            {
                ["test1"] = 1,
                ["test2"] = "hello"
            };
            var serializer = new JsonSerializer();
            var obj        = serializer.Deserialize <ITest>(json);

            System.Console.WriteLine(obj.Test1);
            System.Console.WriteLine(obj.Test2);

            var schema04json = JsonSchema04.MetaSchema.ToJson(null);
            var schema04     = JsonSchemaFactory.FromJson(schema04json);

            System.Console.ReadLine();
        }
コード例 #7
0
ファイル: ClientTest.cs プロジェクト: uyghurjava/Manatee.Json
        public void DeclaredTypeWithDeclaredEnum_Issue15()
        {
            JsonSchemaFactory.SetDefaultSchemaVersion <JsonSchema04>();

            var text     = "{\"type\":\"string\",\"enum\":[\"FeatureCollection\"]}";
            var json     = JsonValue.Parse(text);
            var expected = new JsonSchema04
            {
                Type = JsonSchemaType.String,
                Enum = new List <EnumSchemaValue>
                {
                    new EnumSchemaValue("FeatureCollection")
                }
            };

            var actual = JsonSchemaFactory.FromJson(json);

            Assert.AreEqual(expected, actual);
        }
コード例 #8
0
        public SchemaValidationResults Validate(IJsonSchema schema, JsonValue json, JsonValue root)
        {
            var errors      = new List <SchemaValidationError>();
            var definitions = json.Object["definitions"];

            if (definitions.Type != JsonValueType.Object)
            {
                var message = SchemaErrorMessages.Definitions.ResolveTokens(new Dictionary <string, object>
                {
                    ["value"] = json
                });
                errors.Add(new SchemaValidationError(schema, "definitions", message));
            }

            var metaSchema = JsonSchemaFactory.GetMetaSchema(schema.GetType());

            foreach (var value in definitions.Object.Values)
            {
                errors.AddRange(metaSchema.Validate(value).Errors);
            }
            return(new SchemaValidationResults(errors));
        }
コード例 #9
0
        private static void _Run <T>(string fileName, JsonValue testJson, JsonValue schemaJson)
            where T : IJsonSchema
        {
            try
            {
                JsonSchemaFactory.SetDefaultSchemaVersion <T>();
                var test    = _serializer.Deserialize <SchemaTest>(testJson);
                var schema  = _serializer.Deserialize <IJsonSchema>(schemaJson);
                var results = schema.Validate(test.Data);

                if (test.Valid != results.Valid)
                {
                    Console.WriteLine(string.Join("\n", results.Errors));
                }
                Assert.AreEqual(test.Valid, results.Valid);
            }
            catch (Exception)
            {
                Console.WriteLine(fileName);
                throw;
            }
        }
コード例 #10
0
        public void QuasiSchema7IfTrueThenTrue()
        {
            JsonSchemaFactory.RegisterExtendedSchema(QuasiSchema7.MetaQuasi.Id, () => new QuasiSchema7(), QuasiSchema7.MetaQuasi);
            JsonSchemaPropertyValidatorFactory.RegisterValidator(new QuasiSchema7IfThenElseValidator());

            var schema = new QuasiSchema7
            {
                Schema = QuasiSchema7.MetaQuasi.Id,
                If     = new QuasiSchema7 {
                    Type = JsonSchemaType.Integer
                },
                Then = new QuasiSchema7 {
                    Minimum = 10
                },
                Else = JsonSchema06.False
            };

            JsonValue json = 15;

            var results = schema.Validate(json);

            results.AssertValid();
        }
コード例 #11
0
        public ActionResult CustomerQueryType()
        {
            var schema = JsonSchemaFactory.Generate(typeof(CustomerQuery));

            return(Ok(schema));
        }
コード例 #12
0
        public ActionResult NewAddressType()
        {
            var schema = JsonSchemaFactory.Generate(typeof(NewAddress));

            return(Ok(schema));
        }
コード例 #13
0
 protected override async Task When()
 {
     schema = await JsonSchemaFactory.GenerateSchemaAsync(typeof(MyParameter)).ConfigureAwait(false);
 }
コード例 #14
0
        public T Deserialize <T>(JsonValue json, JsonSerializer serializer)
        {
            var value = JsonSchemaFactory.FromJson(json);

            return((T)value);
        }
コード例 #15
0
 public void Run07(string fileName, JsonValue testJson, JsonValue schemaJson, string testname)
 {
     JsonSchemaFactory.SetDefaultSchemaVersion <JsonSchema07>();
     _Run <JsonSchema07>(fileName, testJson, schemaJson);
 }
コード例 #16
0
        public ActionResult CreateCustomerParametersType()
        {
            var schema = JsonSchemaFactory.Generate(typeof(CreateCustomerParameters));

            return(Ok(schema));
        }
コード例 #17
0
        public async Task <ActionResult> NewAddressType()
        {
            var schema = await JsonSchemaFactory.Generate(typeof(NewAddress)).ConfigureAwait(false);

            return(Ok(schema));
        }