-Lots and lots of JSON Schema work

-JSON Schema spec tests added
-Added Path to JToken
-Fixed JTokenWriter returning null reference
-Build script logging
-Documentation
-Updated to NUnit 2.6
This commit is contained in:
James Newton-King
2012-12-31 00:53:08 +13:00
parent 86a9b7b66b
commit e3a720504e
110 changed files with 19585 additions and 5992 deletions
@@ -1068,12 +1068,12 @@ namespace Newtonsoft.Json.Tests
}
[Test]
public void NoAdditionalProperties()
public void NoAdditionalItems()
{
string schemaJson = @"{
""type"":""array"",
""items"": [{""type"":""string""},{""type"":""integer""}],
""additionalProperties"": false
""additionalItems"": false
}";
string json = @"[1, 'a', null]";
@@ -75,6 +75,23 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.IsTrue(ReferenceEquals(newValue1, newValue2));
}
[Test]
public void JObjectCount()
{
JObject o = new JObject();
dynamic d = o;
long? c1 = d.Count;
o["Count"] = 99;
long? c2 = d.Count;
Assert.AreEqual(null, c1);
Assert.AreEqual(99, c2);
}
[Test]
public void JObjectEnumerator()
{
@@ -949,5 +949,38 @@ namespace Newtonsoft.Json.Tests.Linq
JToken.Parse(json);
});
}
[Test]
public void Path()
{
JObject o =
new JObject(
new JProperty("Test1", new JArray(1, 2, 3)),
new JProperty("Test2", "Test2Value"),
new JProperty("Test3", new JObject(new JProperty("Test1", new JArray(1, new JObject(new JProperty("Test1", 1)), 3)))),
new JProperty("Test4", new JConstructor("Date", new JArray(1, 2, 3)))
);
JToken t = o.SelectToken("Test1[0]");
Assert.AreEqual("Test1[0]", t.Path);
t = o.SelectToken("Test2");
Assert.AreEqual("Test2", t.Path);
t = o.SelectToken("");
Assert.AreEqual("", t.Path);
t = o.SelectToken("Test4[0][0]");
Assert.AreEqual("Test4[0][0]", t.Path);
t = o.SelectToken("Test4[0]");
Assert.AreEqual("Test4[0]", t.Path);
t = t.DeepClone();
Assert.AreEqual("", t.Path);
t = o.SelectToken("Test3.Test1[1].Test1");
Assert.AreEqual("Test3.Test1[1].Test1", t.Path);
}
}
}
@@ -25,6 +25,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
#if !NETFX_CORE
using NUnit.Framework;
@@ -362,6 +363,39 @@ namespace Newtonsoft.Json.Tests.Linq
var o = JObject.Parse(json);
o.Property("DateTimeOffset").Value = dateTimeOffset;
}
public void ParseAndConvertDateTimeOffset()
{
var json = @"{ d: ""\/Date(0+0100)\/"" }";
using (var stringReader = new StringReader(json))
using (var jsonReader = new JsonTextReader(stringReader))
{
jsonReader.DateParseHandling = DateParseHandling.DateTimeOffset;
var obj = JObject.Load(jsonReader);
var d = (JValue)obj["d"];
Assert.IsInstanceOfType(typeof(DateTimeOffset), d.Value);
TimeSpan offset = ((DateTimeOffset)d.Value).Offset;
Assert.AreEqual(TimeSpan.FromHours(1), offset);
DateTimeOffset dateTimeOffset = (DateTimeOffset) d;
Assert.AreEqual(TimeSpan.FromHours(1), dateTimeOffset.Offset);
}
}
public void ReadDatesAsDateTimeOffsetViaJsonConvert()
{
var content = @"{""startDateTime"":""2012-07-19T14:30:00+09:30""}";
var jsonSerializerSettings = new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.IsoDateFormat, DateParseHandling = DateParseHandling.DateTimeOffset, DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind };
JObject obj = (JObject)JsonConvert.DeserializeObject(content, jsonSerializerSettings);
object startDateTime = obj["startDateTime"];
Assert.IsInstanceOfType(typeof(DateTimeOffset), startDateTime);
}
#endif
}
}
@@ -864,5 +864,24 @@ keyword such as type of business.""
JArray a = JArray.Parse(json);
Assert.AreEqual("hi!", (string)a[0]);
}
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
[Test]
public void ExceptionFromOverloadWithJValue()
{
dynamic name = new JValue("Matthew Doig");
IDictionary<string, string> users = new Dictionary<string, string>();
// unfortunatly there doesn't appear to be a way around this
ExceptionAssert.Throws<Microsoft.CSharp.RuntimeBinder.RuntimeBinderException>("The best overloaded method match for 'System.Collections.Generic.IDictionary<string,string>.Add(string, string)' has some invalid arguments",
() =>
{
users.Add("name2", name);
Assert.AreEqual(users["name2"], "Matthew Doig");
});
}
#endif
}
}
@@ -149,6 +149,7 @@
<Compile Include="Linq\DynamicTests.cs" />
<Compile Include="Linq\JPathTests.cs" />
<Compile Include="Linq\JRawTests.cs" />
<Compile Include="Schema\JsonSchemaSpecTests.cs" />
<Compile Include="Serialization\ConstructorHandlingTests.cs" />
<Compile Include="Serialization\ContractResolverTests.cs" />
<Compile Include="Serialization\DefaultValueHandlingTests.cs" />
@@ -309,6 +310,69 @@
<LastGenOutput>LinqToSqlClasses.designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</None>
<None Include="Schema\Specs\additionalItems.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Schema\Specs\additionalProperties.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Schema\Specs\dependencies.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Schema\Specs\disallow.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Schema\Specs\divisibleBy.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Schema\Specs\enum.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Schema\Specs\extends.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Schema\Specs\items.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Schema\Specs\maximum.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Schema\Specs\maxItems.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Schema\Specs\maxLength.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Schema\Specs\minimum.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Schema\Specs\minItems.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Schema\Specs\minLength.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Schema\Specs\pattern.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Schema\Specs\patternProperties.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Schema\Specs\properties.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Schema\Specs\ref.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Schema\Specs\required.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Schema\Specs\type.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Schema\Specs\uniqueItems.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Service Include="{3259AA49-8AA1-44D3-9025-A0B520596A8C}" />
@@ -347,6 +411,9 @@
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<Folder Include="Schema\Specs\optional\" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
@@ -76,5 +76,5 @@ using System.Security;
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")]
#if !PocketPC
[assembly: AssemblyFileVersion("4.5.11.15520")]
[assembly: AssemblyFileVersion("4.5.11.15631")]
#endif
@@ -300,14 +300,156 @@ namespace Newtonsoft.Json.Tests.Schema
public void DivisibleBy_Int()
{
ExceptionAssert.Throws<JsonSchemaException>("Integer 10 is not evenly divisible by 3.",
() =>
{
JsonSchema schema = new JsonSchema();
schema.DivisibleBy = 3;
() =>
{
JsonSchema schema = new JsonSchema();
schema.DivisibleBy = 3;
JValue v = new JValue(10);
v.Validate(schema);
});
JValue v = new JValue(10);
v.Validate(schema);
});
}
[Test]
public void UniqueItems_SimpleUnique()
{
JsonSchema schema = new JsonSchema();
schema.UniqueItems = true;
JArray a = new JArray(1, 2, 3);
Assert.IsTrue(a.IsValid(schema));
}
[Test]
public void UniqueItems_SimpleDuplicate()
{
JsonSchema schema = new JsonSchema();
schema.UniqueItems = true;
JArray a = new JArray(1, 2, 3, 2, 2);
IList<string> errorMessages;
Assert.IsFalse(a.IsValid(schema, out errorMessages));
Assert.AreEqual(2, errorMessages.Count);
Assert.AreEqual("Non-unique array item at index 3.", errorMessages[0]);
Assert.AreEqual("Non-unique array item at index 4.", errorMessages[1]);
}
[Test]
public void UniqueItems_ComplexDuplicate()
{
JsonSchema schema = new JsonSchema();
schema.UniqueItems = true;
JArray a = new JArray(1, new JObject(new JProperty("value", "value!")), 3, 2, new JObject(new JProperty("value", "value!")), 4, 2, new JObject(new JProperty("value", "value!")));
IList<string> errorMessages;
Assert.IsFalse(a.IsValid(schema, out errorMessages));
Assert.AreEqual(3, errorMessages.Count);
Assert.AreEqual("Non-unique array item at index 4.", errorMessages[0]);
Assert.AreEqual("Non-unique array item at index 6.", errorMessages[1]);
Assert.AreEqual("Non-unique array item at index 7.", errorMessages[2]);
}
[Test]
public void UniqueItems_NestedDuplicate()
{
JsonSchema schema = new JsonSchema();
schema.UniqueItems = true;
schema.Items = new List<JsonSchema>
{
new JsonSchema
{
UniqueItems = true
}
};
schema.PositionalItemsValidation = false;
JArray a = new JArray(
new JArray(1, 2),
new JArray(1, 1),
new JArray(3, 4),
new JArray(1, 2),
new JArray(1, 1)
);
IList<string> errorMessages;
Assert.IsFalse(a.IsValid(schema, out errorMessages));
Assert.AreEqual(4, errorMessages.Count);
Assert.AreEqual("Non-unique array item at index 1.", errorMessages[0]);
Assert.AreEqual("Non-unique array item at index 3.", errorMessages[1]);
Assert.AreEqual("Non-unique array item at index 1.", errorMessages[2]);
Assert.AreEqual("Non-unique array item at index 4.", errorMessages[3]);
}
[Test]
public void Enum_Properties()
{
JsonSchema schema = new JsonSchema();
schema.Properties = new Dictionary<string, JsonSchema>
{
{
"bar",
new JsonSchema
{
Enum = new List<JToken>
{
new JValue(1),
new JValue(2)
}
}
}
};
JObject o = new JObject(
new JProperty("bar", 1)
);
IList<string> errorMessages;
Assert.IsTrue(o.IsValid(schema, out errorMessages));
Assert.AreEqual(0, errorMessages.Count);
o = new JObject(
new JProperty("bar", 3)
);
Assert.IsFalse(o.IsValid(schema, out errorMessages));
Assert.AreEqual(1, errorMessages.Count);
}
[Test]
public void UniqueItems_Property()
{
JsonSchema schema = new JsonSchema();
schema.Properties = new Dictionary<string, JsonSchema>
{
{
"bar",
new JsonSchema
{
UniqueItems = true
}
}
};
JObject o = new JObject(
new JProperty("bar", new JArray(1, 2, 3, 3))
);
IList<string> errorMessages;
Assert.IsFalse(o.IsValid(schema, out errorMessages));
Assert.AreEqual(1, errorMessages.Count);
}
[Test]
public void Items_Positional()
{
JsonSchema schema = new JsonSchema();
schema.Items = new List<JsonSchema>
{
new JsonSchema { Type = JsonSchemaType.Object },
new JsonSchema { Type = JsonSchemaType.Integer }
};
schema.PositionalItemsValidation = true;
JArray a = new JArray(new JObject(), 1);
IList<string> errorMessages;
Assert.IsTrue(a.IsValid(schema, out errorMessages));
Assert.AreEqual(0, errorMessages.Count);
}
}
}
@@ -59,7 +59,7 @@ namespace Newtonsoft.Json.Tests.Schema
";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.AreEqual("A person", schema.Description);
Assert.AreEqual(JsonSchemaType.Object, schema.Type);
@@ -80,7 +80,7 @@ namespace Newtonsoft.Json.Tests.Schema
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.AreEqual("Age", schema.Description);
Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Integer, schema.Type);
@@ -96,7 +96,7 @@ namespace Newtonsoft.Json.Tests.Schema
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.AreEqual("MultipleItems", schema.Description);
Assert.AreEqual(JsonSchemaType.String, schema.Items[0].Type);
@@ -113,7 +113,7 @@ namespace Newtonsoft.Json.Tests.Schema
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.AreEqual("AdditionalProperties", schema.Description);
Assert.AreEqual(JsonSchemaType.Object | JsonSchemaType.Boolean, schema.AdditionalProperties.Type);
@@ -128,7 +128,7 @@ namespace Newtonsoft.Json.Tests.Schema
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.AreEqual("Required", schema.Description);
Assert.AreEqual(true, schema.Required);
@@ -143,7 +143,7 @@ namespace Newtonsoft.Json.Tests.Schema
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.AreEqual(true, schema.ExclusiveMinimum);
Assert.AreEqual(true, schema.ExclusiveMaximum);
@@ -158,7 +158,7 @@ namespace Newtonsoft.Json.Tests.Schema
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.AreEqual("ReadOnly", schema.Description);
Assert.AreEqual(true, schema.ReadOnly);
@@ -173,7 +173,7 @@ namespace Newtonsoft.Json.Tests.Schema
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.AreEqual("Hidden", schema.Description);
Assert.AreEqual(true, schema.Hidden);
@@ -188,7 +188,7 @@ namespace Newtonsoft.Json.Tests.Schema
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.AreEqual("Id", schema.Description);
Assert.AreEqual("testid", schema.Id);
@@ -203,7 +203,7 @@ namespace Newtonsoft.Json.Tests.Schema
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.AreEqual("Title", schema.Description);
Assert.AreEqual("testtitle", schema.Title);
@@ -218,7 +218,7 @@ namespace Newtonsoft.Json.Tests.Schema
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.AreEqual("Pattern", schema.Description);
Assert.AreEqual("testpattern", schema.Pattern);
@@ -233,7 +233,7 @@ namespace Newtonsoft.Json.Tests.Schema
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.AreEqual("Format", schema.Description);
Assert.AreEqual("testformat", schema.Format);
@@ -248,45 +248,12 @@ namespace Newtonsoft.Json.Tests.Schema
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.AreEqual("Requires", schema.Description);
Assert.AreEqual("PurpleMonkeyDishwasher", schema.Requires);
}
[Test]
public void IdentitySingle()
{
string json = @"{
""description"":""Identity"",
""identity"":""PurpleMonkeyDishwasher""
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
Assert.AreEqual("Identity", schema.Description);
Assert.AreEqual(1, schema.Identity.Count);
Assert.AreEqual("PurpleMonkeyDishwasher", schema.Identity[0]);
}
[Test]
public void IdentityMultiple()
{
string json = @"{
""description"":""Identity"",
""identity"":[""PurpleMonkeyDishwasher"",""Antelope""]
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
Assert.AreEqual("Identity", schema.Description);
Assert.AreEqual(2, schema.Identity.Count);
Assert.AreEqual("PurpleMonkeyDishwasher", schema.Identity[0]);
Assert.AreEqual("Antelope", schema.Identity[1]);
}
[Test]
public void MinimumMaximum()
{
@@ -302,7 +269,7 @@ namespace Newtonsoft.Json.Tests.Schema
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.AreEqual("MinimumMaximum", schema.Description);
Assert.AreEqual(1.1, schema.Minimum);
@@ -323,7 +290,7 @@ namespace Newtonsoft.Json.Tests.Schema
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.AreEqual("DisallowSingleType", schema.Description);
Assert.AreEqual(JsonSchemaType.String, schema.Disallow);
@@ -338,7 +305,7 @@ namespace Newtonsoft.Json.Tests.Schema
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.AreEqual("DisallowMultipleTypes", schema.Description);
Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Float, schema.Disallow);
@@ -353,10 +320,10 @@ namespace Newtonsoft.Json.Tests.Schema
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.AreEqual("DefaultPrimitiveType", schema.Description);
Assert.AreEqual(1.1, (double)schema.Default);
Assert.AreEqual(1.1, (double) schema.Default);
}
[Test]
@@ -368,36 +335,12 @@ namespace Newtonsoft.Json.Tests.Schema
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.AreEqual("DefaultComplexType", schema.Description);
Assert.IsTrue(JToken.DeepEquals(JObject.Parse(@"{""pie"":true}"), schema.Default));
}
[Test]
public void Options()
{
string json = @"{
""description"":""NZ Island"",
""type"":""string"",
""options"":
[
{""value"":""NI"",""label"":""North Island""},
{""value"":""SI"",""label"":""South Island""}
]
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
Assert.AreEqual("NZ Island", schema.Description);
Assert.AreEqual(JsonSchemaType.String, schema.Type);
Assert.AreEqual(2, schema.Options.Count);
Assert.AreEqual("North Island", schema.Options[new JValue("NI")]);
Assert.AreEqual("South Island", schema.Options[new JValue("SI")]);
}
[Test]
public void Enum()
{
@@ -408,14 +351,14 @@ namespace Newtonsoft.Json.Tests.Schema
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
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]);
Assert.AreEqual("string", (string) schema.Enum[0]);
Assert.AreEqual("any", (string) schema.Enum[schema.Enum.Count - 1]);
}
[Test]
@@ -429,7 +372,7 @@ namespace Newtonsoft.Json.Tests.Schema
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.AreEqual("CircularReference", schema.Description);
Assert.AreEqual("CircularReferenceArray", schema.Id);
@@ -441,19 +384,19 @@ namespace Newtonsoft.Json.Tests.Schema
[Test]
public void UnresolvedReference()
{
ExceptionAssert.Throws<Exception>(@"Could not resolve schema reference for Id 'MyUnresolvedReference'.",
() =>
{
string json = @"{
ExceptionAssert.Throws<Exception>(@"Could not resolve schema reference 'MyUnresolvedReference'.",
() =>
{
string json = @"{
""id"":""CircularReferenceArray"",
""description"":""CircularReference"",
""type"":[""array""],
""items"":{""$ref"":""MyUnresolvedReference""}
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
});
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
});
}
[Test]
@@ -466,11 +409,161 @@ namespace Newtonsoft.Json.Tests.Schema
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Parse(new JsonTextReader(new StringReader(json)));
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.IsNotNull(schema.PatternProperties);
Assert.AreEqual(1, schema.PatternProperties.Count);
Assert.AreEqual("Blah", schema.PatternProperties["[abc]"].Id);
}
[Test]
public void AdditionalItems()
{
string json = @"{
""items"": [],
""additionalItems"": {""type"": ""integer""}
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.IsNotNull(schema.AdditionalItems);
Assert.AreEqual(JsonSchemaType.Integer, schema.AdditionalItems.Type);
Assert.AreEqual(true, schema.AllowAdditionalItems);
}
[Test]
public void DisallowAdditionalItems()
{
string json = @"{
""items"": [],
""additionalItems"": false
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.IsNull(schema.AdditionalItems);
Assert.AreEqual(false, schema.AllowAdditionalItems);
}
[Test]
public void AllowAdditionalItems()
{
string json = @"{
""items"": {},
""additionalItems"": false
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.IsNull(schema.AdditionalItems);
Assert.AreEqual(false, schema.AllowAdditionalItems);
}
[Test]
public void Location()
{
string json = @"{
""properties"":{
""foo"":{
""type"":""array"",
""items"":[
{
""type"":""integer""
},
{
""properties"":{
""foo"":{
""type"":""integer""
}
}
}
]
}
}
}
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.AreEqual("#", schema.Location);
Assert.AreEqual("#/properties/foo", schema.Properties["foo"].Location);
Assert.AreEqual("#/properties/foo/items/1/properties/foo", schema.Properties["foo"].Items[1].Properties["foo"].Location);
}
[Test]
public void Reference_BackwardsLocation()
{
string json = @"{
""properties"": {
""foo"": {""type"": ""integer""},
""bar"": {""$ref"": ""#/properties/foo""}
}
}";
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
Assert.AreEqual(schema.Properties["foo"], schema.Properties["bar"]);
}
[Test]
public void Reference_ForwardsLocation()
{
string json = @"{
""properties"": {
""bar"": {""$ref"": ""#/properties/foo""},
""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"]);
}
[Test]
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"]);
}
[Test]
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);
}
}
}
@@ -619,20 +619,6 @@ namespace Newtonsoft.Json.Tests.Schema
0,
1,
-1
],
""options"": [
{
""value"": 0,
""label"": ""No""
},
{
""value"": 1,
""label"": ""Asc""
},
{
""value"": -1,
""label"": ""Desc""
}
]
}
}
@@ -25,6 +25,7 @@
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
#if !NETFX_CORE
using NUnit.Framework;
@@ -62,7 +63,7 @@ namespace Newtonsoft.Json.Tests.Schema
}";
JsonSchema second = JsonSchema.Parse(json, resolver);
Assert.AreEqual(first, second.Extends);
Assert.AreEqual(first, second.Extends[0]);
json =
@"{
@@ -73,8 +74,8 @@ namespace Newtonsoft.Json.Tests.Schema
}";
JsonSchema third = JsonSchema.Parse(json, resolver);
Assert.AreEqual(second, third.Extends);
Assert.AreEqual(first, third.Extends.Extends);
Assert.AreEqual(second, third.Extends[0]);
Assert.AreEqual(first, third.Extends[0].Extends[0]);
StringWriter writer = new StringWriter();
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
@@ -346,43 +347,6 @@ namespace Newtonsoft.Json.Tests.Schema
}", json);
}
[Test]
public void ReadOptions()
{
JsonSchema schema = JsonSchema.Parse(@"{
""type"": ""object"",
""properties"": {
""x"": {
""type"": ""integer"",
""enum"": [
0,
1,
-1
],
""options"": [
{
""value"": 0,
""label"": ""No""
},
{
""value"": 1,
""label"": ""Asc""
},
{
""value"": -1,
""label"": ""Desc""
}
]
}
}
}");
Assert.AreEqual(schema.Properties["x"].Options.Count, 3);
Assert.AreEqual(schema.Properties["x"].Options[0], "No");
Assert.AreEqual(schema.Properties["x"].Options[1], "Asc");
Assert.AreEqual(schema.Properties["x"].Options[-1], "Desc");
}
[Test]
public void WriteTo_ExclusiveMinimum_ExclusiveMaximum()
{
@@ -425,6 +389,86 @@ namespace Newtonsoft.Json.Tests.Schema
""patternProperties"": {
""[abc]"": {}
}
}", json);
}
[Test]
public void ToString_AdditionalItems()
{
JsonSchema schema = JsonSchema.Parse(@"{
""additionalItems"": {""type"": ""integer""}
}");
string json = schema.ToString();
Assert.AreEqual(@"{
""additionalItems"": {
""type"": ""integer""
}
}", json);
}
[Test]
public void WriteTo_PositionalItemsValidation_True()
{
JsonSchema schema = new JsonSchema();
schema.PositionalItemsValidation = true;
StringWriter writer = new StringWriter();
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
jsonWriter.Formatting = Formatting.Indented;
schema.WriteTo(jsonWriter);
string json = writer.ToString();
Assert.AreEqual(@"{
""items"": []
}", json);
}
[Test]
public void WriteTo_PositionalItemsValidation_TrueWithItemsSchema()
{
JsonSchema schema = new JsonSchema();
schema.PositionalItemsValidation = true;
schema.Items = new List<JsonSchema> { new JsonSchema { Type = JsonSchemaType.String }};
StringWriter writer = new StringWriter();
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
jsonWriter.Formatting = Formatting.Indented;
schema.WriteTo(jsonWriter);
string json = writer.ToString();
Assert.AreEqual(@"{
""items"": [
{
""type"": ""string""
}
]
}", json);
}
[Test]
public void WriteTo_PositionalItemsValidation_FalseWithItemsSchema()
{
JsonSchema schema = new JsonSchema();
schema.Items = new List<JsonSchema> { new JsonSchema { Type = JsonSchemaType.String } };
StringWriter writer = new StringWriter();
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
jsonWriter.Formatting = Formatting.Indented;
schema.WriteTo(jsonWriter);
string json = writer.ToString();
Assert.AreEqual(@"{
""items"": {
""type"": ""string""
}
}", json);
}
}
@@ -0,0 +1,82 @@
[
{
"description": "additionalItems as schema",
"schema": {
"items": [],
"additionalItems": {"type": "integer"}
},
"tests": [
{
"description": "additional items match schema",
"data": [ 1, 2, 3, 4 ],
"valid": true
},
{
"description": "additional items do not match schema",
"data": [ 1, 2, 3, "foo" ],
"valid": false
}
]
},
{
"description": "items is schema, no additionalItems",
"schema": {
"items": {},
"additionalItems": false
},
"tests": [
{
"description": "all items match schema",
"data": [ 1, 2, 3, 4, 5 ],
"valid": true
}
]
},
{
"description": "array of items with no additionalItems",
"schema": {
"items": [{}, {}, {}],
"additionalItems": false
},
"tests": [
{
"description": "no additional items present",
"data": [ 1, 2, 3 ],
"valid": true
},
{
"description": "additional items are not permitted",
"data": [ 1, 2, 3, 4 ],
"valid": false
}
]
},
{
"description": "additionalItems as false without items",
"schema": {"additionalItems": false},
"tests": [
{
"description":
"items defaults to empty schema so everything is valid",
"data": [ 1, 2, 3, 4, 5 ],
"valid": true
},
{
"description": "ignores non-arrays",
"data": {"foo" : "bar"},
"valid": true
}
]
},
{
"description": "additionalItems are allowed by default",
"schema": {"items": []},
"tests": [
{
"description": "only the first items are validated",
"data": [1, "foo", false],
"valid": true
}
]
}
]
@@ -0,0 +1,63 @@
[
{
"description":
"additionalProperties being false does not allow other properties",
"schema": {
"properties": {"foo": {}, "bar": {}},
"additionalProperties": false
},
"tests": [
{
"description": "no additional properties is valid",
"data": {"foo": 1},
"valid": true
},
{
"description": "an additional property is invalid",
"data": {"foo" : 1, "bar" : 2, "quux" : "boom"},
"valid": false
},
{
"description": "ignores non-objects",
"data": [1, 2, 3],
"valid": true
}
]
},
{
"description":
"additionalProperties allows a schema which should validate",
"schema": {
"properties": {"foo": {}, "bar": {}},
"additionalProperties": {"type": "boolean"}
},
"tests": [
{
"description": "no additional properties is valid",
"data": {"foo": 1},
"valid": true
},
{
"description": "an additional valid property is valid",
"data": {"foo" : 1, "bar" : 2, "quux" : true},
"valid": true
},
{
"description": "an additional invalid property is invalid",
"data": {"foo" : 1, "bar" : 2, "quux" : 12},
"valid": false
}
]
},
{
"description": "additionalProperties are allowed by default",
"schema": {"properties": {"foo": {}, "bar": {}}},
"tests": [
{
"description": "additional properties are allowed",
"data": {"foo": 1, "bar": 2, "quux": true},
"valid": true
}
]
}
]
@@ -0,0 +1,108 @@
[
{
"description": "dependencies",
"schema": {
"dependencies": {"bar": "foo"}
},
"tests": [
{
"description": "neither",
"data": {},
"valid": true
},
{
"description": "nondependant",
"data": {"foo": 1},
"valid": true
},
{
"description": "with dependency",
"data": {"foo": 1, "bar": 2},
"valid": true
},
{
"description": "missing dependency",
"data": {"bar": 2},
"valid": false
},
{
"description": "ignores non-objects",
"data": "foo",
"valid": true
}
]
},
{
"description": "multiple dependencies",
"schema": {
"dependencies": {"quux": ["foo", "bar"]}
},
"tests": [
{
"description": "neither",
"data": {},
"valid": true
},
{
"description": "nondependants",
"data": {"foo": 1, "bar": 2},
"valid": true
},
{
"description": "with dependencies",
"data": {"foo": 1, "bar": 2, "quux": 3},
"valid": true
},
{
"description": "missing dependency",
"data": {"foo": 1, "quux": 2},
"valid": false
},
{
"description": "missing other dependency",
"data": {"bar": 1, "quux": 2},
"valid": false
},
{
"description": "missing both dependencies",
"data": {"quux": 1},
"valid": false
}
]
},
{
"description": "multiple dependencies subschema",
"schema": {
"dependencies": {
"bar": {
"properties": {
"foo": {"type": "integer"},
"bar": {"type": "integer"}
}
}
}
},
"tests": [
{
"description": "valid",
"data": {"foo": 1, "bar": 2},
"valid": true
},
{
"description": "wrong type",
"data": {"foo": "quux", "bar": 2},
"valid": false
},
{
"description": "wrong type other",
"data": {"foo": 2, "bar": "quux"},
"valid": false
},
{
"description": "wrong type both",
"data": {"foo": "quux", "bar": "quux"},
"valid": false
}
]
}
]
@@ -0,0 +1,80 @@
[
{
"description": "disallow",
"schema": {
"disallow": "integer"
},
"tests": [
{
"description": "allowed",
"data": "foo",
"valid": true
},
{
"description": "disallowed",
"data": 1,
"valid": false
}
]
},
{
"description": "multiple disallow",
"schema": {
"disallow": ["integer", "boolean"]
},
"tests": [
{
"description": "valid",
"data": "foo",
"valid": true
},
{
"description": "mismatch",
"data": 1,
"valid": false
},
{
"description": "other mismatch",
"data": true,
"valid": false
}
]
},
{
"description": "multiple disallow subschema",
"schema": {
"disallow":
["string",
{
"type": "object",
"properties": {
"foo": {
"type": "string"
}
}
}]
},
"tests": [
{
"description": "match",
"data": 1,
"valid": true
},
{
"description": "other match",
"data": {"foo": 1},
"valid": true
},
{
"description": "mismatch",
"data": "foo",
"valid": false
},
{
"description": "other mismatch",
"data": {"foo": "bar"},
"valid": false
}
]
}
]
@@ -0,0 +1,60 @@
[
{
"description": "by int",
"schema": {"divisibleBy": 2},
"tests": [
{
"description": "int by int",
"data": 10,
"valid": true
},
{
"description": "int by int fail",
"data": 7,
"valid": false
},
{
"description": "ignores non-numbers",
"data": "foo",
"valid": true
}
]
},
{
"description": "by number",
"schema": {"divisibleBy": 1.5},
"tests": [
{
"description": "zero is divisible by anything (except 0)",
"data": 0,
"valid": true
},
{
"description": "4.5 is divisible by 1.5",
"data": 4.5,
"valid": true
},
{
"description": "35 is not divisible by 1.5",
"data": 35,
"valid": false
}
]
},
{
"description": "by small number",
"schema": {"divisibleBy": 0.0001},
"tests": [
{
"description": "0.0075 is divisible by 0.0001",
"data": 0.0075,
"valid": true
},
{
"description": "0.00751 is not divisible by 0.0001",
"data": 0.00751,
"valid": false
}
]
}
]
@@ -0,0 +1,39 @@
[
{
"description": "simple enum validation",
"schema": {"enum": [1, 2, 3]},
"tests": [
{
"description": "one of the enum is valid",
"data": 1,
"valid": true
},
{
"description": "something else is invalid",
"data": 4,
"valid": false
}
]
},
{
"description": "heterogeneous enum validation",
"schema": {"enum": [6, "foo", [], true, {"foo": 12}]},
"tests": [
{
"description": "one of the enum is valid",
"data": [],
"valid": true
},
{
"description": "something else is invalid",
"data": null,
"valid": false
},
{
"description": "objects are deep compared",
"data": {"foo": false},
"valid": false
}
]
}
]
@@ -0,0 +1,94 @@
[
{
"description": "extends",
"schema": {
"properties": {"bar": {"type": "integer", "required": true}},
"extends": {
"properties": {
"foo": {"type": "string", "required": true}
}
}
},
"tests": [
{
"description": "extends",
"data": {"foo": "baz", "bar": 2},
"valid": true
},
{
"description": "mismatch extends",
"data": {"foo": "baz"},
"valid": false
},
{
"description": "mismatch extended",
"data": {"bar": 2},
"valid": false
},
{
"description": "wrong type",
"data": {"foo": "baz", "bar": "quux"},
"valid": false
}
]
},
{
"description": "multiple extends",
"schema": {
"properties": {"bar": {"type": "integer", "required": true}},
"extends" : [
{
"properties": {
"foo": {"type": "string", "required": true}
}
},
{
"properties": {
"baz": {"type": "null", "required": true}
}
}
]
},
"tests": [
{
"description": "valid",
"data": {"foo": "quux", "bar": 2, "baz": null},
"valid": true
},
{
"description": "mismatch first extends",
"data": {"bar": 2, "baz": null},
"valid": false
},
{
"description": "mismatch second extends",
"data": {"foo": "quux", "bar": 2},
"valid": false
},
{
"description": "mismatch both",
"data": {"bar": 2},
"valid": false
}
]
},
{
"description": "extends simple types",
"schema": {
"minimum": 20,
"extends": {"maximum": 30}
},
"tests": [
{
"description": "valid",
"data": 25,
"valid": true
},
{
"description": "mismatch extends",
"data": 35,
"valid": false
}
]
}
]
@@ -0,0 +1,46 @@
[
{
"description": "a schema given for items",
"schema": {
"items": {"type": "integer"}
},
"tests": [
{
"description": "valid items",
"data": [ 1, 2, 3 ],
"valid": true
},
{
"description": "wrong type of items",
"data": [1, "x"],
"valid": false
},
{
"description": "ignores non-arrays",
"data": {"foo" : "bar"},
"valid": true
}
]
},
{
"description": "an array of schemas for items",
"schema": {
"items": [
{"type": "integer"},
{"type": "string"}
]
},
"tests": [
{
"description": "correct types",
"data": [ 1, "foo" ],
"valid": true
},
{
"description": "wrong types",
"data": [ "foo", 1 ],
"valid": false
}
]
}
]
@@ -0,0 +1,28 @@
[
{
"description": "maxItems validation",
"schema": {"maxItems": 2},
"tests": [
{
"description": "shorter is valid",
"data": [1],
"valid": true
},
{
"description": "exact length is valid",
"data": [1, 2],
"valid": true
},
{
"description": "too long is invalid",
"data": [1, 2, 3],
"valid": false
},
{
"description": "ignores non-arrays",
"data": "foobar",
"valid": true
}
]
}
]
@@ -0,0 +1,28 @@
[
{
"description": "maxLength validation",
"schema": {"maxLength": 2},
"tests": [
{
"description": "shorter is valid",
"data": "f",
"valid": true
},
{
"description": "exact length is valid",
"data": "fo",
"valid": true
},
{
"description": "too long is invalid",
"data": "foo",
"valid": false
},
{
"description": "ignores non-strings",
"data": 10,
"valid": true
}
]
}
]
@@ -0,0 +1,42 @@
[
{
"description": "maximum validation",
"schema": {"maximum": 3.0},
"tests": [
{
"description": "below the maximum is valid",
"data": 2.6,
"valid": true
},
{
"description": "above the maximum is invalid",
"data": 3.5,
"valid": false
},
{
"description": "ignores non-numbers",
"data": "x",
"valid": true
}
]
},
{
"description": "exclusiveMaximum validation",
"schema": {
"maximum": 3.0,
"exclusiveMaximum": true
},
"tests": [
{
"description": "below the maximum is still valid",
"data": 2.2,
"valid": true
},
{
"description": "boundary point is invalid",
"data": 3.0,
"valid": false
}
]
}
]
@@ -0,0 +1,28 @@
[
{
"description": "minItems validation",
"schema": {"minItems": 1},
"tests": [
{
"description": "longer is valid",
"data": [1, 2],
"valid": true
},
{
"description": "exact length is valid",
"data": [1],
"valid": true
},
{
"description": "too short is invalid",
"data": [],
"valid": false
},
{
"description": "ignores non-arrays",
"data": "",
"valid": true
}
]
}
]
@@ -0,0 +1,28 @@
[
{
"description": "minLength validation",
"schema": {"minLength": 2},
"tests": [
{
"description": "longer is valid",
"data": "foo",
"valid": true
},
{
"description": "exact length is valid",
"data": "fo",
"valid": true
},
{
"description": "too short is invalid",
"data": "f",
"valid": false
},
{
"description": "ignores non-strings",
"data": 1,
"valid": true
}
]
}
]
@@ -0,0 +1,42 @@
[
{
"description": "minimum validation",
"schema": {"minimum": 1.1},
"tests": [
{
"description": "above the minimum is valid",
"data": 2.6,
"valid": true
},
{
"description": "below the minimum is invalid",
"data": 0.6,
"valid": false
},
{
"description": "ignores non-numbers",
"data": "x",
"valid": true
}
]
},
{
"description": "exclusiveMinimum validation",
"schema": {
"minimum": 1.1,
"exclusiveMinimum": true
},
"tests": [
{
"description": "above the minimum is still valid",
"data": 1.2,
"valid": true
},
{
"description": "boundary point is invalid",
"data": 1.1,
"valid": false
}
]
}
]
@@ -0,0 +1,23 @@
[
{
"description": "pattern validation",
"schema": {"pattern": "^a*$"},
"tests": [
{
"description": "a matching pattern is valid",
"data": "aaa",
"valid": true
},
{
"description": "a non-matching pattern is invalid",
"data": "abc",
"valid": false
},
{
"description": "ignores non-strings",
"data": true,
"valid": true
}
]
}
]
@@ -0,0 +1,79 @@
[
{
"description":
"patternProperties validates properties matching a regex",
"schema": {
"patternProperties": {
"f.*o": {"type": "integer"}
}
},
"tests": [
{
"description": "a single valid match is valid",
"data": {"foo": 1},
"valid": true
},
{
"description": "multiple valid matches is valid",
"data": {"foo": 1, "foooooo" : 2},
"valid": true
},
{
"description": "a single invalid match is invalid",
"data": {"foo": "bar", "fooooo": 2},
"valid": false
},
{
"description": "multiple invalid matches is invalid",
"data": {"foo": "bar", "foooooo" : "baz"},
"valid": false
},
{
"description": "ignores non-objects",
"data": 12,
"valid": true
}
]
},
{
"description": "multiple simultaneous patternProperties are validated",
"schema": {
"patternProperties": {
"a*": {"type": "integer"},
"aaa*": {"maximum": 20}
}
},
"tests": [
{
"description": "a single valid match is valid",
"data": {"a": 21},
"valid": true
},
{
"description": "a simultaneous match is valid",
"data": {"aaaa": 18},
"valid": true
},
{
"description": "multiple matches is valid",
"data": {"a": 21, "aaaa": 18},
"valid": true
},
{
"description": "an invalid due to one is invalid",
"data": {"a": "bar"},
"valid": false
},
{
"description": "an invalid due to the other is invalid",
"data": {"aaaa": 31},
"valid": false
},
{
"description": "an invalid due to both is invalid",
"data": {"aaa": "foo", "aaaa": 31},
"valid": false
}
]
}
]
@@ -0,0 +1,92 @@
[
{
"description": "object properties validation",
"schema": {
"properties": {
"foo": {"type": "integer"},
"bar": {"type": "string"}
}
},
"tests": [
{
"description": "both properties present and valid is valid",
"data": {"foo": 1, "bar": "baz"},
"valid": true
},
{
"description": "one property invalid is invalid",
"data": {"foo": 1, "bar": {}},
"valid": false
},
{
"description": "both properties invalid is invalid",
"data": {"foo": [], "bar": {}},
"valid": false
},
{
"description": "doesn't invalidate other properties",
"data": {"quux": []},
"valid": true
},
{
"description": "ignores non-objects",
"data": [],
"valid": true
}
]
},
{
"description":
"properties, patternProperties, additionalProperties interaction",
"schema": {
"properties": {
"foo": {"type": "array", "maxItems": 3},
"bar": {"type": "array"}
},
"patternProperties": {"f.o": {"minItems": 2}},
"additionalProperties": {"type": "integer"}
},
"tests": [
{
"description": "property validates property",
"data": {"foo": [1, 2]},
"valid": true
},
{
"description": "property invalidates property",
"data": {"foo": [1, 2, 3, 4]},
"valid": false
},
{
"description": "patternProperty invalidates property",
"data": {"foo": []},
"valid": false
},
{
"description": "patternProperty validates nonproperty",
"data": {"fxo": [1, 2]},
"valid": true
},
{
"description": "patternProperty invalidates nonproperty",
"data": {"fxo": []},
"valid": false
},
{
"description": "additionalProperty ignores property",
"data": {"bar": []},
"valid": true
},
{
"description": "additionalProperty validates others",
"data": {"quux": 3},
"valid": true
},
{
"description": "additionalProperty invalidates others",
"data": {"quux": "foo"},
"valid": false
}
]
}
]
@@ -0,0 +1,84 @@
[
{
"description": "root pointer ref",
"schema": {
"properties": {
"foo": {"$ref": "#"}
},
"additionalProperties": false
},
"tests": [
{
"description": "match",
"data": {"foo": false},
"valid": true
},
{
"description": "recursive match",
"data": {"foo": {"foo": false}},
"valid": true
},
{
"description": "mismatch",
"data": {"bar": false},
"valid": false
},
{
"description": "recursive mismatch",
"data": {"foo": {"bar": false}},
"valid": false
}
]
},
{
"description": "relative pointer ref",
"schema": {
"properties": {
"foo": {"type": "integer"},
"bar": {"$ref": "#/properties/foo"}
}
},
"tests": [
{
"description": "match",
"data": {"bar": 3},
"valid": true
},
{
"description": "mismatch",
"data": {"bar": true},
"valid": false
}
]
},
{
"description": "escaped pointer ref",
"schema": {
"tilda~field": {"type": "integer"},
"slash/field": {"type": "integer"},
"percent%field": {"type": "integer"},
"properties": {
"tilda": {"$ref": "#/tilda~0field"},
"slash": {"$ref": "#/slash~1field"},
"percent": {"$ref": "#/percent%25field"}
}
},
"tests": [
{
"description": "slash",
"data": {"slash": "aoeu"},
"valid": false
},
{
"description": "tilda",
"data": {"tilda": "aoeu"},
"valid": false
},
{
"description": "percent",
"data": {"percent": "aoeu"},
"valid": false
}
]
}
]
@@ -0,0 +1,53 @@
[
{
"description": "required validation",
"schema": {
"properties": {
"foo": {"required" : true},
"bar": {}
}
},
"tests": [
{
"description": "present required property is valid",
"data": {"foo": 1},
"valid": true
},
{
"description": "non-present required property is invalid",
"data": {"bar": 1},
"valid": false
}
]
},
{
"description": "required default validation",
"schema": {
"properties": {
"foo": {}
}
},
"tests": [
{
"description": "not required by default",
"data": {},
"valid": true
}
]
},
{
"description": "required explicitly false validation",
"schema": {
"properties": {
"foo": {"required": false}
}
},
"tests": [
{
"description": "not required if required is false",
"data": {},
"valid": true
}
]
}
]
@@ -0,0 +1,479 @@
[
{
"description": "integer type matches integers",
"schema": {"type": "integer"},
"tests": [
{
"description": "an integer is an integer",
"data": 1,
"valid": true
},
{
"description": "a float is not an integer",
"data": 1.1,
"valid": false
},
{
"description": "a float is not an integer even without fractional part",
"data": 1.0,
"valid": false
},
{
"description": "a string is not an integer",
"data": "foo",
"valid": false
},
{
"description": "an object is not an integer",
"data": {},
"valid": false
},
{
"description": "an array is not an integer",
"data": [],
"valid": false
},
{
"description": "a boolean is not an integer",
"data": true,
"valid": false
},
{
"description": "null is not an integer",
"data": null,
"valid": false
}
]
},
{
"description": "number type matches numbers",
"schema": {"type": "number"},
"tests": [
{
"description": "an integer is a number",
"data": 1,
"valid": true
},
{
"description": "a float is a number",
"data": 1.1,
"valid": true
},
{
"description": "a string is not a number",
"data": "foo",
"valid": false
},
{
"description": "an object is not a number",
"data": {},
"valid": false
},
{
"description": "an array is not a number",
"data": [],
"valid": false
},
{
"description": "a boolean is not a number",
"data": true,
"valid": false
},
{
"description": "null is not a number",
"data": null,
"valid": false
}
]
},
{
"description": "string type matches strings",
"schema": {"type": "string"},
"tests": [
{
"description": "1 is not a string",
"data": 1,
"valid": false
},
{
"description": "a float is not a string",
"data": 1.1,
"valid": false
},
{
"description": "a string is a string",
"data": "foo",
"valid": true
},
{
"description": "an object is not a string",
"data": {},
"valid": false
},
{
"description": "an array is not a string",
"data": [],
"valid": false
},
{
"description": "a boolean is not a string",
"data": true,
"valid": false
},
{
"description": "null is not a string",
"data": null,
"valid": false
}
]
},
{
"description": "object type matches objects",
"schema": {"type": "object"},
"tests": [
{
"description": "an integer is not an object",
"data": 1,
"valid": false
},
{
"description": "a float is not an object",
"data": 1.1,
"valid": false
},
{
"description": "a string is not an object",
"data": "foo",
"valid": false
},
{
"description": "an object is an object",
"data": {},
"valid": true
},
{
"description": "an array is not an object",
"data": [],
"valid": false
},
{
"description": "a boolean is not an object",
"data": true,
"valid": false
},
{
"description": "null is not an object",
"data": null,
"valid": false
}
]
},
{
"description": "array type matches arrays",
"schema": {"type": "array"},
"tests": [
{
"description": "an integer is not an array",
"data": 1,
"valid": false
},
{
"description": "a float is not an array",
"data": 1.1,
"valid": false
},
{
"description": "a string is not an array",
"data": "foo",
"valid": false
},
{
"description": "an object is not an array",
"data": {},
"valid": false
},
{
"description": "an array is not an array",
"data": [],
"valid": true
},
{
"description": "a boolean is not an array",
"data": true,
"valid": false
},
{
"description": "null is not an array",
"data": null,
"valid": false
}
]
},
{
"description": "boolean type matches booleans",
"schema": {"type": "boolean"},
"tests": [
{
"description": "an integer is not a boolean",
"data": 1,
"valid": false
},
{
"description": "a float is not a boolean",
"data": 1.1,
"valid": false
},
{
"description": "a string is not a boolean",
"data": "foo",
"valid": false
},
{
"description": "an object is not a boolean",
"data": {},
"valid": false
},
{
"description": "an array is not a boolean",
"data": [],
"valid": false
},
{
"description": "a boolean is not a boolean",
"data": true,
"valid": true
},
{
"description": "null is not a boolean",
"data": null,
"valid": false
}
]
},
{
"description": "null type matches only the null object",
"schema": {"type": "null"},
"tests": [
{
"description": "an integer is not null",
"data": 1,
"valid": false
},
{
"description": "a float is not null",
"data": 1.1,
"valid": false
},
{
"description": "a string is not null",
"data": "foo",
"valid": false
},
{
"description": "an object is not null",
"data": {},
"valid": false
},
{
"description": "an array is not null",
"data": [],
"valid": false
},
{
"description": "a boolean is not null",
"data": true,
"valid": false
},
{
"description": "null is null",
"data": null,
"valid": true
}
]
},
{
"description": "any type matches any type",
"schema": {"type": "any"},
"tests": [
{
"description": "any type includes integers",
"data": 1,
"valid": true
},
{
"description": "any type includes float",
"data": 1.1,
"valid": true
},
{
"description": "any type includes string",
"data": "foo",
"valid": true
},
{
"description": "any type includes object",
"data": {},
"valid": true
},
{
"description": "any type includes array",
"data": [],
"valid": true
},
{
"description": "any type includes boolean",
"data": true,
"valid": true
},
{
"description": "any type includes null",
"data": null,
"valid": true
}
]
},
{
"description": "multiple types can be specified in an array",
"schema": {"type": ["integer", "string"]},
"tests": [
{
"description": "an integer is valid",
"data": 1,
"valid": true
},
{
"description": "a string is valid",
"data": "foo",
"valid": true
},
{
"description": "a float is invalid",
"data": 1.1,
"valid": false
},
{
"description": "an object is invalid",
"data": {},
"valid": false
},
{
"description": "an array is invalid",
"data": [],
"valid": false
},
{
"description": "a boolean is invalid",
"data": true,
"valid": false
},
{
"description": "null is invalid",
"data": null,
"valid": false
}
]
},
{
"description": "types can include schemas",
"schema": {
"type": [
"array",
{"type": "object"}
]
},
"tests": [
{
"description": "an integer is invalid",
"data": 1,
"valid": false
},
{
"description": "a string is invalid",
"data": "foo",
"valid": false
},
{
"description": "a float is invalid",
"data": 1.1,
"valid": false
},
{
"description": "an object is valid",
"data": {},
"valid": true
},
{
"description": "an array is valid",
"data": [],
"valid": true
},
{
"description": "a boolean is invalid",
"data": true,
"valid": false
},
{
"description": "null is invalid",
"data": null,
"valid": false
}
]
},
{
"description":
"when types includes a schema it should fully validate the schema",
"schema": {
"type": [
"integer",
{
"properties": {
"foo": {"type": "null"}
}
}
]
},
"tests": [
{
"description": "an integer is valid",
"data": 1,
"valid": true
},
{
"description": "an object is valid only if it is fully valid",
"data": {"foo": null},
"valid": true
},
{
"description": "an object is invalid otherwise",
"data": {"foo": "bar"},
"valid": false
}
]
},
{
"description": "types from separate schemas are merged",
"schema": {
"type": [
{"type": ["string"]},
{"type": ["array", "null"]}
]
},
"tests": [
{
"description": "an integer is invalid",
"data": 1,
"valid": false
},
{
"description": "a string is valid",
"data": "foo",
"valid": true
},
{
"description": "an array is valid",
"data": [1, 2, 3],
"valid": true
}
]
}
]
@@ -0,0 +1,79 @@
[
{
"description": "uniqueItems validation",
"schema": {"uniqueItems": true},
"tests": [
{
"description": "unique array of integers is valid",
"data": [1, 2],
"valid": true
},
{
"description": "non-unique array of integers is invalid",
"data": [1, 1],
"valid": false
},
{
"description": "numbers are unique if mathematically unequal",
"data": [1.0, 1.00, 1],
"valid": false
},
{
"description": "unique array of objects is valid",
"data": [{"foo": "bar"}, {"foo": "baz"}],
"valid": true
},
{
"description": "non-unique array of objects is invalid",
"data": [{"foo": "bar"}, {"foo": "bar"}],
"valid": false
},
{
"description": "unique array of nested objects is valid",
"data": [
{"foo": {"bar" : {"baz" : true}}},
{"foo": {"bar" : {"baz" : false}}}
],
"valid": true
},
{
"description": "non-unique array of nested objects is invalid",
"data": [
{"foo": {"bar" : {"baz" : true}}},
{"foo": {"bar" : {"baz" : true}}}
],
"valid": false
},
{
"description": "unique array of arrays is valid",
"data": [["foo"], ["bar"]],
"valid": true
},
{
"description": "non-unique array of arrays is invalid",
"data": [["foo"], ["foo"]],
"valid": false
},
{
"description": "1 and true are unique",
"data": [1, true],
"valid": true
},
{
"description": "0 and false are unique",
"data": [0, false],
"valid": true
},
{
"description": "unique heterogeneous types are valid",
"data": [{}, [1], true, null, 1],
"valid": true
},
{
"description": "non-unique heterogeneous types are invalid",
"data": [{}, [1], true, null, {}, 1],
"valid": false
}
]
}
]
+1 -1
View File
@@ -129,7 +129,7 @@ namespace Newtonsoft.Json.Tests
catch (TException ex)
{
if (message != null)
Assert.AreEqual(message, ex.Message, "Unexpected exception message." + Environment.NewLine + "Expected: " + message + Environment.NewLine + "Got: " + ex.Message);
Assert.AreEqual(message, ex.Message, "Unexpected exception message." + Environment.NewLine + "Expected: " + message + Environment.NewLine + "Got: " + ex.Message + Environment.NewLine + Environment.NewLine + ex);
}
catch (Exception ex)
{