public void Reference_NonStandardLocation()
        {
            string json = @"{
  ""properties"": {
    ""bar"": {""$ref"": ""#/common/foo""},
    ""foo"": {""$ref"": ""#/common/foo""}
  },
  ""common"": {
    ""foo"": {""type"": ""integer""}
  }
}";

            JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
            JsonSchema        schema  = builder.Read(new JsonTextReader(new StringReader(json)));

            Assert.AreEqual(schema.Properties["foo"], schema.Properties["bar"]);
        }
示例#2
0
        public void References_Array()
        {
            string json =
                @"{
            ""array"": [{""type"": ""integer""},{""prop"":{""type"": ""object""}}],
            ""properties"": {
                ""array"": {""$ref"": ""#/array/0""},
                ""arrayprop"": {""$ref"": ""#/array/1/prop""}
            }
        }";

            JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
            JsonSchema        schema  = builder.Read(new JsonTextReader(new StringReader(json)));

            Assert.AreEqual(JsonSchemaType.Integer, schema.Properties["array"].Type);
            Assert.AreEqual(JsonSchemaType.Object, schema.Properties["arrayprop"].Type);
        }
        public void References_IndexTooBig()
        {
            // JsonException : Could not resolve schema reference '#/array/10'.

            string json = @"{
            ""array"": [{""type"": ""integer""},{""prop"":{""type"": ""object""}}],
            ""properties"": {
                ""array"": {""$ref"": ""#/array/0""},
                ""arrayprop"": {""$ref"": ""#/array/10""}
            }
        }";

            ExceptionAssert.Throws <JsonException>(() =>
            {
                JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
                builder.Read(new JsonTextReader(new StringReader(json)));
            }, "Could not resolve schema reference '#/array/10'.");
        }
        public void CircularReference()
        {
            string json = @"{
  ""id"":""CircularReferenceArray"",
  ""description"":""CircularReference"",
  ""type"":[""array""],
  ""items"":{""$ref"":""CircularReferenceArray""}
}";

            JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
            JsonSchema        schema  = builder.Read(new JsonTextReader(new StringReader(json)));

            Assert.AreEqual("CircularReference", schema.Description);
            Assert.AreEqual("CircularReferenceArray", schema.Id);
            Assert.AreEqual(JsonSchemaType.Array, schema.Type);

            Assert.AreEqual(schema, schema.Items[0]);
        }
        public void Enum()
        {
            string json = @"{
  ""description"":""Type"",
  ""type"":[""string"",""array""],
  ""enum"":[""string"",""object"",""array"",""boolean"",""number"",""integer"",""null"",""any""]
}";

            JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
            JsonSchema        schema  = builder.Read(new JsonTextReader(new StringReader(json)));

            Assert.AreEqual("Type", schema.Description);
            Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Array, schema.Type);

            Assert.AreEqual(8, schema.Enum.Count);
            Assert.AreEqual("string", (string)schema.Enum[0]);
            Assert.AreEqual("any", (string)schema.Enum[schema.Enum.Count - 1]);
        }
示例#6
0
        public void AdditionalProperties()
        {
            string json =
                @"{
  ""description"":""AdditionalProperties"",
  ""type"":[""string"", ""integer""],
  ""additionalProperties"":{""type"":[""object"", ""boolean""]}
}";

            JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
            JsonSchema        schema  = builder.Read(new JsonTextReader(new StringReader(json)));

            Assert.AreEqual("AdditionalProperties", schema.Description);
            Assert.AreEqual(
                JsonSchemaType.Object | JsonSchemaType.Boolean,
                schema.AdditionalProperties.Type
                );
        }
示例#7
0
        public void UnresolvedReference()
        {
            ExceptionAssert.Throws <Exception>(
                () =>
            {
                string json =
                    @"{
  ""id"":""CircularReferenceArray"",
  ""description"":""CircularReference"",
  ""type"":[""array""],
  ""items"":{""$ref"":""MyUnresolvedReference""}
}";

                JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
                JsonSchema schema         = builder.Read(new JsonTextReader(new StringReader(json)));
            },
                @"Could not resolve schema reference 'MyUnresolvedReference'."
                );
        }
        public void EscapedReferences()
        {
            string json = @"{
            ""tilda~field"": {""type"": ""integer""},
            ""slash/field"": {""type"": ""object""},
            ""percent%field"": {""type"": ""array""},
            ""properties"": {
                ""tilda"": {""$ref"": ""#/tilda~0field""},
                ""slash"": {""$ref"": ""#/slash~1field""},
                ""percent"": {""$ref"": ""#/percent%25field""}
            }
        }";

            JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
            JsonSchema        schema  = builder.Read(new JsonTextReader(new StringReader(json)));

            Assert.AreEqual(JsonSchemaType.Integer, schema.Properties["tilda"].Type);
            Assert.AreEqual(JsonSchemaType.Object, schema.Properties["slash"].Type);
            Assert.AreEqual(JsonSchemaType.Array, schema.Properties["percent"].Type);
        }
        public void RequiredArray()
        {
            string            json    = @"{
  ""properties"": {
    ""firstProperty"": {
      ""type"" : ""string""
    },
    ""secondProperty"": {
      ""type"" : ""string""
    },
    ""thirdProperty"": {
      ""type"" : ""string""
    },
  },
  ""required"": [ ""firstProperty"", ""thirdProperty"" ]
}";
            JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
            JsonSchema        schema  = builder.Read(new JsonTextReader(new StringReader(json)));

            Assert.IsTrue(schema.Properties["firstProperty"].Required.Value);
            Assert.IsFalse(schema.Properties["secondProperty"].Required.HasValue);
            Assert.IsTrue(schema.Properties["thirdProperty"].Required.Value);
        }
        public void ShouldRejectInvalidRequiredArrayValues(string json)
        {
            JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());

            var exception = Assert.Throws <JsonException>(() => builder.Read(new JsonTextReader(new StringReader(json))));
        }