public void Simple()
        {
            const string json = @"
{
  ""description"": ""A person"",
  ""type"": ""object"",
  ""properties"":
  {
    ""name"": {""type"":""string""},
    ""hobbies"": {
      ""type"": ""array"",
      ""items"": {""type"":""string""}
    }
  }
}
";

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

            Assert.Equal("A person", schema.Description);
            Assert.Equal(JsonSchemaType.Object, schema.Type);

            Assert.Equal(2, schema.Properties.Count);

            Assert.Equal(JsonSchemaType.String, schema.Properties["name"].Type);
            Assert.Equal(JsonSchemaType.Array, schema.Properties["hobbies"].Type);
            Assert.Equal(JsonSchemaType.String, schema.Properties["hobbies"].Items[0].Type);
        }
        public void MultipleTypes()
        {
            const string json = @"{
  ""description"":""Age"",
  ""type"":[""string"", ""integer""]
}";

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

            Assert.Equal("Age", schema.Description);
            Assert.Equal(JsonSchemaType.String | JsonSchemaType.Integer, schema.Type);
        }
        public void ExclusiveMinimumExclusiveMaximum()
        {
            const string json = @"{
  ""exclusiveMinimum"":true,
  ""exclusiveMaximum"":true
}";

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

            Assert.Equal(true, schema.ExclusiveMinimum);
            Assert.Equal(true, schema.ExclusiveMaximum);
        }
        public void MultipleItems()
        {
            const string json = @"{
  ""description"":""MultipleItems"",
  ""type"":""array"",
  ""items"": [{""type"":""string""},{""type"":""array""}]
}";

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

            Assert.Equal("MultipleItems", schema.Description);
            Assert.Equal(JsonSchemaType.String, schema.Items[0].Type);
            Assert.Equal(JsonSchemaType.Array, schema.Items[1].Type);
        }
        public void ReferencesIndexNotInteger()
        {
            const string json = @"{
            ""array"": [{""type"": ""integer""},{""prop"":{""type"": ""object""}}],
            ""properties"": {
                ""array"": {""$ref"": ""#/array/0""},
                ""arrayprop"": {""$ref"": ""#/array/one""}
            }
        }";

            AssertException.Throws<JsonException>(() =>
            {
                var builder = new JsonSchemaBuilder(new JsonSchemaResolver());
                builder.Read(new JsonTextReader(new StringReader(json)));
            }, "Could not resolve schema reference '#/array/one'.");
        }
        public void EscapedReferences()
        {
            const 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""}
            }
        }";

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

            Assert.Equal(JsonSchemaType.Integer, schema.Properties["tilda"].Type);
            Assert.Equal(JsonSchemaType.Object, schema.Properties["slash"].Type);
            Assert.Equal(JsonSchemaType.Array, schema.Properties["percent"].Type);
        }
        public void Location()
        {
            const string json = @"{
  ""properties"":{
    ""foo"":{
      ""type"":""array"",
      ""items"":[
        {
          ""type"":""integer""
        },
        {
          ""properties"":{
            ""foo"":{
              ""type"":""integer""
            }
          }
        }
      ]
    }
  }
}";

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

            Assert.Equal("#", schema.Location);
            Assert.Equal("#/properties/foo", schema.Properties["foo"].Location);
            Assert.Equal("#/properties/foo/items/1/properties/foo", schema.Properties["foo"].Items[1].Properties["foo"].Location);
        }
        public void AdditionalItems()
        {
            const string json = @"{
    ""items"": [],
    ""additionalItems"": {""type"": ""integer""}
}";

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

            Assert.NotNull(schema.AdditionalItems);
            Assert.Equal(JsonSchemaType.Integer, schema.AdditionalItems.Type);
            Assert.Equal(true, schema.AllowAdditionalItems);
        }
        public void UnresolvedReference()
        {
            AssertException.Throws<Exception>(() =>
            {
                const string json = @"{
  ""id"":""CircularReferenceArray"",
  ""description"":""CircularReference"",
  ""type"":[""array""],
  ""items"":{""$ref"":""MyUnresolvedReference""}
}";

                var builder = new JsonSchemaBuilder(new JsonSchemaResolver());
                JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
            }, @"Could not resolve schema reference 'MyUnresolvedReference'.");
        }
        public void Format()
        {
            const string json = @"{
  ""description"":""Format"",
  ""format"":""testformat""
}";

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

            Assert.Equal("Format", schema.Description);
            Assert.Equal("testformat", schema.Format);
        }
        public void Pattern()
        {
            const string json = @"{
  ""description"":""Pattern"",
  ""pattern"":""testpattern""
}";

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

            Assert.Equal("Pattern", schema.Description);
            Assert.Equal("testpattern", schema.Pattern);
        }
        public void Title()
        {
            const string json = @"{
  ""description"":""Title"",
  ""title"":""testtitle""
}";

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

            Assert.Equal("Title", schema.Description);
            Assert.Equal("testtitle", schema.Title);
        }
        public void Id()
        {
            const string json = @"{
  ""description"":""Id"",
  ""id"":""testid""
}";

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

            Assert.Equal("Id", schema.Description);
            Assert.Equal("testid", schema.Id);
        }
        public void Hidden()
        {
            const string json = @"{
  ""description"":""Hidden"",
  ""hidden"":true
}";

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

            Assert.Equal("Hidden", schema.Description);
            Assert.Equal(true, schema.Hidden);
        }
        public void ReadOnly()
        {
            const string json = @"{
  ""description"":""ReadOnly"",
  ""readonly"":true
}";

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

            Assert.Equal("ReadOnly", schema.Description);
            Assert.Equal(true, schema.ReadOnly);
        }
        public void CircularReference()
        {
            const string json = @"{
  ""id"":""CircularReferenceArray"",
  ""description"":""CircularReference"",
  ""type"":[""array""],
  ""items"":{""$ref"":""CircularReferenceArray""}
}";

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

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

            Assert.Equal(schema, schema.Items[0]);
        }
        public void Requires()
        {
            const string json = @"{
  ""description"":""Requires"",
  ""requires"":""PurpleMonkeyDishwasher""
}";

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

            Assert.Equal("Requires", schema.Description);
            Assert.Equal("PurpleMonkeyDishwasher", schema.Requires);
        }
        public void PatternProperties()
        {
            const string json = @"{
  ""patternProperties"": {
    ""[abc]"": { ""id"":""Blah"" }
  }
}";

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

            Assert.NotNull(schema.PatternProperties);
            Assert.Equal(1, schema.PatternProperties.Count);
            Assert.Equal("Blah", schema.PatternProperties["[abc]"].Id);
        }
        public void MinimumMaximum()
        {
            const string json = @"{
  ""description"":""MinimumMaximum"",
  ""minimum"":1.1,
  ""maximum"":1.2,
  ""minItems"":1,
  ""maxItems"":2,
  ""minLength"":5,
  ""maxLength"":50,
  ""divisibleBy"":3,
}";

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

            Assert.Equal("MinimumMaximum", schema.Description);
            Assert.Equal(1.1, schema.Minimum);
            Assert.Equal(1.2, schema.Maximum);
            Assert.Equal(1, schema.MinimumItems);
            Assert.Equal(2, schema.MaximumItems);
            Assert.Equal(5, schema.MinimumLength);
            Assert.Equal(50, schema.MaximumLength);
            Assert.Equal(3, schema.DivisibleBy);
        }
        public void AllowAdditionalItems()
        {
            const string json = @"{
    ""items"": {},
    ""additionalItems"": false
}";

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

            Assert.Null(schema.AdditionalItems);
            Assert.Equal(false, schema.AllowAdditionalItems);
        }
        public void DisallowSingleType()
        {
            const string json = @"{
  ""description"":""DisallowSingleType"",
  ""disallow"":""string""
}";

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

            Assert.Equal("DisallowSingleType", schema.Description);
            Assert.Equal(JsonSchemaType.String, schema.Disallow);
        }
        public void ReferenceNonStandardLocation()
        {
            const string json = @"{
  ""properties"": {
    ""bar"": {""$ref"": ""#/common/foo""},
    ""foo"": {""$ref"": ""#/common/foo""}
  },
  ""common"": {
    ""foo"": {""type"": ""integer""}
  }
}";

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

            Assert.Equal(schema.Properties["foo"], schema.Properties["bar"]);
        }
        public void DisallowMultipleTypes()
        {
            const string json = @"{
  ""description"":""DisallowMultipleTypes"",
  ""disallow"":[""string"",""number""]
}";

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

            Assert.Equal("DisallowMultipleTypes", schema.Description);
            Assert.Equal(JsonSchemaType.String | JsonSchemaType.Float, schema.Disallow);
        }
        public void ReferencesArray()
        {
            const string json = @"{
            ""array"": [{""type"": ""integer""},{""prop"":{""type"": ""object""}}],
            ""properties"": {
                ""array"": {""$ref"": ""#/array/0""},
                ""arrayprop"": {""$ref"": ""#/array/1/prop""}
            }
        }";

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

            Assert.Equal(JsonSchemaType.Integer, schema.Properties["array"].Type);
            Assert.Equal(JsonSchemaType.Object, schema.Properties["arrayprop"].Type);
        }
        public void DefaultPrimitiveType()
        {
            const string json = @"{
  ""description"":""DefaultPrimitiveType"",
  ""default"":1.1
}";

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

            Assert.Equal("DefaultPrimitiveType", schema.Description);
            Assert.Equal(1.1, (double)schema.Default);
        }
        public void DefaultComplexType()
        {
            const string json = @"{
  ""description"":""DefaultComplexType"",
  ""default"":{""pie"":true}
}";

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

            Assert.Equal("DefaultComplexType", schema.Description);
            Assert.True(JToken.DeepEquals(JObject.Parse(@"{""pie"":true}"), schema.Default));
        }
        public void Enum()
        {
            const string json = @"{
  ""description"":""Type"",
  ""type"":[""string"",""array""],
  ""enum"":[""string"",""object"",""array"",""boolean"",""number"",""integer"",""null"",""any""]
}";

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

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

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

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

            Assert.Equal("AdditionalProperties", schema.Description);
            Assert.Equal(JsonSchemaType.Object | JsonSchemaType.Boolean, schema.AdditionalProperties.Type);
        }
Beispiel #29
0
        /// <summary>
        /// Reads a <see cref="JsonSchema"/> from the specified <see cref="JsonReader"/>.
        /// </summary>
        /// <param name="reader">The <see cref="JsonReader"/> containing the JSON Schema to read.</param>
        /// <param name="resolver">The <see cref="JsonSchemaResolver"/> to use when resolving schema references.</param>
        /// <returns>The <see cref="JsonSchema"/> object representing the JSON Schema.</returns>
        public static JsonSchema Read(JsonReader reader, JsonSchemaResolver resolver)
        {
            ValidationUtils.ArgumentNotNull(reader, "reader");
            ValidationUtils.ArgumentNotNull(resolver, "resolver");

            JsonSchemaBuilder builder = new JsonSchemaBuilder(resolver);
            return builder.Read(reader);
        }